Repository Public
1#!/usr/bin/env python32# ORIX DJ3# File: Scripts/localize.py4# Description: Rebuilds App/Resources/Localizable.xcstrings from the keys the compiler extracted (build the app first),5# with French-Canadian values from Scripts/translations-fr-CA.json. `--missing` writes build/missing-fr.json.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9import glob, json, os, re, subprocess, sys1011ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))12CATALOG = os.path.join(ROOT, "App/Resources/Localizable.xcstrings")13TRANSLATIONS = os.path.join(ROOT, "Scripts/translations-fr-CA.json")1415def main():16 data = []17 for base in ("build/dd", "build/DerivedDataRelease"):18 data += glob.glob(os.path.join(ROOT, base, "Build/Intermediates.noindex/ORIXDJ.build/**/*.stringsdata"), recursive=True)19 if not data:20 if os.path.exists(CATALOG):21 print("no .stringsdata found; keeping the existing catalog")22 return23 sys.exit("No .stringsdata found: build the app first (Scripts/build.sh debug).")24 # Use the most recently built configuration only.25 groups = {}26 for p in data:27 groups.setdefault(p.split("/Build/Intermediates.noindex/")[0], []).append(p)28 data = max(groups.values(), key=lambda files: max(os.path.getmtime(f) for f in files))29 json.dump({"sourceLanguage": "en", "strings": {}, "version": "1.0"}, open(CATALOG, "w"))30 subprocess.run(["xcrun", "xcstringstool", "sync", CATALOG, "--stringsdata", *data], check=True)31 catalog = json.load(open(CATALOG, encoding="utf-8"))32 french = json.load(open(TRANSLATIONS, encoding="utf-8")) if os.path.exists(TRANSLATIONS) else {}33 missing = []34 for key, entry in catalog["strings"].items():35 if not re.search(r"[A-Za-z]{2}", key):36 entry["shouldTranslate"] = False37 continue38 if key in french:39 entry["localizations"] = {"fr-CA": {"stringUnit": {"state": "translated", "value": french[key]}}}40 else:41 missing.append(key)42 json.dump(catalog, open(CATALOG, "w", encoding="utf-8"), ensure_ascii=False, indent=2, sort_keys=True)43 unused = sorted(set(french) - set(catalog["strings"]))44 print(f"{len(catalog['strings'])} keys, {len(catalog['strings']) - len(missing)} with French, {len(missing)} missing, {len(unused)} unused translations")45 if "--missing" in sys.argv:46 os.makedirs(os.path.join(ROOT, "build"), exist_ok=True)47 json.dump(sorted(missing), open(os.path.join(ROOT, "build/missing-fr.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=0)4849main()50