-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathi18n.sh
180 lines (152 loc) · 3.19 KB
/
i18n.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
#!/bin/bash
mapfile -t LANG < <(ls nezha/translations)
TEMPLATE="nezha/template.pot"
PODIR="nezha/translations/%s/LC_MESSAGES"
GIT_ROOT=$(git rev-parse --show-toplevel)
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
err() {
printf "${red}%s${plain}\n" "$*" >&2
}
success() {
printf "${green}%s${plain}\n" "$*"
}
info() {
printf "${yellow}%s${plain}\n" "$*"
}
generate() {
case $1 in
"template")
generate_template
;;
"en")
generate_en
;;
*)
err "invalid argument"
;;
esac
}
generate_template() {
xgettext -C --add-comments=TRANSLATORS: -k_ --from-code=UTF-8 -o $TEMPLATE ./nezha/template.sh
}
generate_en() {
local po_file
po_file=$(printf "$PODIR/nezha.po" "en_US")
local mo_file
mo_file=$(printf "$PODIR/nezha.mo" "en_US")
msginit --input=$TEMPLATE --locale=en_US.UTF-8 --output-file="$po_file" --no-translator
msgfmt "$po_file" -o "$mo_file"
}
compile() {
if [[ $# != 0 && "$1" != "" ]]; then
compile_single "$1"
else
compile_all
fi
}
compile_single() {
local param="$1"
local found=0
for lang in "${LANG[@]}"; do
if [[ "$lang" == "$param" ]]; then
found=1
break
fi
done
if [[ $found == 0 ]]; then
err "the language does not exist."
return
fi
local po_file
po_file=$(printf "$PODIR/nezha.po" "$param")
local mo_file
mo_file=$(printf "$PODIR/nezha.mo" "$param")
msgfmt "$po_file" -o "$mo_file"
}
compile_all() {
local po_file
local mo_file
for lang in "${LANG[@]}"; do
po_file=$(printf "$PODIR/nezha.po" "$lang")
mo_file=$(printf "$PODIR/nezha.mo" "$lang")
msgfmt "$po_file" -o "$mo_file"
done
}
update() {
if [[ $# != 0 && "$1" != "" ]]; then
update_single "$1"
else
update_all
fi
}
update_single() {
local param="$1"
local found=0
for lang in "${LANG[@]}"; do
if [[ "$lang" == "$param" ]]; then
found=1
break
fi
done
if [[ $found == 0 ]]; then
err "the language does not exist."
return
fi
local po_file
po_file=$(printf "$PODIR/nezha.po" "$param")
msgmerge -U "$po_file" $TEMPLATE
}
update_all() {
for lang in "${LANG[@]}"; do
local po_file
po_file=$(printf "$PODIR/nezha.po" "$lang")
msgmerge -U "$po_file" $TEMPLATE
done
}
show_help() {
echo "Usage: $0 [command] args"
echo ""
echo "Available commands:"
echo " update Update .po from .pot"
echo " compile Compile .mo from .po"
echo " generate Generate template or en_US locale"
echo ""
echo "Examples:"
echo " $0 update # Update all locales"
echo " $0 update zh_CN # Update zh_CN locale"
echo " $0 compile # Compile all locales"
echo " $0 compile zh_CN # Compile zh_CN locale"
echo " $0 generate template # Generate template"
echo " $0 generate en # Generate en_US locale"
}
version() { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }
main() {
if [[ $(version "$BASH_VERSION") < $(version "4.0") ]]; then
err "This version of bash does not support mapfile"
exit 1
fi
if [[ $PWD != "$GIT_ROOT" ]]; then
err "Must execute in the project root"
exit 1
fi
case "$1" in
"update")
update "$2"
;;
"compile")
compile "$2"
;;
"generate")
generate "$2"
;;
*)
echo "Error: Unknown command '$1'"
show_help
exit 1
;;
esac
}
main "$@"