mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
58 lines
2.6 KiB
Python
58 lines
2.6 KiB
Python
"""Writes hookshot_builder/strings.py from builder/HookshotBuilder/Strings.cs (the WPF builder's languages): the same text, the same
|
|
order. Run again whenever Strings.cs changes: python -I builder/py/gen_strings.py"""
|
|
import json
|
|
import os
|
|
import re
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
src = open(os.path.join(HERE, "..", "HookshotBuilder", "Strings.cs"), encoding="utf-8").read()
|
|
enum = re.search(r"enum S\s*\{(.*?)\}", src, re.S).group(1)
|
|
keys = [k.strip() for k in re.sub(r"//.*", "", enum).replace("\n", " ").split(",") if k.strip()]
|
|
arrays = {}
|
|
for m in re.finditer(r"static readonly string\[\] (\w+) =\s*\{(.*?)\n \};", src, re.S):
|
|
arrays[m.group(1)] = [json.loads('"' + s + '"') for s in re.findall(r'"((?:[^"\\]|\\.)*)"', m.group(2))]
|
|
langs = re.findall(r'new\("([^"]+)", "([^"]+)", (\w+)\)', src)
|
|
|
|
out = ['"""The builder\'s languages, generated by gen_strings.py from builder/HookshotBuilder/Strings.cs: one of the game\'s languages',
|
|
'is its code in language.txt, its own name, and the app\'s text in it. Do not edit by hand."""',
|
|
"", "KEYS = " + repr(keys), "", "TEXT = {"]
|
|
for name, arr in arrays.items():
|
|
out.append(" %r: %s," % (name, json.dumps(arr, ensure_ascii=False)))
|
|
out += ["}", "", "LANGUAGES = [ # (code, its own name, the TEXT it uses)"]
|
|
out += [" (%r, %r, %r)," % l for l in langs]
|
|
out += ["]", ""]
|
|
out.append('''
|
|
|
|
def text(lang, key):
|
|
"""A string in the language (a LANGUAGES entry), English where it has none (the update's lines are English only)"""
|
|
i = KEYS.index(key)
|
|
arr = TEXT[lang[2]]
|
|
return arr[i] if i < len(arr) else TEXT["En"][i]
|
|
|
|
|
|
def default_language():
|
|
"""The language closest to Windows' own: the exact one, then the same family, else English (US)"""
|
|
name = ""
|
|
try:
|
|
import ctypes
|
|
buf = ctypes.create_unicode_buffer(85)
|
|
if ctypes.windll.kernel32.GetUserDefaultLocaleName(buf, 85):
|
|
name = buf.value
|
|
except Exception:
|
|
pass
|
|
for lang in LANGUAGES:
|
|
if lang[0].lower() == name.lower():
|
|
return lang
|
|
if name.startswith("zh"):
|
|
pick = "zh-Hant" if any(t in name for t in ("TW", "HK", "Hant")) else "zh-Hans"
|
|
elif name.startswith("es-") and name != "es-ES":
|
|
pick = "es-419"
|
|
elif name.startswith("pt"):
|
|
pick = "pt-BR"
|
|
else:
|
|
pick = name.split("-")[0]
|
|
return next((lang for lang in LANGUAGES if lang[0] == pick), LANGUAGES[0])
|
|
''')
|
|
open(os.path.join(HERE, "hookshot_builder", "strings.py"), "w", encoding="utf-8", newline="\n").write("\n".join(out))
|
|
print(len(keys), "keys;", {k: len(v) for k, v in arrays.items()}, len(langs), "languages")
|