forked from meerk40t/meerk40t
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_check.py
278 lines (261 loc) · 11.3 KB
/
translate_check.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
This module scans the complete source tree and looks for candidates
to be translated and compares this with the existing translation of
a given locale. It creates then a delta_{locale}.po file for review
and integration into the translation file.
poboy does something similar (and even better when recognising
strings), but that comes at a cost, as it will at the same time
discard translated but non-used msgid/msgstr pairs. This is not
always the intended behaviour.
You are supposed to call this tool from the command line
specifying the locale as a parameter, e.g.:
python ./translate_check.py zh
"""
import os
import sys
# def testroutine():
# _ = print
# msg = _("Test for a '") + "-" + _('another test for "')
# msg = _(
# "part 1" +
# "part2"
# + "part3"
# )
def read_source():
id_strings_source = []
id_usage = []
# sourcedir = "./meerk40t"
sourcedir = "./"
linecount = 0
filecount = 0
# debugit = False
ignoredirs = [".git", ".github", "venv", ".venv"]
for root, dirs, files in os.walk(sourcedir):
mayignore = False
for s in ignoredirs:
if root.startswith(s) or root.startswith("./" + s):
mayignore = True
break
if mayignore:
continue
for filename in files:
fname = os.path.join(root, filename)
if not fname.endswith(".py"):
continue
# debugit = fname.endswith("translate_check.py")
with open(fname, mode="r", encoding="utf-8", errors="surrogateescape") as f:
pfname = os.path.normpath(fname).replace("\\", "/")
filecount += 1
localline = 0
msgid_mode = False
msgid = ""
while True:
linecount += 1
localline += 1
line = f.readline()
if not line:
break
while line:
line = line.strip()
# if debugit:
# print (f"[{msgid_mode}, '{msgid}']: '{line}'")
if not line:
break
if msgid_mode:
if line.startswith(")"):
if msgid:
#
idx = 0
while True:
idx = msgid.find('"', idx)
if idx == 0:
# Starts with a '"'
msgid = "\\" + msgid
idx += 1
elif idx > 0:
if msgid[idx-1] != "\\":
msgid = msgid[:idx] + "\\" + msgid[idx:]
idx += 1
else:
break
idx += 1
if msgid not in id_strings_source:
id_strings_source.append(msgid)
id_usage.append(f"#: {pfname}:{localline}")
else:
found_index = id_strings_source.index(msgid)
id_usage[found_index] += f" {pfname}:{localline}"
# print (f"'{orgline}' -> '{msgid}'")
msgid_mode = False
msgid = ""
idx = 0
if idx + 1 >= len(line):
line = ""
else:
line = line[idx + 1 :]
continue
elif line.startswith("+"):
idx = 0
if idx + 1 >= len(line):
line = ""
else:
line = line[idx + 1 :]
continue
elif line.startswith("'"):
quote = "'"
startidx = 1
while True:
idx = line.find(quote, startidx)
if idx < 0:
# strange
msgid_mode = False
line = ""
break
if line[idx - 1] == "\\": # escape character
startidx = idx + 1
else:
# All good
break
msgid += line[1:idx]
if idx + 1 >= len(line):
line = ""
else:
line = line[idx + 1 :]
continue
elif line.startswith('"'):
quote = '"'
startidx = 1
while True:
idx = line.find(quote, startidx)
if idx < 0:
# strange
msgid_mode = False
line = ""
break
if line[idx - 1] == "\\": # escape character
startidx = idx + 1
else:
# All good
break
msgid += line[1:idx]
if idx + 1 >= len(line):
line = ""
else:
line = line[idx + 1 :]
continue
else:
# strange
msgid_mode = False
line = ""
break
else:
# Fine so we need to look for '_('
idx = line.find("_(")
if idx >= 0:
msgid_mode = True
msgid = ""
line = line[idx + 2 :]
else:
# Nothing to be done here in this line
line = ""
break
# for dirname in dirs:
# dname = os.path.join(root, dirname))
print(
f"Read {filecount} files with {linecount} lines and found {len(id_strings_source)} entries..."
)
return id_strings_source, id_usage
def read_po(locale):
id_strings = []
localedir = "./locale"
po_dir = localedir + "/" + locale + "/LC_MESSAGES/"
po_files = [f for f in next(os.walk(po_dir))[2] if os.path.splitext(f)[1] == ".po"]
linecount = 0
for po_file in po_files:
fname = po_dir + po_file
with open(fname, "r", encoding="utf-8", errors="surrogateescape") as f:
msgid_mode = False
id_str = ""
while True:
linecount += 1
line = f.readline()
if not line:
break
line = line.strip()
if line.startswith("msgid"):
msgid_mode = True
id_str = ""
idx = line.find('"')
if idx >= 0:
candidate = line[idx + 1 :]
try:
idx = -1
while candidate[idx] != '"':
idx -= 1
candidate = candidate[:idx]
id_str += candidate
except IndexError:
print(
f"Stumbled across: '{line}', candidate:'{candidate}', idx={idx}"
)
# print (f"start '{line}' -> '{candidate}'")
elif line.startswith('"'):
if msgid_mode:
candidate = line[1:]
try:
idx = -1
while candidate[idx] != '"':
idx -= 1
candidate = candidate[:idx]
# print (f"add '{line}' -> '{candidate}'")
id_str += candidate
except IndexError:
print(
f"Stumbled across: '{line}', candidate:'{candidate}', idx={idx}"
)
elif line.startswith("msgstr"):
msgid_mode = False
if id_str and id_str not in id_strings:
id_strings.append(id_str)
id_str = ""
else:
pass
if id_str and msgid_mode and id_str not in id_strings:
id_strings.append(id_str)
elif id_str and msgid_mode:
print (f"Duplicte entry found for {locale}: ´{id_str}´")
print(f"Read {linecount} lines for {locale} and found {len(id_strings)} entries...")
return id_strings
def compare(locale, id_strings, id_strings_source, id_usage):
counts = [0, 0, 0]
with open(f"./delta_{locale}.po", "w", encoding="utf-8") as outp:
for idx, key in enumerate(id_strings_source):
counts[0] += 1
if key in id_strings:
counts[1] += 1
else:
counts[2] += 1
outp.write(f"{id_usage[idx]}\n")
outp.write(f'msgid "{key}"\n')
outp.write('msgstr ""\n\n')
print(
f"Done for {locale}: examined={counts[0]}, found={counts[1]}, new={counts[2]}"
)
def main():
args = sys.argv[1:]
locale = [
"de",
]
if len(args) > 0:
locale = args
if locale[0].lower() == "all":
locale = ["de", "es", "fr", "hu", "it", "ja", "nl", "pt_BR", "pt_PT", "zh"]
print("Usage: python ./translate_check.py <locale>")
print("<locale> one of de, es, fr, hu, it, ja, nl, pt_BR, pt_PT, zh")
print("Reading sources...")
id_strings_source, id_usage = read_source()
for loc in locale:
print(f"Checking translation strings for locale {loc}...")
id_strings = read_po(loc)
compare(loc, id_strings, id_strings_source, id_usage)
main()