-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauto-gtrans.py
87 lines (66 loc) · 2.32 KB
/
auto-gtrans.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
"""Auto google translator for Factorio locale files
Requires python!=3.13, googletrans=3.1.0a0, and regex module"""
import os
import googletrans as gt
import regex as re
import argparse
LFNAME = "Mod-Locale-1.cfg"
def main(args):
pattern = r"(?P<prefix>[a-zA-Z0-9\-]+=)(?P<text>.+)"
r = re.compile(pattern)
# Open translator
trans = gt.Translator()
# Make sure new locale files exist and will be overwritten
lfs = {}
for lc in args.locale:
try:
os.mkdir(os.path.join("locale", lc))
except FileExistsError:
pass
lfs[lc] = open(os.path.join("locale", lc, LFNAME), "w+", encoding="utf8")
# Get raw data
with open(args.input, "r") as rf:
# Process each line
for line in rf:
results = r.match(line)
if results is None:
for wf in lfs.values():
wf.write(line + "\n")
continue
print(results.group("text"))
for lc, wf in lfs.items():
out = trans.translate(results.group("text"), dest=lc)
wf.write(results.group("prefix") + out.text + "\n")
if __name__ == "__main__":
import argparse
def dir_path(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
def file_path(path):
if os.path.isfile(path):
return path
else:
raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
def test_locales(locale):
if not gt.LANGUAGES.get(locale):
raise argparse.ArgumentTypeError(
f"locale: {locale} is not a valid locale code"
)
if locale == "en":
raise argparse.ArgumentTypeError(
f"locale: {locale} is not a valid locale code for this script"
)
return locale
parser = argparse.ArgumentParser(description="Auto translate locale file")
parser.add_argument(
"-i",
"--input",
type=file_path,
default=os.path.join("locale", "en", LFNAME),
help="Input locale file to parse",
)
parser.add_argument("locale", type=test_locales, nargs="+", help="locales to parse")
args = parser.parse_args()
main(args)