File: generateTranslationDiffs

package info (click to toggle)
rawtherapee 5.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124,328 kB
  • sloc: cpp: 271,715; ansic: 27,904; sh: 1,109; python: 521; cs: 155; xml: 57; makefile: 15
file content (129 lines) | stat: -rwxr-xr-x 3,931 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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 "!".
#
# Developers should run it from the project root after receiving
# a translation file from a translator:
# cp /tmp/new_japanese_translation rtdata/languages/Japanese
# ./tools/generateTranslationDiffs "Japanese"
#
# Running the script without an argument iterates through all files.
#
# Locale files are generated automatically:
# - English (UK)

tmp=temp_file
if [[ -w $tmp ]]; then
    rm -v "$tmp"
fi

abort () {
    printf "%s\n" "" "Aborting"
    rm -v "$tmp"
    exit 1
}

trap 'abort' HUP INT QUIT ABRT TERM

cd "rtdata/languages" || { printf "%s\n" "You must run this script from the root of the project."; exit 1; }
# Build array of all interface translation files, or use user-specified ones only
unset langFiles

if [[ $# = 0 ]]; then
    while read -r; do
        langFiles+=("$REPLY")
    done < <(find . -not -iname "default" \
    -not -iname "LICENSE" \
    -not -iname "README" \
    -not -iname "*.sh" \
    -not -iname ".*" \
    -not -iname "$tmp" \
    -not -iname "English (UK)" \
    | sort)
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

getComments () {
    grep -E "^#.+" "$1" | sort -Vu
}

getChanged () {
    grep -Ev '^(!|#|$)' "$1" | sort -Vu -t ';' --key=1,1
}

getUnchanged () {
    grep -E "^\!.+" "$1" | sort -Vu -t ';' --key=1,1
}

# First thing, fix default, so move comments to front, then strip the rest of any "!" and duplicates.
dos2unix default 2>/dev/null
getComments default > "$tmp"
getChanged default >> "$tmp"
mv "$tmp" "default"

i=1
printf "%s\n" "Digging through ${#langFiles[@]} file(s). This may take a while."
ttot1="$(date +%s)"
for file in "${langFiles[@]}"; do
    t1="$(date +%s)"
    printf "%02d - ${file#.*/}" "$i"
    dos2unix "$file" 2>/dev/null
    unset newLines

  # KEY;String
  # Match "default" keys with those in current translation file. If no match, add !KEY;String
  while read -r 'defLine'; do
      defKey="${defLine%%;*}"
      if ! grep -q "^${defKey}\;" "$file"; then
          newLines+=("!${defLine}")
      fi
  done < <(getChanged default)

  # Form final translation file
  # Start with comments
  if [[ -n "$(getComments "$file")" ]]; then
      printf "%s\n" "$(getComments "$file")" "" >> "$tmp"
  fi
  # Add already-translated lines
  getChanged "$file" >> "$tmp"
  printf '%s\n' "" >> "$tmp"
  # End with new, untranslated lines
  if [[ -n "${newLines[@]}" ]]; then
      printf "%s\n" "!!!!!!!!!!!!!!!!!!!!!!!!!" "! Untranslated keys follow; remove the ! prefix after an entry is translated." "!!!!!!!!!!!!!!!!!!!!!!!!!" "" "${newLines[@]}" >> "$tmp"
  fi
  mv "$tmp" "$file"
  t2="$(date +%s)"
  tt=$((t2-t1))
  printf "%s\n" " - took $tt seconds"
  ((i++))
done

case "${langFiles[@]}" in
    *"./English (US)"*) printf "%s\n" "Creating English (UK) file"
        getComments "English (US)" > "English (UK)"
        grep -Ei ".+;.*(color|behavior|center).*" default | \
        sed -e '/^#/d' -e 'h;s/^[^;]*;//; s/olor/olour/g; x;s/;.*//;G;s/\n/;/' \
        -e 'h;s/^[^;]*;//; s/ehavior/ehaviour/g; x;s/;.*//;G;s/\n/;/' \
        -e 'h;s/^[^;]*;//; s/center/centre/g; x;s/;.*//;G;s/\n/;/' \
        -e 'h;s/^[^;]*;//; s/Center/Centre/g; x;s/;.*//;G;s/\n/;/' >> English\ \(UK\)
        grep -Evi ".+;.*(color|behavior|center).*" "English (US)" | grep -Ev "^#" >> "English (UK)"
        ;;&
esac

ttot2="$(date +%s)"
ttot=$((ttot2-ttot1))
tsec=$((ttot%60))
tmin=$((ttot/60))

printf "%s\n" "Finished updating ${#langFiles[@]} files." "Total time: ${tmin}m ${tsec}s"