-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencc_s2t.py
49 lines (37 loc) · 1.09 KB
/
opencc_s2t.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
import opencc
import json
import sys
converter = opencc.OpenCC('s2t.json')
def convert(text):
assert type(text) == str
return converter.convert(text)
def recursive_convert(value):
if type(value) == str:
return converter.convert(value)
elif type(value) == list:
return [recursive_convert(item) for item in value]
else:
print('Unsupported type:', type(value))
sys.exit(1)
def convert_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
return converter.convert(text)
custom_dict = {
"LANG": "zh_TW",
"LANG_NAME": "繁體中文",
# "KEY_WITH_LIST_VALUE": [
# "v我",
# "50"
# ],
}
if __name__ == '__main__':
with open("multi_language/zh_CN.json", 'r') as f:
lang_cn = json.load(f)
for key in lang_cn:
if key in custom_dict:
lang_cn[key] = custom_dict[key]
else:
lang_cn[key] = recursive_convert(lang_cn[key])
with open("multi_language/zh_TW.json", 'w') as f:
json.dump(lang_cn, f, ensure_ascii=False, indent=4)