-
Notifications
You must be signed in to change notification settings - Fork 19
/
conv.py
52 lines (45 loc) · 1.58 KB
/
conv.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
import json
import xml.etree.ElementTree as ET
import sys
import os
def translate_file(in_file, out_file):
print(f'translating {in_file}')
strings = ET.parse(in_file)
root_trans = dict()
for strs in strings.getroot():
attr = strs.attrib['name']
text = strs.text.strip();
#cleanup2
if "<" in text:
tmp = text[0 : text.index('<')]
#handle the case where the whole text is surrounded by a tag
if len(tmp) == 0:
tmp = text
while '<' in tmp:
tmp = tmp[tmp.index('>') + 1 : tmp.rindex('<')]
text = tmp
ns = 'global'
key = attr
#escapes <o>
text = text.replace("\\n", "\n")
text = text.replace("\\'", "'")
text = text.replace("\\‘", "‘")
text = text.replace("\\’", "’")
if text.find("\\") >= 0:
print("Bad Escape: " + attr + ":: " + text[text.index("\\"): text.index("\\") + 2])
text = text.strip()
try:
idx = attr.index('_')
ns = attr[0:idx]
key = attr[idx + 1:]
except ValueError:
pass
if not ns in root_trans:
root_trans[ns] = dict()
root_trans[ns][key] = text
with open(out_file, 'w+') as res:
json.dump(root_trans, res, indent=2)
base_dir = sys.argv[1]
translate_file(os.path.join(base_dir, 'app/src/main/res/values/strings.xml'), 'locales/en.json')
translate_file(os.path.join(base_dir, 'app/src/main/res/values-es/strings.xml'), 'locales/es.json')
print("done")