95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
|
|
"""Prüft die Sprachdateien und den Code auf Übersetzungsfehler.
|
||
|
|
|
||
|
|
py -3.12 tools/check_translations.py
|
||
|
|
|
||
|
|
- doppelte Keys in einer Sprachdatei (der letzte gewinnt beim Laden)
|
||
|
|
- Keys, die in de/es fehlen oder nur dort existieren
|
||
|
|
- im Code per L.t("...") benutzte Keys, die in en.json fehlen
|
||
|
|
- fest eingebaute deutsche Texte im Code (Heuristik: Umlaute/typische Wörter
|
||
|
|
in einem String-Literal, das nicht über L.t() läuft)
|
||
|
|
|
||
|
|
Exit-Code 1, wenn etwas gefunden wurde.
|
||
|
|
"""
|
||
|
|
import collections
|
||
|
|
import glob
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
import sys
|
||
|
|
|
||
|
|
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
||
|
|
LANGS = ["en", "de", "es"]
|
||
|
|
|
||
|
|
# Dateien ohne Prüfung auf fest eingebaute Texte (mit Grund):
|
||
|
|
IGNORE_FILES = {
|
||
|
|
}
|
||
|
|
# Einzelne Zeilen mit "// i18n-ok" am Ende gelten als bewusst (z. B. Sprachnamen).
|
||
|
|
STRING = re.compile(r'"((?:[^"\\]|\\.)*)"')
|
||
|
|
GERMAN = re.compile(r"[äöüÄÖÜß]|\b(der|die|das|und|ist|nicht|mit|dein|deine|Jahr|Leben|Spiel)\b")
|
||
|
|
|
||
|
|
|
||
|
|
def load_pairs(lang):
|
||
|
|
path = os.path.join(ROOT, "Languages", f"{lang}.json")
|
||
|
|
with open(path, encoding="utf-8-sig") as f:
|
||
|
|
return json.load(f, object_pairs_hook=lambda p: p)
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
problems = 0
|
||
|
|
keys = {}
|
||
|
|
for lang in LANGS:
|
||
|
|
pairs = load_pairs(lang)
|
||
|
|
names = [k for k, _ in pairs]
|
||
|
|
dup = sorted(k for k, c in collections.Counter(names).items() if c > 1)
|
||
|
|
keys[lang] = set(names)
|
||
|
|
if dup:
|
||
|
|
problems += len(dup)
|
||
|
|
print(f"[{lang}] doppelt: {', '.join(dup)}")
|
||
|
|
|
||
|
|
for lang in LANGS[1:]:
|
||
|
|
missing = sorted(keys["en"] - keys[lang])
|
||
|
|
extra = sorted(keys[lang] - keys["en"])
|
||
|
|
if missing:
|
||
|
|
problems += len(missing)
|
||
|
|
print(f"[{lang}] fehlt: {', '.join(missing)}")
|
||
|
|
if extra:
|
||
|
|
problems += len(extra)
|
||
|
|
print(f"[{lang}] nicht in en: {', '.join(extra)}")
|
||
|
|
|
||
|
|
used = set()
|
||
|
|
hardcoded = []
|
||
|
|
# Quelltexte: Game/, Core/, Mono/; Build-, Tool- und Archiv-Ordner auslassen
|
||
|
|
skip = ("bin", "obj", "publish", "Spritegenerator", "_Archiv")
|
||
|
|
for path in sorted(p for p in glob.glob(os.path.join(ROOT, "**", "*.cs"), recursive=True)
|
||
|
|
if not any(os.sep + s + os.sep in p for s in skip)):
|
||
|
|
name = os.path.basename(path)
|
||
|
|
with open(path, encoding="utf-8") as f:
|
||
|
|
for n, line in enumerate(f, 1):
|
||
|
|
used.update(re.findall(r'L\.t\("([^"{}]+)"\)', line))
|
||
|
|
code = line.strip()
|
||
|
|
if code.startswith("//") or name in IGNORE_FILES or "i18n-ok" in code:
|
||
|
|
continue
|
||
|
|
code = code.split(" //")[0]
|
||
|
|
for m in STRING.finditer(code):
|
||
|
|
text = m.group(1)
|
||
|
|
before = code[max(0, m.start() - 4):m.start()]
|
||
|
|
if "L.t(" in before or "t(" == before[-2:]:
|
||
|
|
continue
|
||
|
|
if re.search(r"[A-Za-zÄÖÜäöüß]{3,}", text) and GERMAN.search(text):
|
||
|
|
hardcoded.append(f"{name}:{n}: {text[:80]}")
|
||
|
|
|
||
|
|
missing_en = sorted(used - keys["en"])
|
||
|
|
if missing_en:
|
||
|
|
problems += len(missing_en)
|
||
|
|
print(f"[code] Key fehlt in en.json: {', '.join(missing_en)}")
|
||
|
|
for h in hardcoded:
|
||
|
|
print(f"[code] fest eingebaut? {h}")
|
||
|
|
problems += len(hardcoded)
|
||
|
|
|
||
|
|
print(f"{problems} Auffälligkeiten" if problems else "alles in Ordnung")
|
||
|
|
return 1 if problems else 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|