-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconvertfiles.py
370 lines (311 loc) · 14.1 KB
/
convertfiles.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014 Jordi Mas i Hernandez <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import logging
import os
import shutil
from .converttmx import ConvertTmx
from .findfiles import FindFiles
from .convertini import ConvertIni
from enum import Enum
class ConversorID(str, Enum):
Ts = "ts"
Strings = "strings"
Php = "php"
Android = "android"
Properties = "properties"
Json = "json"
Csv = "csv"
Apple = "apple"
class ConvertFiles:
def __init__(self, convert_dir, conversor_setup):
self.convert_dir = convert_dir
self.findFiles = None
self.conversor_setup = conversor_setup
def convert(self):
self.findFiles = FindFiles()
self._uncompress_files()
self._convert_tmx_files_to_po()
self._convert_ts_files_to_po()
self._convert_strings_files_to_po()
self._convert_ini_files_to_po()
self._convert_php_resources_files_to_po()
self._convert_android_resources_files_to_po()
self._convert_apple_resources_files_to_po()
self._convert_properties_files_to_po()
self._convert_json_files_to_po()
self._convert_yml_files_to_po()
self._convert_csv_files_to_po()
self._convert_xliff_file_to_po()
def _add_conversor_setup_to_cmd(self, cmd, conversor_id=None):
if (
self.conversor_setup
and conversor_id
and conversor_id.lower() == self.conversor_setup.type
):
if (
self.conversor_setup.type == conversor_id
and self.conversor_setup.verb == "add"
):
cmd += self.conversor_setup.command
logging.info(
f"Adding to conversor '{conversor_id}' parameter '{self.conversor_setup.command}'"
)
return cmd
def _convert_ts_files_to_po(self):
for tsfile in self.findFiles.find_recursive(self.convert_dir, "*.ts"):
fileName, fileExtension = os.path.splitext(tsfile)
logging.info("convert ts file: {0}".format(tsfile))
cmd = self._add_conversor_setup_to_cmd(
"ts2po {0} -o {1}.po".format(tsfile, fileName), ConversorID.Ts
)
os.system(cmd)
def _convert_strings_files_to_po(self):
for tsfile in self.findFiles.find_recursive(self.convert_dir, "ca.strings"):
dirName = os.path.dirname(tsfile)
logging.info("convert strings file: {0}".format(dirName))
filename = "{0}/strings-ca.po".format(dirName)
# Allow process files with duplicated entries
cmd = (
"prop2po -t {0}/en.strings {0}/ca.strings "
"--personality strings --duplicates merge -o {1}"
)
cmd = self._add_conversor_setup_to_cmd(
cmd.format(dirName, filename), ConversorID.Strings
)
os.system(cmd)
def _uncompress_files(self):
for zipfile in self.findFiles.find_recursive(self.convert_dir, "*.zip"):
# Some projects have files with passwords that we do not know,
# we pass an 'unknown' password to prevent been prompted for it
cmd = "unzip -p unknown -t {0} > /dev/null ".format(zipfile)
os.system(cmd)
def _convert_tmx_files_to_po(self):
for tmxfile in self.findFiles.find_recursive(self.convert_dir, "*.tmx"):
fileName, fileExtension = os.path.splitext(tmxfile)
tmx = ConvertTmx(tmxfile, fileName + ".po")
tmx.convert()
logging.info("convert tmx file: {0}".format(tmxfile))
def _convert_csv_files_to_po(self):
for csvfile in self.findFiles.find_recursive(self.convert_dir, "ca.csv"):
dirName = os.path.dirname(csvfile)
pofile = dirName + "/ca.po"
cmd = "csv2po -i {0} -o {1}".format(csvfile, pofile)
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Csv)
os.system(cmd)
logging.info("convert csv file: {0}".format(csvfile))
def _convert_properties_files_to_po(self):
en_file = "en.properties"
files = self.findFiles.find_recursive(self.convert_dir, "ca.properties")
if len(files) == 0:
files = self.findFiles.find_recursive(self.convert_dir, "ca_ES.properties")
if len(files) == 0:
files = self.findFiles.find_recursive(
self.convert_dir, "dictionary_ca.properties"
)
if len(files) > 0:
en_file = "dictionary.properties"
for propfile in files:
dirName = os.path.dirname(propfile)
prop_filename = os.path.basename(propfile)
logging.info("convert properties file: {0}".format(dirName))
po_filename = "{0}/properties-ca.po".format(dirName)
# Allow process files with duplicated entries
cmd = (
f"prop2po -t {dirName}/{en_file} {dirName}/{prop_filename} "
f"--personality java --duplicates merge -o {po_filename}"
)
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Properties)
os.system(cmd)
def _convert_ini_files_to_po(self):
for inifile in self.findFiles.find_recursive(self.convert_dir, "*.ini"):
dirName = os.path.dirname(inifile)
filename = os.path.basename(inifile)
trg = None
for filename in ["ca.ini", "CA.ini", "ca_ES.ini", "ca-ES.ini"]:
fullName = "{0}/{1}".format(dirName, filename)
if filename in inifile:
trg = fullName
break
src = None
for filename in ["en.ini", "EN.ini", "en_GB.ini", "en-US.ini"]:
fullName = "{0}/{1}".format(dirName, filename)
if os.path.isfile(fullName):
src = fullName
break
if src is None or trg is None:
continue
logging.info("convert ini file: {0}".format(inifile))
filename = "{0}/strings-ca.po".format(dirName)
ConvertIni(src, trg, filename).convert()
def _convert_php_resources_files_to_po(self):
if len(self.findFiles.find_recursive(self.convert_dir, "*.php")) == 0:
return
logging.info("convert php directory: {0}".format(self.convert_dir))
# Name arbitrary choosen (not sepecific to an expected dir structure)
OUT_DIRNAME = "po-files"
cmd = "cd {0} && php2po -t en -i ca " "-o {1}".format(
self.convert_dir, OUT_DIRNAME
)
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Php)
os.system(cmd)
def _convert_android_file(self, src_file, tgt_file, dir):
output_file = os.path.join(dir, "ca.po")
cmd = f"android2po -t {src_file} -i {tgt_file} -o {output_file}"
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Android)
os.system(cmd)
# Some times Android resources are found in values/ and values-ca/ subdirectories
# Other times, just in the same subdirectory (en.xml and ca.xml)
def _convert_android_resources_files_to_po(self):
filenames = self.findFiles.find_recursive(self.convert_dir, "*.xml")
if len(filenames) == 0:
return
dirs = set()
subdirs = set()
for filename in filenames:
dir = os.path.dirname(filename)
if dir in dirs:
continue
dirs.add(dir)
src = os.path.join(dir, "en.xml")
tgt = os.path.join(dir, "ca.xml")
if os.path.exists(src) and os.path.exists(tgt):
self._convert_android_file(src, tgt, dir)
if dir == self.convert_dir:
continue
# Remove subdir where file was found a/b/c/strings.xml becomes a/b/values/strings.xml
dir = os.path.dirname(dir)
if len(dir) > 0:
if dir in subdirs:
continue
subdirs.add(dir)
for src, tgt in zip(
["values/strings.xml", "values/strings.xml"],
["values-ca/strings.xml", "values-ca-rES/strings.xml"],
):
src = os.path.join(dir, src)
tgt = os.path.join(dir, tgt)
if os.path.exists(src) and os.path.exists(tgt):
self._convert_android_file(src, tgt, dir)
logging.info("convert Android directory: {0}".format(self.convert_dir))
def _convert_json_file_to_po(self, jsonfile, source, target):
dirName = os.path.dirname(jsonfile)
source = os.path.join(dirName, source)
target = os.path.join(dirName, target)
filename = os.path.join(dirName, "json-ca.po")
if (
not os.path.exists(source)
or not os.path.exists(target)
or os.path.exists(filename)
):
return
logging.info("convert json file: {0}".format(dirName))
cmd = f"json2po -t {source} -i {target} -o {filename}"
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Json)
os.system(cmd)
def _convert_json_files_to_po(self):
for jsonfile in self.findFiles.find_recursive(
self.convert_dir, "messages.json"
):
if "/ca/" not in jsonfile:
continue
# Used for Privacy Badger
self._convert_json_file_to_po(
jsonfile, "../en_US/messages.json", "../ca/messages.json"
)
# Used for Bitwarden
self._convert_json_file_to_po(
jsonfile, "../en/messages.json", "../ca/messages.json"
)
files = [
("ca.json", "en.json", "ca.json"),
("ca_ES.json", "en.json", "ca_ES.json"),
("ca.json", "en-US.json", "ca.json"),
("ca.i18n.json", "en.i18n.json", "ca.i18n.json"),
("main-ca.json", "main.json", "main-ca.json"),
("strings_ca.i18n.json", "strings.i18n.json", "strings_ca.i18n.json"),
("strings_ca.json", "strings.json", "strings_ca.json"),
]
for spec, src, tgt in files:
for jsonfile in self.findFiles.find_recursive(self.convert_dir, spec):
self._convert_json_file_to_po(jsonfile, src, tgt)
def _convert_yml_files_to_po(self):
EXPECTED_SRC = "en.yml"
EXPECTED_TRG = "ca.yml"
for trgfile in self.findFiles.find_recursive(self.convert_dir, "*ca.yml"):
srcfile = trgfile.replace("ca.yml", "en.yml")
if os.path.isfile(srcfile) is False:
continue
dirName = os.path.dirname(srcfile)
src_base = os.path.basename(srcfile)
if src_base != EXPECTED_SRC:
new = os.path.join(dirName, EXPECTED_SRC)
shutil.copyfile(srcfile, new)
trg_base = os.path.basename(trgfile)
if trg_base != EXPECTED_TRG:
new = os.path.join(dirName, EXPECTED_TRG)
shutil.copyfile(trgfile, new)
logging.info("convert yml file: {0}".format(dirName))
cmd = "i18n-translate convert --locale_dir {0} -f yml -l ca -t po -d en".format(
dirName
)
cmd = self._add_conversor_setup_to_cmd(cmd)
os.system(cmd)
def _convert_xliff_file_to_po(self):
for xlfile in self.findFiles.find_recursive(self.convert_dir, "*.xliff"):
fileName, fileExtension = os.path.splitext(xlfile)
pofile = xlfile.replace(".xliff", ".po")
cmd = f'xliff2po -i "{xlfile}" -o "{pofile}" --duplicates=merge'
cmd = self._add_conversor_setup_to_cmd(cmd)
os.system(cmd)
def _convert_apple_file(self, src_file, tgt_file, directory):
output_file = os.path.join(directory, "ca-apple.po")
cmd = f"prop2po -t {src_file} -i {tgt_file} -o {output_file} --personality strings --duplicates merge"
cmd = self._add_conversor_setup_to_cmd(cmd, ConversorID.Apple)
if "--encoding" not in cmd:
cmd += " --encoding utf-8"
os.system(cmd)
def _convert_apple_resources_files_to_po(self):
filenames = self.findFiles.find_recursive(
self.convert_dir, "Localizable.strings"
)
if len(filenames) == 0:
return
dirs = set()
subdirs = set()
for filename in filenames:
dir = os.path.dirname(filename)
if dir in dirs:
continue
dirs.add(dir)
if dir == self.convert_dir:
continue
# Remove subdir where file was found a/b/c/Localizable.strings becomes en.lproj/Localizable.strings
dir = os.path.dirname(dir)
if len(dir) > 0 and dir not in subdirs:
subdirs.add(dir)
src = os.path.join(dir, "en.lproj/Localizable.strings")
tgt = os.path.join(dir, "ca.lproj/Localizable.strings")
if os.path.exists(src) and os.path.exists(tgt):
self._convert_apple_file(src, tgt, dir)
else:
src = os.path.join(dir, "Base.lproj/Localizable.strings")
tgt = os.path.join(dir, "ca.lproj/Localizable.strings")
if os.path.exists(src) and os.path.exists(tgt):
self._convert_apple_file(src, tgt, dir)
logging.info("convert Apple directory: {0}".format(self.convert_dir))