-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.py
executable file
·417 lines (378 loc) · 13.4 KB
/
translate.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import shutil
from collections import defaultdict
from functools import partial
from multiprocessing.pool import ThreadPool
from os.path import join
from translatepy.translators.yandex import YandexTranslate
from common import (
Font,
combine_desc_and_note,
iterate_over_dict,
collapse,
except_gab_text,
fix_name,
FMT_REGEX,
ASCII_REGEX,
replace_escapes,
translate_category,
)
from settings import TRANSLATE_CACHE_FILENAME
class GameTranslator:
def __init__(self, src_game_dir: str, dst_game_dir: str, line_limit: int):
self.src_game_dir = src_game_dir
self.dst_game_dir = dst_game_dir
self.to_path = join(dst_game_dir, 'www/data')
self.translate_map = {}
self.translate_map_counter = defaultdict(int)
self.translator = YandexTranslate()
self.line_limit = line_limit
self.bad_formatting = {}
self.bad_translate = {}
self.font = Font(
join(src_game_dir, 'www/fonts/Garamond-Premier-Pro_19595.ttf'))
self.overspaces = {}
def run(self):
filenames = self.fetch_dir()
filenames = self.sort_files(filenames)
for n, filename in enumerate(filenames, start=1):
print(f'{n}/{len(filenames)} {filename} .. ')
self.process_single_file(filename)
def fetch_dir(self) -> list[str]:
return [
filename
for filename in os.listdir(self.to_path)
if os.path.splitext(filename)[1].lower() == '.json'
]
@staticmethod
def sort_files(filenames: list[str]) -> list[str]:
up = []
middle = []
down = []
for fn in filenames:
if fn.startswith('Map'):
down.append(fn)
elif fn.startswith('CommonEvents'):
middle.append(fn)
else:
up.append(fn)
up.sort()
middle.sort()
down.sort()
return up + middle + down
def process_single_file(self, filename: str):
from_path = os.path.join(self.to_path, filename)
data = json.loads(open(from_path).read())
with ThreadPool(10) as pool:
pool.map(
partial(self.task, filename),
collapse(iterate_over_dict(data)),
)
for obj in iterate_over_dict(data):
self.wrap_lines(obj)
self.save_single_file(filename, data)
def wrap_lines(self, obj: dict):
items = obj.get('list')
if not items:
return
new_items = []
for item in items:
if (
item.get('code') == 401 and
item['parameters'] and
item['parameters'][0]
):
parts = self.font.split_text(
item['parameters'][0],
self.line_limit,
4,
)
for part in parts:
new_item = item.copy()
new_item['parameters'] = [part]
new_items.append(new_item)
else:
new_items.append(item)
obj['list'] = new_items
def save_single_file(self, filename: str, data: dict):
to_path = os.path.join(self.to_path, filename)
with open(to_path, 'w') as f:
f.write(json.dumps(data, ensure_ascii=False))
def task(self, filename: str, obj: dict):
match filename:
case (
'Items.json' |
'Actors.json' |
'Weapons.json' |
'Enemies.json' |
'Armors.json' |
'Skills.json'
):
if 'name' in obj:
obj['name'] = self.translate(obj['name'])
if 'description' in obj and obj['description'].strip():
combine_desc_and_note(obj)
obj['description'] = self.split_and_translate_text(
obj['description'],
3,
)
translate_category(obj)
case 'Classes.json':
if 'name' in obj:
obj['name'] = self.translate(obj['name'])
if 'description' in obj:
obj['description'] = self.split_and_translate_text(
obj['description'],
3,
)
if 'note' in obj:
obj['note'] = self.mark_translate(obj['note'])
case 'System.json':
if 'gameTitle' in obj:
obj['gameTitle'] = self.mark_translate(obj['gameTitle'])
if 'equipTypes' in obj:
obj['equipTypes'] = [self.mark_translate(item) for item in obj['equipTypes']]
if 'skillTypes' in obj:
obj['skillTypes'] = [self.mark_translate(item) for item in obj['skillTypes']]
if 'terms' in obj:
if 'params' in obj['terms']:
obj['terms']['params'] = [
self.mark_translate(item)
for item in obj['terms']['params']
]
if 'messages' in obj['terms']:
obj['terms']['messages'] = {
k: self.mark_translate(v)
for k, v in obj['terms']['messages'].items()
}
if 'basic' in obj['terms']:
obj['terms']['basic'] = [
self.mark_translate(item)
for item in obj['terms']['basic']
]
if 'commands' in obj['terms']:
obj['terms']['commands'] = [
self.mark_translate(item)
for item in obj['terms']['commands']
]
if 'displayName' in obj:
obj['displayName'] = self.translate(obj['displayName'])
match obj.get('code'):
case 102:
obj['parameters'][0] = [
self.translate(parameter)
for parameter in obj['parameters'][0]
]
case 356:
obj['parameters'][0] = except_gab_text(self.split_and_translate_text)(
obj['parameters'][0],
1,
)
case 401:
assert len(obj['parameters']) == 1
obj['parameters'] = [
self.split_and_translate_text(obj['parameters'][0], 4)
]
case 320:
assert len(obj['parameters']) == 2
obj['parameters'][1] = self.translate(obj['parameters'][1])
case 324 | 402:
obj['parameters'][1] = self.split_and_translate_text(
obj['parameters'][1],
1,
)
def translate(self, text: str) -> str:
if not isinstance(text, str) or not text.strip():
return text
orig_text = text
self.translate_map_counter[orig_text] += 1
if orig_text in self.translate_map:
translated = self.translate_map[orig_text]
else:
text = fix_name(text)
translated = replace_escapes(self.call_translator)(text)
self.translate_map[orig_text] = translated
self.check_bad_translate(orig_text, translated)
self.check_format_after_translate(orig_text, translated)
if orig_text != translated:
logging.info('%s > %s', orig_text, translated)
return translated
def mark_translate(self, text) -> str:
if not isinstance(text, str) or not text.strip():
return text
self.translate_map_counter[text] += 1
if text in self.translate_map:
return self.translate_map[text]
self.translate_map[text] = text
return text
def call_translator(self, text: str) -> str:
return self.translator.translate(
text,
source_language='en',
destination_language='ru',
).result
def split_and_translate_text(
self,
text: str,
count_lines: int,
) -> str:
translated = self.translate(text)
parts = self.font.split_text(translated, self.line_limit, count_lines)
result = '\n'.join(parts)
if len(parts) > count_lines:
self.overspaces[text] = count_lines, len(parts), result
return result
def check_format_after_translate(self, text: str, translated: str):
a = FMT_REGEX.findall(text)
b = FMT_REGEX.findall(translated)
if a != b:
self.bad_formatting[text] = translated
def check_bad_translate(self, text: str, translated: str):
orig_translated = translated
for fmt in reversed(list(FMT_REGEX.finditer(translated))):
start, stop = fmt.span(1)
translated = translated[:start] + translated[stop:]
m = ASCII_REGEX.search(translated)
if m:
self.bad_translate[text] = orig_translated
def copy_to_game_dir(self):
if self.src_game_dir != self.dst_game_dir:
shutil.copytree(
self.src_game_dir, self.dst_game_dir, dirs_exist_ok=True)
def load_translate_cache(self):
if os.path.exists(TRANSLATE_CACHE_FILENAME):
print('load translate cache from', TRANSLATE_CACHE_FILENAME)
with open(TRANSLATE_CACHE_FILENAME, 'r') as f:
self.translate_map = json.load(f)
def resort_translate_cache(self):
new_map = {}
for k in self.translate_map_counter.keys():
new_map[k] = self.translate_map[k]
self.translate_map = new_map
def save_translate_cache(self):
print('save translate cache to', TRANSLATE_CACHE_FILENAME)
with open(TRANSLATE_CACHE_FILENAME, 'w') as f:
json.dump(self.translate_map, f, ensure_ascii=False, indent=2)
def clean_bad_cache(self):
was_deleted = {}
for key in set(self.translate_map) - set(self.translate_map_counter):
was_deleted[key] = self.translate_map[key]
del self.translate_map[key]
if was_deleted:
print('bad cache:')
for k, v in was_deleted.items():
print(repr(k), '>', repr(v))
def print_bad_format(self):
if not self.bad_formatting:
return
print('=== Bad formatting strings:')
for text, translated in self.bad_formatting.items():
print(
json.dumps(text, ensure_ascii=False),
'>',
json.dumps(translated, ensure_ascii=False),
)
print()
print()
def print_overspaces(self):
if not self.overspaces:
return
print()
print('=== Over space strings ===')
print()
for text, (need_ln, ln, translated) in self.overspaces.items():
print(
f'[{need_ln}]',
json.dumps(text, ensure_ascii=False),
)
print(
f'[{ln}]',
json.dumps(translated, ensure_ascii=False),
)
print()
print()
print()
def print_bad_translate(self):
if not self.bad_translate:
return
print('=== Bad translate strings:')
for text, translated in self.bad_translate.items():
print(
json.dumps(text, ensure_ascii=False),
'>',
json.dumps(translated, ensure_ascii=False),
)
print()
print()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--game-dir',
help='location of game directory',
required=True,
)
parser.add_argument(
'--line-limit',
help='the number of characters in the line after which'
' the sentence will be wrapped to a new line',
type=int,
required=True,
)
parser.add_argument(
'--resort-cache',
action='store_true',
)
parser.add_argument(
'--print-bad-format',
action='store_true',
)
parser.add_argument(
'--print-overspaces',
action='store_true',
)
parser.add_argument(
'--print-bad-translate',
action='store_true',
)
parser.add_argument(
'--log',
action='store_true',
)
args = parser.parse_args()
try:
if args.log:
logging.basicConfig(
filename='log.txt',
format='%(message)s',
level=logging.ERROR,
)
logging.info('start')
app = GameTranslator(
'src_game',
args.game_dir,
args.line_limit,
)
app.copy_to_game_dir()
app.load_translate_cache()
except KeyboardInterrupt:
pass
else:
try:
app.run()
except KeyboardInterrupt:
pass
else:
app.clean_bad_cache()
if args.resort_cache:
app.resort_translate_cache()
app.save_translate_cache()
if args.print_bad_format:
app.print_bad_format()
if args.print_overspaces:
app.print_overspaces()
if args.print_bad_translate:
app.print_bad_translate()