-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate
executable file
·173 lines (152 loc) · 5.32 KB
/
translate
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
#!/bin/bash
# _ _ _ ___
# | | (_) |__ _ _ ___/ __| ___ _ ___ _____ _ _
# | |__| | '_ \ '_/ -_)__ \/ -_) '_\ V / -_) '_|
# |____|_|_.__/_| \___|___/\___|_| \_/\___|_|
#
# A script to create and install translations for commands
#
# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='libreserver'
# languages to translate into
language=( af sq ar eu be bs bg ca hr zh_cn zh_tw cs da nl en_us et fa fil fi fr fr_CH fr_BE fr_ca ga gl ka de de_du el gu he hi hu is id it ja kn km ko lo lt lv ml ms mi_tn mi_wwow mn no no_gr nn pl pt pt_br ro ru sm sr sk sl so es sv tl ta th to tr uk vi )
MY_EMAIL_ADDRESS='[email protected]'
COMMAND_FILES=src/${PROJECT_NAME}*
function install_i18next-conv {
SUDO=''
if [ -f /usr/bin/sudo ]; then
SUDO='sudo'
fi
if [ -f /usr/sbin/sudo ]; then
SUDO='sudo'
fi
if [ ! -f /usr/bin/i18next-conv ]; then
${SUDO} apt-get install -y curl npm
curl -sL https://deb.nodesource.com/setup_0.12 | ${SUDO} bash -
${SUDO} apt-get install -y nodejs
${SUDO} npm install i18next-conv -g
fi
}
function create_translation_files {
create_arg=$1
if [ ! -d /tmp/${PROJECT_NAME} ]; then
mkdir -p /tmp/${PROJECT_NAME}
fi
for f in $COMMAND_FILES
do
COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
bash --dump-po-strings src/${COMMAND_NAME} | xgettext --msgid-bugs-address=$MY_EMAIL_ADDRESS -L PO -o /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -
if [ -f /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot ]; then
for lang in "${language[@]}"
do
if [ ! -d locale/${lang} ]; then
mkdir -p locale/${lang}
fi
if [[ ! -f locale/${lang}/${COMMAND_NAME}.json || "$create_arg" == "overwrite" ]]; then
# create po file
echo "Creating ${lang} Translation file for ${COMMAND_NAME}..."
msginit --no-translator -l ${lang} -i /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -o locale/${lang}/${COMMAND_NAME}.po
echo 'testing'
# convert po to json
if [ -f /usr/bin/i18next-conv ]; then
if [ -f locale/${lang}/${COMMAND_NAME}.po ]; then
i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.po -t locale/${lang}/${COMMAND_NAME}.json
git add locale/${lang}/${COMMAND_NAME}.json
fi
fi
rm locale/${lang}/${COMMAND_NAME}.po
fi
done
rm /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot
fi
done
}
function add_all_translations {
for lang in "${language[@]}"
do
git add locale/${lang}/*
done
}
function remove_all_translations {
for lang in "${language[@]}"
do
rm locale/${lang}/*
done
}
function install_translations {
for f in $COMMAND_FILES
do
COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
for lang in "${language[@]}"
do
# convert json to mo
if [ -f /usr/bin/i18next-conv ]; then
if [ ! -f locale/${lang}/${COMMAND_NAME}.mo ]; then
if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t locale/${lang}/${COMMAND_NAME}.mo
git add locale/${lang}/${COMMAND_NAME}.mo
fi
fi
fi
# install the mo
if [ -d /usr/share/locale/${lang} ]; then
if [ -f locale/${lang}/${COMMAND_NAME}.mo ]; then
cp locale/${lang}/${COMMAND_NAME}.mo /usr/share/locale/${lang}/${COMMAND_NAME}.mo
fi
fi
done
done
}
function uninstall_translations {
for f in $COMMAND_FILES
do
COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
for lang in "${language[@]}"
do
if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
fi
done
done
}
if [[ $1 == "translation"* ]]; then
install_i18next-conv
create_translation_files overwrite
add_all_translations
exit 0
fi
if [[ $1 == "remove"* ]]; then
remove_all_translations
exit 0
fi
if [[ $1 == "make" ]]; then
install_i18next-conv
create_translation_files
add_all_translations
exit 0
fi
if [[ $1 == "install" ]]; then
install_translations
exit 0
fi
if [[ $1 == "uninstall" ]]; then
uninstall_translations
exit 0
fi
exit 1