-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslate.py
More file actions
81 lines (69 loc) · 2.71 KB
/
translate.py
File metadata and controls
81 lines (69 loc) · 2.71 KB
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
#!/usr/bin/env python3
"""
MDM translation generator.
Reads strings from translations/translation_en.xml and produces
one translation_XX.xml per FS25 language via Google Translate.
"""
import os, sys, xml.etree.ElementTree as ET
from pathlib import Path
try:
from deep_translator import GoogleTranslator
except ImportError:
print("Installing deep-translator...")
os.system(f"{sys.executable} -m pip install deep-translator")
from deep_translator import GoogleTranslator
LANGUAGES = {
"de": "de", "fr": "fr", "fc": "fr", "es": "es", "ea": "es",
"it": "it", "pt": "pt", "br": "pt", "pl": "pl", "cz": "cs",
"ru": "ru", "uk": "uk", "nl": "nl", "hu": "hu", "tr": "tr",
"jp": "ja", "kr": "ko", "da": "da", "id": "id", "no": "no",
"ro": "ro", "sv": "sv", "vi": "vi", "fi": "fi", "ct": "zh-TW",
}
MOD_ROOT = Path(__file__).parent
TRANS_DIR = MOD_ROOT / "translations"
EN_FILE = TRANS_DIR / "translation_en.xml"
if not EN_FILE.exists():
print(f"ERROR: {EN_FILE} not found"); sys.exit(1)
# Parse English source
tree = ET.parse(EN_FILE)
strings = {} # key -> english text
for t in tree.getroot().findall(".//text"):
name = t.get("name", "")
text = t.get("text", "")
if name and text:
strings[name] = text
print(f"Found {len(strings)} strings in translation_en.xml\n")
def xml_escape(s):
s = s.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
return s
def write_file(lang_code, translated):
path = TRANS_DIR / f"translation_{lang_code}.xml"
lines = ['<?xml version="1.0" encoding="utf-8" standalone="no"?>', "<l10n>", " <texts>", ""]
for key, val in translated.items():
lines.append(f' <text name="{key}" text="{xml_escape(val)}"/>')
lines += ["", " </texts>", "</l10n>"]
path.write_text("\n".join(lines), encoding="utf-8")
def translate_batch(google_lang):
out = {}
translator = GoogleTranslator(source="en", target=google_lang)
items = list(strings.items())
for i, (key, val) in enumerate(items, 1):
try:
result = translator.translate(val)
out[key] = result if result else val
except Exception as e:
print(f" WARNING [{key}]: {e}")
out[key] = val
if i % 10 == 0 or i == len(items):
print(f" {i}/{len(items)}", end="\r")
print()
return out
for fs_code, google_code in LANGUAGES.items():
print(f"[{fs_code.upper()}] {google_code}...")
try:
translated = translate_batch(google_code)
write_file(fs_code, translated)
print(f" Written: translation_{fs_code}.xml")
except Exception as e:
print(f" ERROR: {e}")
print("\nDone.")