forked from typora/Typora-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.sh
executable file
·276 lines (232 loc) · 7.11 KB
/
tools.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh
### Typora i18n development tool
#
# Created by
# @Hayao0819 / Yamada Hayao <hayao@fascode.net>
#
set -e -u
# Show help message if command is help or empty.
if [ -z "$*" ] || [ "$1" = "help" ]; then
cat << EOF
Usage: $0 COMMAND ARGS
Command:
keys LANG Show a key list in specified language.
missing LANG Show a sorted key list that is not translated.
old LANG Show a sorted key list that have been changed and are not currently in use.
diff LANG Show differences between Base and the specified language
percent LANG Show the percentage that has been translated.
all-percent Show the percentage of all languages translated.
translated LANG Show the translated text.
help Show this help message.
Example:
keys ja-JP # This shows key list in Japanese
EOF
exit 0
fi
# internal function
# This function makes keys list from strings file.
# Usage: make_key <path to .strings file>
make_keys(){
#grep "=" < "$1" | cut -d "=" -f 1 | sed -e 's/^ *//' -e 's/ *$//' | sort
grep "=" < "$1" | awk -F "\" *= *\"" '{print $1}' | grep -xv "^$" | sed "s/^\"//g"
}
# internal function
# This function will make ./keys-* dir and store key list to each files
# Usage: make_keyslist <lang>
make_keyslist(){
if [ -e "./keys-$1/" ]; then
return 0
fi
# make temp dir for each lang
mkdir -p "./keys-$1"
# remove file when script exists due to something wrong
if [ "${TYPORA_I18N_NOCLEAN-""}" != true ]; then
trap clean_all_keyslist 1 2 3 15
fi
# run make_keys and write it to each files
for _f in ."/${1}.lproj/"*.strings; do
make_keys "$_f" > "./keys-$1/$(basename "$_f")"
done
}
# internal function
# This function will make ./keys-* dir and store sorted key list to each files
# Usage: make_keyslist <lang>
make_sorted_keyslist(){
if [ -e "./keys-$1/" ]; then
return 0
fi
# make temp dir for each lang
mkdir -p "./keys-$1"
# run make_keys and write it to each files
for _f in ."/${1}.lproj/"*.strings; do
make_keys "$_f" | sort > "./keys-$1/$(basename "$_f")"
done
}
clean_all_keyslist(){
# clean up
[ "${TYPORA_I18N_NOCLEAN-""}" = true ] && return 0
rm -rf "./keys-"*
}
# Check lang
# Usage: check_lang <lang>
check_lang(){
# Empty stirng
[ -n "${1-""}" ] || {
echo "Missing argument" >&2
exit 1
}
# Check exists
[ -d "./${1}.lproj" ] || {
echo "Unknown lang: $1" >&2
exit 1
}
}
# Compare lang and base
# This function has two command functions: old and missing
# Usage: compare_lang <command(old or missing)> <lang> [filename]
# If filename is specified, only show the file
compare_lang(){
check_lang "${2-""}"
# make key list
make_sorted_keyslist "${2}"
make_sorted_keyslist "Base"
# diff key list
for _file in "./keys-Base/"*.strings; do
if [ -n "${3-""}" ] && [ "$(basename "$_file")" != "${3-""}" ]; then
continue
fi
if ! [ -e "./keys-$2/$(basename "$_file")" ]; then
echo "Missing file: ./keys-$2/$(basename "$_file")" >&2
continue
else
echo "In $(basename "$_file")"
if [ "$1" = "old" ]; then
# Keys present in translation but not in Base
#grep -xv -f "$_file" "./keys-$2/$(basename "$_file")" | sed "s/^/ /g"
#join -v 2 "$_file" "./keys-$2/$(basename "$_file")" | sed "s/^/ /g"
diff "./keys-$2/$(basename "$_file")" "$_file" | grep "^<" | sed "s/^</ /g"
elif [ "$1" = "missing" ]; then
# Keys that exist in Base but have not yet been translated
#grep -xv -f "./keys-$2/$(basename "$_file")" "$_file" | sed "s/^/ /g"
#join -v 1 "$_file" "./keys-$2/$(basename "$_file")" | sed "s/^/ /g"
diff "./keys-$2/$(basename "$_file")" "$_file" | grep "^>" | sed "s/^>/ /g"
fi
fi
done
clean_all_keyslist
}
# Show a key list that is not translated
missing_command(){
compare_lang "missing" "$@"
}
# Show a key list that there is not in Base
old_command(){
compare_lang "old" "$@"
}
# Show the defference between Base and specified lang
diff_command(){
check_lang "${1-""}"
# make key list
make_keyslist "${1}"
make_keyslist "Base"
# diff key list
for _file in "./keys-Base/"*.strings; do
if [ -n "${2-""}" ] && [ "$(basename "$_file")" != "${2-""}" ]; then
continue
fi
if ! [ -e "./keys-$1/$(basename "$_file")" ]; then
echo "Missing file: ./keys-$1/$(basename "$_file")" >&2
continue
else
echo "In $(basename "$_file")"
diff "./keys-$1/$(basename "$_file")" "$_file" | sed "s/^/ /g"
fi
done
clean_all_keyslist
}
# Show the percentage that has been translated
percent_command(){
# Check lang name
check_lang "${1-""}"
# make keys-* dir
make_keyslist "$1"
make_keyslist "Base"
_base_lines=$(cat ./keys-Base/*.strings | wc -l ) # Base line number
_missing_lines=$(missing_command "$1" | grep -c " " || true) # Count keys which has not been translated yet
# calculate
#echo "$(( 100 - _missing_lines * 100 / _base_lines ))%"
awk "BEGIN{ printf \"%.2f%%\n\", (100 - $_missing_lines * 100 / $_base_lines) }"
# Remove used files
clean_all_keyslist
}
# Show the progress of all language
all_percent_command(){
for lang in ./*.lproj; do
lang="${lang%".lproj"}"
lang="${lang#"./"}"
if [ "$lang" = "." ] || [ "$lang" = "Base" ]; then
continue
fi
echo "$lang: $(percent_command "$lang")"
done
# Remove temp files
clean_all_keyslist
}
# Show a key list to stdout
# Usage: keys_command <lang>
keys_command(){
check_lang "$@"
# Run make_keys with all files and print key list
for file in "./$1.lproj/"*.strings; do
make_keys "$file"
done
unset file
}
translated_command(){
check_lang "${1-""}"
# run make_keys and write it to each files
for _f in ."/${1}.lproj/"*.strings; do
if [ -n "${2-""}" ] && ! [ "$(basename "$_f")" = "${2-""}" ]; then
continue
fi
grep "=" < "$_f" | awk -F "\" *= *\"" '{print $2}' | grep -xv "^$" | sed -e "s/^\"//g" -e "s/\" *;$//g"
done
}
# Parse options
TYPORA_I18N_NOCLEAN="${TYPORA_I18N_NOCLEAN-""}"
if [ "$1" = "--noclean" ];then
TYPORA_I18N_NOCLEAN=true
shift 1
fi
# Set command
command="${1-""}"
shift 1 || true
# Run each command
case "${command}" in
"keys")
keys_command "$@"
;;
"missing")
missing_command "$@"
;;
"percent")
percent_command "$@"
;;
"old")
old_command "$@"
;;
"all-percent")
all_percent_command "$@"
;;
"diff")
diff_command "$@"
;;
"translated")
translated_command "$@"
;;
*)
echo "Undefined command: $command" >&2
echo "Run help command to show usage." >&2
exit 1
;;
esac