-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
translate.py
127 lines (99 loc) · 3.55 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
#
# AUTHOR: Zappastuff - 2021-APR
#
# REQUIRES:
# - Python 3.9.x
# - deep_translator library (pip3 install deep_translator)
#
import argparse
import os
import re
import sys
from deep_translator import GoogleTranslator
CHARS_MAX = 4500
SEPARATOR = "\x0D"
def parse_command_line():
my_parser = argparse.ArgumentParser(description='Translates Solasta game terms')
my_parser.add_argument('input_folder',
type=str,
help='input folder')
my_parser.add_argument('-c', '--code',
type=str,
required=True,
help='language code')
return my_parser.parse_args()
def display_progress(count, total, status=''):
bar_len = 80
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s (%s) (%i)\r' % (bar, percents, '%', status, count))
sys.stdout.flush()
def unpack_record(record):
term = ""
text = ""
try:
(term, text) = record.split("=", 1)
text = text.strip()
except:
term = record
return term, text if text != "" else "EMPTY"
def get_records(filename):
try:
line_count = 0
line_total = sum(1 for line in open(filename))
with open(filename, "rt", encoding="utf-8") as f:
record = "\n"
while record:
display_progress(line_count, line_total, filename)
record = f.readline()
line_count += 1
if record: yield unpack_record(record)
print()
except FileNotFoundError:
print("ERROR")
def translate_text(text, code):
text = text.replace("\\n", "{99}")
if len(text) > 3 and len(text) <= CHARS_MAX:
translated = GoogleTranslator(source="auto", target=code).translate(text)
else:
translated = text
translated = translated.replace("{99}", "\\n")
return translated
# kiddos: this is ugly ;-)
r2 = re.compile(r"<i> (.*?) </i>")
r3 = re.compile(r"<b> (.*?) </b>")
r4 = re.compile(r"< (.*?)>")
r5 = re.compile(r"<(.*?) >")
def fix_translated_format(text):
text = r2.sub(r"<i>\1</i>", text)
text = r3.sub(r"<b>\1</b>", text)
text = r4.sub(r"<\1>", text)
text = r5.sub(r"<\1>", text)
return text
def translate_file(input_file, output_file, code):
with open(output_file, "wt", encoding="utf-8") as f:
for term, text in get_records(input_file):
translated = translate_text(text, code)
fixed = fix_translated_format(translated)
f.write(f"{term}={fixed}\n")
def translate_folder(root_folder_name, folder_name, code):
root_output_name = code if code != "pt" else "pt-BR"
os.mkdir(folder_name.replace(root_folder_name, root_output_name))
for filename in os.listdir(folder_name):
input_file = os.path.join(folder_name, filename)
if os.path.isfile(input_file):
output_file = f"{root_output_name}\\{input_file[3:-7]}-{root_output_name}.txt"
translate_file(input_file, output_file, code)
else:
translate_folder(root_folder_name, input_file, code)
def main():
# args = parse_command_line()
# translate_folder(args.input_folder, args.input_folder, args.code)
translate_folder("en", "en", "pt")
translate_folder("en", "en", "es")
translate_folder("en", "en", "fr")
translate_folder("en", "en", "de")
translate_folder("en", "en", "it")
if __name__ == "__main__":
main()