#!/usr/bin/env bash set -e BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" REPO_ROOT=$BIN_DIR/../../.. cd $REPO_ROOT SSK_DIR="./SignalServiceKit/src" SAE_DIR="./SignalShareExtension" SM_DIR="./SignalMessaging" TARGETS="Signal/src ${SSK_DIR} ${SAE_DIR} ${SM_DIR}" TMP="$(mktemp -d)" STRINGFILE="Signal/translations/en.lproj/Localizable.strings" # Assert preconditions before we do any work # We're more likely to notice errors this way for TARGET_DIR in $TARGETS do if [ ! -d $TARGET_DIR ]; then echo "Unable to find required directory: ${TARGET_DIR}." exit 1 fi done JSQ_STRINGS_PATH="../JSQMessagesViewController/JSQMessagesViewController/Assets/JSQMessagesAssets.bundle/Base.lproj/JSQMessages.strings" if [ ! -f $JSQ_STRINGS_PATH ]; then echo "[!] Error. Expected to find strings for JSQMessagesViewController at ${JSQ_STRINGS_PATH}" exit 1 fi # Now that we've check all our pre-conditions, proceed with the work. # Search directories for .m & .h files and collect string definitions with genstrings find $TARGETS -name "*.m" -print0 -o -name "*.h" -print0 -o -name "*.swift" -print0 | xargs -0 genstrings -o $TMP # We have to convert the new .strings files to UTF-8 in order to deal with them # STRINGFILE is already UTF-8. OLDUTF8=$(cat $STRINGFILE) NEWUTF8=$(iconv -f UTF-16 -t UTF-8 $TMP/Localizable.strings) # Let's merge the old with the new .strings file: # 1. Select old string definition lines # 2. Setup field separators # 3. Read old string definitions as associative array # 4. In new file, if possible, insert old definition # 5. Add separator and semicolon only for string definition lines # 6. Convert output back to UTF-16 to final location echo "$OLDUTF8" | grep -Eo '^".*"' | \ awk 'BEGIN {FS = "[ ]*=[ ]*"; OFS = ""} \ NR == FNR {a[$1] = $2; next} \ {$2 = ($1 in a ? a[$1] : $2); \ if($2 ~ /"[;]*$/){$2 = " = "$2}; \ if($2 ~ /"$/){$2 = $2";"}; \ print}' - <(echo "$NEWUTF8") > $STRINGFILE # Copy in strings from JSQMessagesViewController # # We have a wider array of translators than JSQ, and it's easier for our # translators to translate for us directly rather than to direct them to the # now defunct JSQMessagesView project. echo "Copying strings from JSQMessagesViewController" echo " // Strings Copied in from JSQMessagesViewController" >> $STRINGFILE cat $JSQ_STRINGS_PATH | grep -v "^//" >> $STRINGFILE