-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslations_codegen.py
81 lines (68 loc) · 2.73 KB
/
translations_codegen.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
"""Static code analysis to generate translations.py from blender/addon.py
- Strings are marked with T() function.
- Non-translated strings are pushed to the top of the list, so that they can be easily identified and translated.
"""
from translations import translations_tuple
from collections import defaultdict
import os, ast
SOURCES = ["blender/addon.py"]
LANGS = ["zh_HANS"]
def parse_ast(filename):
with open(filename, "r", encoding="utf-8") as f:
src = ast.parse(f.read(), filename=filename)
for pa in ast.walk(src):
for node in ast.iter_child_nodes(pa):
if isinstance(node, ast.Call) and getattr(node.func, "id", None) == "T":
yield f"scripts/addons/sssekai_blender_io/{filename}:{pa.lineno}", node.args[
0
].value
translations = defaultdict(lambda: defaultdict(list))
for (
(msgctxt, msgid),
(sources, gen_comments),
(lang, translation, (is_fuzzy, comments)),
) in translations_tuple:
translations[msgid][lang].append((msgctxt, sources, translation))
for source in SOURCES:
for sources, text in parse_ast(source):
for lang in LANGS:
has_lang = lang in translations[text]
has_tl = (
any((elem[-1] for elem in translations[text][lang]))
if has_lang
else False
)
if not has_lang or not has_tl:
print(f'* Missing <{lang}> "{text}"')
if not has_lang:
translations[text][lang].append(("*", ((sources,),), ""))
# ((msgctxt, msgid), (sources, gen_comments), (lang, translation, (is_fuzzy, comments)), ...)
translations_tuple = list()
for msgid, translations_dict in translations.items():
for lang, translations_list in translations_dict.items():
for msgctxt, sources, translation in translations_list:
translations_tuple.append(
((msgctxt, msgid), (sources, ""), (lang, translation, (False, ())))
)
translations_tuple = sorted(translations_tuple, key=lambda x: x[0][1]) # key=msgid
translations_tuple = sorted(
translations_tuple, key=lambda x: bool(x[2][1])
) # key=translation
# Sorted by msgid, and then translation status
with open("translations.py", "w", encoding="utf-8") as f:
from pprint import pformat
f.write(
"""# This file is auto-generated by translations_codegen.py
translations_tuple = """
)
f.write(pformat(translations_tuple))
f.write(
"""
translations_dict = {}
for msg in translations_tuple:
key = msg[0]
for lang, trans, (is_fuzzy, comments) in msg[2:]:
if trans and not is_fuzzy:
translations_dict.setdefault(lang, {})[key] = trans
"""
)