forked from NaomiProject/Naomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_translations.py
executable file
·265 lines (254 loc) · 8.95 KB
/
update_translations.py
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Update .po files to receive updated translation strings
# Create one .po file for the files in the main naomi directory,
# then run through all the plugins and create .po files for each of them.
import argparse
import itertools
import logging
import os
import re
import shutil
import subprocess
import tempfile
def update_translation_files(
logger,
pygettext_path,
base_dir,
locale_dir,
languages
):
if(os.path.isdir(locale_dir)):
# temp file working directory
locale_temp_dir = tempfile.gettempdir()
# walk through the directory structure and
# make a list of all the .py files
new_phrases_file = os.path.join(locale_temp_dir, "temp.pot")
cmd = [
pygettext_path,
"-k", "gettext",
"-d", "temp",
"-p", locale_temp_dir
]
for dirName, subdirList, fileList in os.walk(base_dir):
for file in [
x for x in fileList if x.endswith(".py") and x != "i18n.py"
]:
cmd.append(os.path.join(dirName, file))
# create the new phrases file
subprocess.check_call(cmd)
for language in languages:
# make a copy of the .po file containing the current
# translations
language_file = os.path.join(locale_dir, "%s.po" % language)
language_temp_file = os.path.join(
locale_temp_dir, "%s.po" % language
)
if(os.path.isfile(language_file)):
shutil.copyfile(language_file, language_temp_file)
else:
# create a language file
open(language_temp_file, "w").close()
# combine the generated pot file with the temporary po file
# and write them back to the working po file location
with open(language_file, "w") as fp:
try:
for line in (subprocess.check_output(
["msgcat", language_temp_file, new_phrases_file]
)):
fp.write(line)
except subprocess.CalledProcessError as e:
logger.error(e.output)
fp.close()
print("Updated %s" % language_file)
# clean up the copied .po file
os.remove(language_temp_file)
os.remove(new_phrases_file)
else:
logger.warn(
"Skipping %s, %s directory does not exist" % (
base_dir, locale_dir
)
)
def check_executable(executable):
try:
devnull = open(os.devnull)
subprocess.Popen(
[
executable,
'--help'
],
stdout=devnull,
stdin=devnull,
stderr=devnull
).communicate()
return True
except OSError:
return False
def main():
logger = logging.getLogger("update_translations")
base_dir = os.path.dirname(os.path.abspath(__file__))
locale_dir = os.path.join(
base_dir,
"naomi",
"data",
"locale"
)
# Get a list of existing translations from
# the language files located in locale_dir
existing_languages = []
for file in os.listdir(locale_dir):
match = re.search(r'\.po$', file)
if(match):
existing_languages.append(file[:match.start()])
# Get the current language(s) from the parameters
parser = argparse.ArgumentParser(
description=" ".join([
'Update .pot files to receive updated translation strings.',
'Translation files exist for the following languages:',
'%s' % existing_languages
])
)
parser.add_argument(
"-l", "--language",
action="append",
nargs='+')
parser.add_argument(
"-d", "--debug",
action="store_true",
help="Show debugging info"
)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.debug else logging.ERROR
)
if(args.language is None):
languages = existing_languages
else:
# each -l argument adds a list which can contain multiple items.
# for example, -l en-US fr-FR results in [['en-US', 'fr-FR']]
# while -l en-US -l fr-FR results in [['en-US'], ['fr-FR']]
# Use itertools to flatten the list of languages.
languages = []
for language in itertools.chain.from_iterable(args.language):
if(language in existing_languages):
languages.append(language)
else:
print(
" ".join([
"%s" % language,
"is not in the current list of translations: ",
"%s." % existing_languages
])
)
response = ""
while response not in ['Y', 'N']:
response = raw_input(
" ".join([
"Are you sure you want to add it",
"as a new language (Y/N)? "
])
).strip().upper()
if(response=='Y'):
languages.append(language)
if(len(languages) > 0):
pygettext_path = "/".join([
"",
"usr",
"share",
"doc",
"python2.7",
"examples",
"Tools",
"i18n",
"pygettext.py"
])
if(check_executable(pygettext_path)):
if(check_executable("msgcat")):
# Update the translations for the "naomi" folder and subfolders
update_translation_files(
logger,
pygettext_path,
os.path.join(base_dir, "naomi"),
locale_dir,
languages
)
# now walk through all the directories in the plugins/*/
# directories
for pluginType in next(
os.walk(
os.path.join(
base_dir, "plugins"
)
)
)[1]:
for plugin in next(
os.walk(
os.path.join(
base_dir, "plugins", pluginType
)
)
)[1]:
if(
os.path.isfile(
os.path.join(
base_dir,
"plugins",
pluginType,
plugin,
"plugin.info"
)
)
):
print("Processing plugin %s type %s" % (
plugin, pluginType
))
update_translation_files(
logger,
pygettext_path,
os.path.join(
base_dir,
"plugins",
pluginType,
plugin
),
os.path.join(
base_dir,
"plugins",
pluginType,
plugin,
"locale"
),
languages
)
print("Finished.")
print(
" ".join([
"Unfortunately, this program has probably introduced",
"duplicate lines into the .po file headers.",
"Please clean them up while updating the translations.",
"Thank you."
])
)
else:
logger.error(
'\n'.join([
"The msgcat command does not exist.",
"Please install the gettext package:",
"",
"sudo apt install gettext"
])
)
else:
logger.error(
'\n'.join([
"File %s does not exist." % pygettext_path,
"Please install the examples package for python 2.7:",
"",
"sudo apt install python2.7-examples"
])
)
else:
print("No languages listed for updating. Exiting.")
if __name__ == "__main__":
main()