Repository Public
1#!/usr/bin/env python32"""String Catalog helper for MacLustr Command Center (French = source language, English).34 strings.py add <catalog.xcstrings> merge entries read on stdin, one per line: key<TAB>français<TAB>English5 strings.py check <catalog.xcstrings>... --sources <dir>...6 fails if a key lacks a translated fr or en value, if fr and en disagree on format specifiers, or if a7 literal key used in the sources through tr("...") is absent from every catalog given.89The catalogs stay the source of truth (editable in Xcode); `add` is only a convenience for bulk edits.10"""11import json, os, re, sys1213LANGS = ("fr", "en")1415def load(path):16 if os.path.exists(path):17 return json.load(open(path, encoding="utf-8"))18 return {"sourceLanguage": "fr", "version": "1.0", "strings": {}}1920def save(path, cat):21 cat["strings"] = dict(sorted(cat["strings"].items()))22 with open(path, "w", encoding="utf-8") as f:23 json.dump(cat, f, ensure_ascii=False, indent=2, sort_keys=False)24 f.write("\n")2526def add(path):27 cat = load(path)28 n = 029 for line in sys.stdin.read().splitlines():30 if not line.strip() or line.startswith("#"):31 continue32 parts = line.split("\t")33 if len(parts) != 3:34 sys.exit(f"bad line (need key<TAB>fr<TAB>en): {line!r}")35 key, fr, en = (p.strip() for p in parts)36 cat["strings"][key] = {"extractionState": "manual", "localizations": {37 "fr": {"stringUnit": {"state": "translated", "value": fr.replace("\\n", "\n")}},38 "en": {"stringUnit": {"state": "translated", "value": en.replace("\\n", "\n")}}}}39 n += 140 save(path, cat)41 print(f"{path}: {n} entries merged, {len(cat['strings'])} total")4243SPEC = re.compile(r"%(?:\d+\$)?(?:lld|ld|d|@|f|\.\d+f|s|%)")4445def check(args):46 cats, srcs, mode = [], [], "cat"47 for a in args:48 if a == "--sources": mode = "src"; continue49 (cats if mode == "cat" else srcs).append(a)50 problems, keys = [], set()51 for path in cats:52 cat = load(path)53 for key, entry in cat["strings"].items():54 keys.add(key)55 vals = {}56 for lang in LANGS:57 unit = entry.get("localizations", {}).get(lang, {}).get("stringUnit")58 if not unit or unit.get("state") != "translated" or not unit.get("value", "").strip():59 problems.append(f"{path}: {key}: missing {lang}")60 else:61 vals[lang] = unit["value"]62 if len(vals) == 2 and sorted(SPEC.findall(vals["fr"])) != sorted(SPEC.findall(vals["en"])):63 problems.append(f"{path}: {key}: format specifiers differ between fr and en")64 used = set()65 pat = re.compile(r'\btr\(\s*"([^"\\]+)"')66 for root in srcs:67 for d, _, files in os.walk(root):68 for fn in files:69 if fn.endswith(".swift"):70 used.update(pat.findall(open(os.path.join(d, fn), encoding="utf-8").read()))71 for key in sorted(used - keys):72 problems.append(f"used in sources but absent from the catalogs: {key}")73 if problems:74 print("\n".join(problems)); sys.exit(1)75 print(f"{len(keys)} keys complete in {', '.join(LANGS)}; {len(used)} literal keys used in sources, all present")7677if __name__ == "__main__":78 if len(sys.argv) >= 3 and sys.argv[1] == "add": add(sys.argv[2])79 elif len(sys.argv) >= 3 and sys.argv[1] == "check": check(sys.argv[2:])80 else: sys.exit(__doc__)81