-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_strings_po.py
98 lines (80 loc) · 2.57 KB
/
gen_strings_po.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
#!/usr/bin/env python
import shutil
import re
import os
import sys
import polib
from tempfile import mkstemp
from collections import OrderedDict
sys.tracebacklimit = 1
# NOTE: en_gb must be first, others not required
LANGS = ['en_gb', 'ru_ru']
trans = []
for lang in LANGS:
module = __import__('resources.lib.translation.' + lang, fromlist=[lang])
trans.append(getattr(module, lang))
OFFSET = 30000
DIR = os.path.join(os.getcwd(), sys.argv[1])
# en = trans.pop(0) and ordered
en = OrderedDict(((v[0], (i, v[1])) for i, v in enumerate(trans.pop(0))))
for idx, tran in enumerate(trans):
trans[idx] = dict(tran)
# Find all .py files
pyfiles = []
for path, dirs, files in os.walk(DIR):
fnames = filter(lambda f: f.endswith('.py'), files)
fpaths = map(lambda f: os.path.join(path, f), fnames)
pyfiles.extend(list(fpaths))
po_entries = {}
for lang in LANGS:
po_entries[lang] = {}
def key_to_id(key):
pair = en.get(key)
if not pair:
raise Exception("No translation entry for key '%s' in en_gb.py" % key)
idx, msgid = en[key]
num = OFFSET + idx
po_entries['en_gb'][idx] = polib.POEntry(
msgctxt='#%i' % num,
msgid=msgid
)
for i, lang in enumerate(LANGS[1:]):
msgstr = trans[i].get(key)
if not msgstr:
raise Exception("No translation entry for key '%s' in %s" % (key, lang + '.py'))
po_entries[lang][idx] = polib.POEntry(
msgctxt='#%i' % num,
msgid=msgid,
msgstr=msgstr
)
return num
def repl_py(match):
key = match.group(0)[3:-2]
return '_(%i)' % key_to_id(key)
def repl_settings(match):
key = match.group(0)[8:-1]
return ' label="%i"' % key_to_id(key)
def patch_file(path, regex, repl):
fin = open(path, 'r')
dest = mkstemp()[1]
fout = open(dest, 'w')
for line in fin:
out = re.sub(regex, repl, line)
fout.write(out)
fin.close()
fout.close()
shutil.move(dest, path)
for path in pyfiles:
patch_file(path, r'_\((?:\'|").+?(?:\'|")\)', repl_py)
settings_path = os.path.join(DIR, 'resources/settings.xml')
patch_file(settings_path, r'\slabel="(.+?)"', repl_settings)
# Save strings.po for each language
for lang in LANGS:
po = polib.POFile()
po_data = po_entries[lang]
for idx in range(0, len(po_data)):
entry = po_data.get(idx)
if not entry:
raise Exception('Abandoned translation entry in %s at index %i' % (lang + '.py', idx))
po.append(po_data[idx])
po.save(os.path.join(DIR, 'resources/language/resource.language.%s/strings.po' % lang))