SPB Git

Repository Public License

localize.py 6.7 KB 156 lines python

1#!/usr/bin/env python32# ORIX Secure3# File: Scripts/localize.py4# Description: Refreshes App/Resources/Localizable.xcstrings from the compiler's extracted strings (*.stringsdata of the last Debug5#              simulator build), merges the French (Canada) translations of Scripts/translations-fr.json, writes one/other plural6#              variants in English and French, and checks that every key is translated with matching format specifiers.7#              usage: Scripts/localize.py [--no-sync]     (exit code 1 when a key has no French translation or a check fails)8#9# Author: Simon-Pierre Boucher10# Contact: contact@spboucher.ai11import glob12import json13import os14import re15import subprocess16import sys1718ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))19CATALOG = os.path.join(ROOT, "App", "Resources", "Localizable.xcstrings")20TRANSLATIONS = os.path.join(ROOT, "Scripts", "translations-fr.json")21# Strings are extracted from the app and from every engine package: engines localize through the app's catalog (see `L()`).22STRINGSDATA = os.path.join(ROOT, "build", "DerivedData", "Build", "Intermediates.noindex")23FRENCH = "fr-CA"24NBSP = " "2526# Count strings whose noun inflects: key -> (English one, French one, French other or None to use the translation table).27# The key itself is the English "other" form. In French, "one" also covers zero.28PLURALS = {}29# Keys that are not text to translate.30UNTRANSLATABLE = {"", "—", "IPv4", "IPv6", "DNS", "HSTS", "SHA-256", "ORIX Secure", "%@", "%lld", "%@ %@", "%@: %@", "%@ · %@", "%lld. %@", "%lld.", "·", "%@ %@ %@"}3132SPECIFIER = re.compile(r"%(?:(\d+)\$)?(lld|ld|d|@|lf|f|s|u|llu)")33PLACEHOLDER = re.compile(r"\$\{[A-Za-z_]+\}|\{(?:title|date|time|pages)\}")343536def typography(text):37    """French spacing: no-break space before : ; ? ! and inside « »."""38    text = re.sub(r"[  ]([:;?!])", NBSP + r"\1", text)39    text = re.sub(r"«[  ]", "«" + NBSP, text)40    text = re.sub(r"[  ]»", NBSP + "»", text)41    return text424344def specifiers(text):45    """Format arguments as a sorted list of (position, type); implicit positions are numbered in order."""46    found, implicit = [], 047    for match in SPECIFIER.finditer(text):48        if match.group(1):49            position = int(match.group(1))50        else:51            implicit += 152            position = implicit53        found.append((position, match.group(2)))54    return sorted(found)555657def positional(text):58    """Rewrites implicit specifiers as positional ones (needed as soon as a string has several arguments)."""59    counter = [0]6061    def replace(match):62        if match.group(1):63            return match.group(0)64        counter[0] += 165        return "%%%d$%s" % (counter[0], match.group(2))6667    return SPECIFIER.sub(replace, text) if len(SPECIFIER.findall(text)) > 1 else text686970def unit(value):71    return {"stringUnit": {"state": "translated", "value": value}}727374def plural(one, other):75    return {"variations": {"plural": {"one": unit(one), "other": unit(other)}}}767778def sync():79    files = sorted(f for f in glob.glob(os.path.join(STRINGSDATA, "**", "*.stringsdata"), recursive=True)80                   if "Debug-iphonesimulator" in f and "UITests" not in f and "/ORIXSecureShare.build/" not in f or "/ORIXSecureShare.build/Debug-iphonesimulator" in f)81    if not files:82        print("warning: no .stringsdata under build/DerivedData (build the app first: Scripts/build.sh debug); catalog not refreshed")83        return False84    if not os.path.exists(CATALOG):85        with open(CATALOG, "w") as handle:86            json.dump({"sourceLanguage": "en", "strings": {}, "version": "1.0"}, handle)87    subprocess.run(["xcrun", "xcstringstool", "sync", CATALOG, "--stringsdata"] + files, check=True)88    return True899091def main():92    if "--no-sync" not in sys.argv:93        sync()94    if not os.path.exists(CATALOG):95        sys.exit("error: %s does not exist and could not be created" % CATALOG)96    catalog = json.load(open(CATALOG, encoding="utf-8"))97    table = json.load(open(TRANSLATIONS, encoding="utf-8"))["translations"]98    strings = catalog["strings"]99    problems, untranslated, plurals, translated = [], [], 0, 0100101    for key in sorted(strings):102        entry = strings[key]103        if entry.get("extractionState") == "stale":104            continue105        localizations = entry.setdefault("localizations", {})106        if key in UNTRANSLATABLE:107            entry["shouldTranslate"] = False108            entry.pop("localizations", None)109            continue110        if key not in table:111            untranslated.append(key)112            continue113        french = typography(table[key])114        if PLACEHOLDER.findall(key) != [] and sorted(PLACEHOLDER.findall(key)) != sorted(PLACEHOLDER.findall(french)):115            problems.append("placeholders differ: %r -> %r" % (key, french))116        if specifiers(key) != specifiers(french):117            problems.append("format specifiers differ: %r -> %r" % (key, french))118        if key in PLURALS:119            english_one, french_one = PLURALS[key]120            french_one = typography(french_one)121            english_other = positional(key)122            # A singular form may drop the number ("Every page"); otherwise it must take the same arguments.123            for label, value in (("en one", english_one), ("fr one", french_one)):124                if specifiers(value) not in ([], specifiers(key)):125                    problems.append("plural %s has different specifiers: %r" % (label, value))126            localizations["en"] = plural(english_one, english_other)127            localizations[FRENCH] = plural(french_one, positional(french))128            plurals += 1129        else:130            localizations.pop("en", None)131            localizations[FRENCH] = unit(french)132        translated += 1133134    stale = sorted(k for k, v in strings.items() if v.get("extractionState") == "stale")135    orphans = sorted(k for k in table if k not in strings or strings[k].get("extractionState") == "stale")136    with open(CATALOG, "w", encoding="utf-8") as handle:137        json.dump(catalog, handle, ensure_ascii=False, indent=2, sort_keys=True, separators=(",", " : "))138        handle.write("\n")139140    print("%d keys in the catalog, %d translated to %s (%d with one/other plural variants), %d not translatable"141          % (len(strings) - len(stale), translated, FRENCH, plurals, len([k for k in strings if k in UNTRANSLATABLE])))142    if stale:143        print("stale keys (no longer in the code, safe to delete from the catalog): %s" % stale)144    if orphans:145        print("translations without a key in the catalog (remove from translations-fr.json): %s" % orphans)146    for key in untranslated:147        print("UNTRANSLATED: %r" % key)148    for problem in problems:149        print("PROBLEM: %s" % problem)150    if untranslated or problems:151        sys.exit(1)152153154if __name__ == "__main__":155    main()156
Files main
Menu

Theme