SPB Git

Repository Public License

localize.py 8.8 KB 191 lines python

1#!/usr/bin/env python32# ORIX Convert — Scripts/localize.py3# Description: Builds the string catalogs (en + fr-CA).4#   (no argument)  App strings: every L("…") key of App/Sources → App/Resources/Localizable.xcstrings, French from5#                  Scripts/translations-fr.json. Engine strings: Scripts/engine-strings.json → Engines.xcstrings and6#                  EnginePatterns.json, French from Scripts/translations-engines-fr.json. Reports what is missing.7#   --harvest      Refreshes Scripts/engine-strings.json: asks the built app for the strings its engines declare8#                  (-ORIXDumpEngineStrings) and scans the wired packages' sources for warning, error, validation,9#                  inspection and progress messages (interpolations become %@ patterns).10#   --check        Exit 1 when an app string has no French translation; lists untranslated engine strings.11#   --keys         Print the app keys as JSON.12# Author: Simon-Pierre Boucher — contact@spboucher.ai13import json, pathlib, re, subprocess, sys, tempfile1415ROOT = pathlib.Path(__file__).resolve().parent.parent16APP_KEY = re.compile(r'\bL\(\s*"((?:[^"\\]|\\.)*)"')17RES = ROOT / "App/Resources"18INFO_FR = {19    "CFBundleDisplayName": "ORIX Convert",20    "File": "Fichier",21    "Folder": "Dossier",22}2324def unescape(s):25    return s.replace('\\"', '"').replace("\\n", "\n").replace("\\\\", "\\")2627def app_keys():28    found = set()29    for f in sorted((ROOT / "App/Sources").rglob("*.swift")):30        for m in APP_KEY.finditer(f.read_text()):31            found.add(unescape(m.group(1)))32    return sorted(found)3334def placeholders(s):35    return sorted(p[-1] for p in re.findall(r"%(?:\d+\$)?(?:lld|@|d)", s))3637def unit(value, state="translated"):38    return {"stringUnit": {"state": state, "value": value}}3940def catalog(strings):41    return json.dumps({"sourceLanguage": "en", "strings": strings, "version": "1.0"}, ensure_ascii=False, indent=1, sort_keys=True) + "\n"4243# ---------------------------------------------------------------- engine sources4445HEADS = re.compile(r"(ConversionWarning\(|\.warn\(\.init\(|\bwarnings?\.append\(\.init\(|\bConversionError\.\w+\(|throw \.\w+\(|ValidationCheck\(|\bCheck\(|InspectionField\(|\bF\(|message:\s*|notes\.append\(|\.unavailable\(reason:\s*|EngineAvailability\.unavailable\()")4647def read_literal(src, i):48    """src[i] is the opening quote of a single-line Swift string literal. Returns (pattern, end index) where49    interpolations are replaced by %@."""50    out, i = [], i + 151    while i < len(src):52        c = src[i]53        if c == "\\":54            n = src[i + 1]55            if n == "(":56                depth, i = 1, i + 257                while i < len(src) and depth:58                    if src[i] == '"':59                        _, i = read_literal(src, i)60                        continue61                    depth += src[i] == "("62                    depth -= src[i] == ")"63                    i += 164                out.append("%@")65                continue66            out.append({"n": "\n", "t": "\t", '"': '"', "\\": "\\"}.get(n, n))67            i += 268            continue69        if c == '"':70            return "".join(out), i + 171        if c == "\n":72            return None, i73        out.append(c)74        i += 175    return None, i7677def literals_after(src, start):78    """String literals found in the argument list that starts at `start` (until the parenthesis that opened before79    `start` closes, or the end of the statement for `message:`)."""80    found, depth, i = [], 1, start81    while i < len(src) and depth > 0:82        c = src[i]83        if c == '"':84            if src.startswith('"""', i):85                end = src.find('"""', i + 3)86                i = len(src) if end < 0 else end + 387                continue88            before = src[max(0, i - 12):i]89            text, i = read_literal(src, i)90            if text is not None and not re.search(r"\b(code|forKey|named|id):\s*$", before):91                found.append(text)92            continue93        if c == "(": depth += 194        elif c == ")": depth -= 195        elif c == "\n" and depth == 1 and src[start - 1] != "(": break96        i += 197    return found9899def is_sentence(s):100    stripped = s.replace("%@", "").strip()101    return len(stripped) >= 3 and re.search(r"[A-Za-z]{3}", stripped) is not None102103def scan_sources(packages):104    exact, patterns = set(), set()105    for package in packages:106        base = ROOT / "Packages" / package / "Sources"107        for f in sorted(base.rglob("*.swift")):108            src = f.read_text()109            for m in HEADS.finditer(src):110                for text in literals_after(src, m.end()):111                    if not is_sentence(text): continue112                    if "%@" in text:113                        if text.replace("%@", "").strip(" .:;,()") : patterns.add(text)114                    else:115                        exact.add(text)116    return sorted(exact), sorted(patterns)117118def wired_packages():119    text = (ROOT / "project.yml").read_text()120    names = re.findall(r"^\s{2}(Engine\w+):\s*$", text, re.M)121    return ["ORIXConvertCore"] + sorted(set(names))122123def harvest():124    app = ROOT / "build/DerivedData/Build/Products/Debug/ORIX Convert.app/Contents/MacOS/ORIX Convert"125    declared = {}126    if app.exists():127        with tempfile.TemporaryDirectory() as tmp:128            out = pathlib.Path(tmp) / "strings.json"129            subprocess.run([str(app), "-ORIXDumpEngineStrings", str(out)], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=120)130            if out.exists(): declared = json.loads(out.read_text())131    else:132        print("warning: the Debug app is not built; only sources were scanned (run Scripts/build.sh debug first)")133    exact, patterns = scan_sources(wired_packages())134    data = {"declared": declared, "messages": exact, "patterns": patterns}135    (ROOT / "Scripts/engine-strings.json").write_text(json.dumps(data, ensure_ascii=False, indent=1, sort_keys=True) + "\n")136    total = sum(len(v) for v in declared.values())137    print(f"harvested: {total} declared strings, {len(exact)} messages, {len(patterns)} patterns from {', '.join(wired_packages())}")138139# ---------------------------------------------------------------- catalogs140141def load(name, default):142    p = ROOT / "Scripts" / name143    return json.loads(p.read_text()) if p.exists() else default144145def main():146    if "--harvest" in sys.argv:147        harvest()148    keys = app_keys()149    if "--keys" in sys.argv:150        print(json.dumps(keys, ensure_ascii=False, indent=0)); return 0151    fr = load("translations-fr.json", {})152    missing = [k for k in keys if k not in fr]153    bad = [k for k in keys if k in fr and placeholders(k) != placeholders(fr[k])]154    strings = {}155    for k in keys:156        loc = {"en": unit(k)}157        if k in fr and k not in bad: loc["fr-CA"] = unit(fr[k])158        strings[k] = {"localizations": loc}159    RES.mkdir(parents=True, exist_ok=True)160    (RES / "Localizable.xcstrings").write_text(catalog(strings))161    info = {k: {"localizations": {"fr-CA": unit(v)}} for k, v in INFO_FR.items()}162    (RES / "InfoPlist.xcstrings").write_text(catalog(info))163164    engine = load("engine-strings.json", {"declared": {}, "messages": [], "patterns": []})165    efr = load("translations-engines-fr.json", {})166    # Translations of strings the harvest cannot see (error wrappers built in a switch, reasons joined at run167    # time) are kept in the translation file and shipped too.168    manual = [k for k in efr if k not in engine["patterns"]]169    exact = sorted(set(s for v in engine["declared"].values() for s in v) | set(engine["messages"]) | set(k for k in manual if "%@" not in k))170    estrings = {}171    for k in exact:172        loc = {"en": unit(k)}173        if k in efr: loc["fr-CA"] = unit(efr[k])174        estrings[k] = {"localizations": loc}175    (RES / "Engines.xcstrings").write_text(catalog(estrings))176    pattern_table = {k: v for k, v in efr.items() if "%@" in k}177    (RES / "EnginePatterns.json").write_text(json.dumps(pattern_table, ensure_ascii=False, indent=1, sort_keys=True) + "\n")178    emissing = [k for k in exact + engine["patterns"] if k not in efr]179180    stale = [k for k in fr if k not in keys]181    print(f"app: {len(keys)} keys, {len(missing)} without French, {len(bad)} with different placeholders, {len(stale)} unused translations")182    print(f"engines: {len(exact)} strings + {len(engine['patterns'])} patterns, {len(emissing)} without French")183    if "--check" in sys.argv or "--missing" in sys.argv:184        for k in missing: print("MISSING:", json.dumps(k, ensure_ascii=False))185        for k in bad: print("PLACEHOLDERS DIFFER:", json.dumps(k, ensure_ascii=False), "=>", json.dumps(fr[k], ensure_ascii=False))186        for k in emissing: print("ENGINE MISSING:", json.dumps(k, ensure_ascii=False))187        for k in stale: print("UNUSED:", json.dumps(k, ensure_ascii=False))188    return 1 if ("--check" in sys.argv and (missing or bad)) else 0189190sys.exit(main())191
Files main
Menu

Theme