Revision of generateTranslationDiffs, issue 2138

This commit is contained in:
DrSlony
2013-12-24 13:48:48 +01:00
parent 2175b799c2
commit b6f1af48ad

View File

@@ -1,59 +1,63 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This script iterates through interface translation files,
# moves comments to the front, puts translated strings next,
# and finally looks for untranslated/missing strings by matching
# against "default" which it then adds to the translation, each
# line prepended by "!".
# #
# Append translation differences on the end of all files. Developers should run this script # Developers should run it from the project root after receiving
# after changing default, so that translators can easily see what items need to be translated. # a translation file from a translator:
# cp /tmp/new_japanese_translation rtdata/languages/Japanese
# ./tools/generateTranslationDiffs "Japanese"
# #
# This script should be run from the project root, e.g: # Running the script without an argument iterates through all files.
# $ ./tools/generateTranslationDiffs.sh
#
#####################
tmp=temp_file tmp=temp_file
cd "rtdata/languages" cd "rtdata/languages" || { printf "%s\n" "You must run this script from the root of the project."; exit 1; }
if [[ $? != 0 ]]; then # Build array of all interface translation files, or use user-specified ones only
printf "%s\n" "You must run this script from the root of the project." unset langFiles
exit if [[ $# = 0 ]]; then
while read -r -d $'\0'; do
langFiles+=("$REPLY")
done < <(find . -not -iname "default" -not -iname "LICENSE" -not -iname "README" -not -iname "*.sh" -not -iname ".*" -not -iname "$tmp" -print0)
else
langFiles=("$@")
for langFile in "${langFiles[@]}"; do
if [[ ! -w $langFile ]]; then
printf "%s\n" "File \"$langFile\" not found or not writable." ""
exit 1
fi
done
fi fi
# First thing, we want to strip default of any "!" and duplicates. # First thing, we want to strip default of any "!" and duplicates.
dos2unix *
grep -v '^!' default | sort -Vu > "$tmp" grep -v '^!' default | sort -Vu > "$tmp"
mv "$tmp" "default" mv "$tmp" "default"
printf "%s\n" "Generating differences... this may take a few minutes." for file in "${langFiles[@]}"; do
t1="$(date +%s)"
printf "%s" "Processing $file"
dos2unix "$file" 2>/dev/null
# printf "%s\n" "Searching $file for changed strings"
# Find all already-translated files unset trLines newLines
find . -not -iname "default" -not -iname "LICENSE" -not -iname "README" -not -iname "*.sh" -not -iname ".*" -not -iname "$tmp" |
# For every file found # Fill trLines with translated text
while read x; do trLines+=("$(grep -Ev '^($|#|\!)' "$file" | sort -Vu)")
printf "%s\n" "Working on differences for $x"
# Find every already-translated line (every line that does not start with an "!") in file "$x", then sort and copy the file to "$tmp" # KEY;String
grep -v '^!' "$x" | sort -Vu > "$tmp" # Match "default" keys with those in current translation file. If no match, add !KEY;String
while read -r 'defLine'; do
printf "%s\n" '' '!!!!!!!!!!!!!!!!!!!!!!!!!' '! Untranslated keys follow; remove the ! prefix after an entry is translated.' '!!!!!!!!!!!!!!!!!!!!!!!!!' '' >> "$tmp" if [[ ! "${trLines[@]}" =~ "${defLine%%;*}" ]]; then
newLines+=("!${defLine}")
# Now to delete the already-translated lines and add them to $tmp fi
# Find every line that is not a comment done < <(grep "^[[:alnum:]].*" default)
grep -v "^#" default | while read -r 'line'
do
# Set "key" to be just the key, without the ";human text"
key="${line%%;*}"
# Scan the translated file $x for a translated $key
grep -q "^$key" "$x"
# If it did not find a translated key in $x, # Form final translation file
if [[ $? != 0 ]] printf "%s\n" "$(grep '^#' "$file" | sort -Vu)" "" "${trLines[@]}" "" "!!!!!!!!!!!!!!!!!!!!!!!!!" "! Untranslated keys follow; remove the ! prefix after an entry is translated." "!!!!!!!!!!!!!!!!!!!!!!!!!" "" "${newLines[@]}" > "$file"
then t2="$(date +%s)"
# then append a "!" to the line and send it to the language file tt=$((t2-t1))
printf "%s\n" "!${line}" >> "$tmp" printf "%s\n" " - took $tt seconds"
fi
done
# Replace the old file with the new one, with a section at the end for differences.
mv "$tmp" "$x"
done done
printf "%s\n" "Finished generating differences."