-
-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathsync-langs.py
77 lines (66 loc) · 2.53 KB
/
sync-langs.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This script synchronizes TAPi18n string jsons from en into other languages.
# The target language json file (ex. zh-TW.i18n.json) will have the same keys as source language,
# and untranslated strings will be copied with a prefix *_
# You can do this anytime source language is updated, the script will keep translated strings.
from io import open
import json
import sys
from collections import OrderedDict
inter_add = False;
def main():
if len(sys.argv) is 1:
print('Usage: "python sync-langs.py en zh-TW". The first language has higher json order priority. If the first language is en, it can be omitted.')
elif len(sys.argv) is 2:
first = 'en'
second = sys.argv[1]
else:
first = sys.argv[1]
second = sys.argv[2]
f1 = open('%s.i18n.json' % first, 'r')
f2 = open('%s.i18n.json' % second, 'r')
j1 = json.loads(f1.read(), object_pairs_hook=OrderedDict)
j2 = json.loads(f2.read(), object_pairs_hook=OrderedDict)
f1.close()
f2.close()
r2raw = comp_dict(j1, j2)
r1 = comp_dict(j2, j1)
r2 = align_dict(j1, r2raw)
with open('%s.i18n.json' % second,'w',encoding='utf-8') as out:
out.write(json.dumps(r2, indent=2, ensure_ascii=False, separators=(',', ': ')))
if inter_add:
with open('%s.i18n.json' % first,'w',encoding='utf-8') as out:
out.write(json.dumps(r1, indent=2, ensure_ascii=False, separators=(',', ': ')))
print('Done.')
def comp_dict(base, comp):
if inter_add:
result = OrderedDict(comp)
else:
result = OrderedDict()
for key, value in base.items():
if key in comp:
if type(comp[key]) != type(value):
print('Type mismatch!! Key=%s; %s %s' % (key, type(comp[key]), type(value)))
if type(value) is OrderedDict:
result[key] = comp_dict(value, comp[key])
else:
result[key] = comp[key]
else:
if type(value) is OrderedDict:
result[key] = comp_dict(value, OrderedDict())
else:
if sys.version_info[0] == 2:
result[key] = "*_%s" % unicode(value)
else:
result[key] = "*_%s" % str(value)
return result
def align_dict(base, target):
result = OrderedDict()
for key in base:
if type(base[key]) is OrderedDict:
result[key] = align_dict(base[key], target[key])
else:
result[key] = target[key]
return result
main()