SPB Git

Repository Public License

localize.py 7.6 KB 170 lines python

1#!/usr/bin/env python32# ORIX Scan3# 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")21STRINGSDATA = os.path.join(ROOT, "build", "DerivedData", "Build", "Intermediates.noindex", "ORIXScan.build",22                           "Debug-iphonesimulator", "ORIXScan.build")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    "%lld pages": ("%lld page", "%lld page"),30    "%lld lines": ("%lld line", "%lld ligne"),31    "%lld lines of text": ("%lld line of text", "%lld ligne de texte"),32    "%lld matches": ("%lld match", "%lld résultat"),33    "%lld files": ("%lld file", "%lld fichier"),34    "%lld steps": ("%lld step", "%lld étape"),35    "%lld documents created": ("%lld document created", "%lld document créé"),36    "%lld documents moved to Recently Deleted": ("%lld document moved to Recently Deleted", "%lld document déplacé dans Supprimés récemment"),37    "Every %lld pages": ("Every page", "Chaque page"),38    "Share %lld Files": ("Share %lld File", "Partager %lld fichier"),39    "Documents are kept for %lld days": ("Documents are kept for %lld day", "Les documents sont conservés %lld jour"),40    "%lld languages are supported by this version of iOS in %@ mode. The list comes from the system; it can differ between modes.":41        ("%1$lld language is supported by this version of iOS in %2$@ mode. The list comes from the system; it can differ between modes.",42         "%1$lld langue est prise en charge par cette version d’iOS en mode %2$@. La liste provient du système ; elle peut varier d’un mode à l’autre."),43}44# Keys that are not text to translate.45UNTRANSLATABLE = {""}4647SPECIFIER = re.compile(r"%(?:(\d+)\$)?(lld|ld|d|@|lf|f|s|u|llu)")48PLACEHOLDER = re.compile(r"\$\{[A-Za-z_]+\}|\{(?:title|date|time|pages)\}")495051def typography(text):52    """French spacing: no-break space before : ; ? ! and inside « »."""53    text = re.sub(r"[  ]([:;?!])", NBSP + r"\1", text)54    text = re.sub(r"«[  ]", "«" + NBSP, text)55    text = re.sub(r"[  ]»", NBSP + "»", text)56    return text575859def specifiers(text):60    """Format arguments as a sorted list of (position, type); implicit positions are numbered in order."""61    found, implicit = [], 062    for match in SPECIFIER.finditer(text):63        if match.group(1):64            position = int(match.group(1))65        else:66            implicit += 167            position = implicit68        found.append((position, match.group(2)))69    return sorted(found)707172def positional(text):73    """Rewrites implicit specifiers as positional ones (needed as soon as a string has several arguments)."""74    counter = [0]7576    def replace(match):77        if match.group(1):78            return match.group(0)79        counter[0] += 180        return "%%%d$%s" % (counter[0], match.group(2))8182    return SPECIFIER.sub(replace, text) if len(SPECIFIER.findall(text)) > 1 else text838485def unit(value):86    return {"stringUnit": {"state": "translated", "value": value}}878889def plural(one, other):90    return {"variations": {"plural": {"one": unit(one), "other": unit(other)}}}919293def sync():94    files = sorted(glob.glob(os.path.join(STRINGSDATA, "**", "*.stringsdata"), recursive=True))95    if not files:96        print("warning: no .stringsdata under build/DerivedData (build the app first: Scripts/build.sh debug); catalog not refreshed")97        return False98    if not os.path.exists(CATALOG):99        with open(CATALOG, "w") as handle:100            json.dump({"sourceLanguage": "en", "strings": {}, "version": "1.0"}, handle)101    subprocess.run(["xcrun", "xcstringstool", "sync", CATALOG, "--stringsdata"] + files, check=True)102    return True103104105def main():106    if "--no-sync" not in sys.argv:107        sync()108    if not os.path.exists(CATALOG):109        sys.exit("error: %s does not exist and could not be created" % CATALOG)110    catalog = json.load(open(CATALOG, encoding="utf-8"))111    table = json.load(open(TRANSLATIONS, encoding="utf-8"))["translations"]112    strings = catalog["strings"]113    problems, untranslated, plurals, translated = [], [], 0, 0114115    for key in sorted(strings):116        entry = strings[key]117        if entry.get("extractionState") == "stale":118            continue119        localizations = entry.setdefault("localizations", {})120        if key in UNTRANSLATABLE:121            entry["shouldTranslate"] = False122            entry.pop("localizations", None)123            continue124        if key not in table:125            untranslated.append(key)126            continue127        french = typography(table[key])128        if PLACEHOLDER.findall(key) != [] and sorted(PLACEHOLDER.findall(key)) != sorted(PLACEHOLDER.findall(french)):129            problems.append("placeholders differ: %r -> %r" % (key, french))130        if specifiers(key) != specifiers(french):131            problems.append("format specifiers differ: %r -> %r" % (key, french))132        if key in PLURALS:133            english_one, french_one = PLURALS[key]134            french_one = typography(french_one)135            english_other = positional(key)136            # A singular form may drop the number ("Every page"); otherwise it must take the same arguments.137            for label, value in (("en one", english_one), ("fr one", french_one)):138                if specifiers(value) not in ([], specifiers(key)):139                    problems.append("plural %s has different specifiers: %r" % (label, value))140            localizations["en"] = plural(english_one, english_other)141            localizations[FRENCH] = plural(french_one, positional(french))142            plurals += 1143        else:144            localizations.pop("en", None)145            localizations[FRENCH] = unit(french)146        translated += 1147148    stale = sorted(k for k, v in strings.items() if v.get("extractionState") == "stale")149    orphans = sorted(k for k in table if k not in strings or strings[k].get("extractionState") == "stale")150    with open(CATALOG, "w", encoding="utf-8") as handle:151        json.dump(catalog, handle, ensure_ascii=False, indent=2, sort_keys=True, separators=(",", " : "))152        handle.write("\n")153154    print("%d keys in the catalog, %d translated to %s (%d with one/other plural variants), %d not translatable"155          % (len(strings) - len(stale), translated, FRENCH, plurals, len([k for k in strings if k in UNTRANSLATABLE])))156    if stale:157        print("stale keys (no longer in the code, safe to delete from the catalog): %s" % stale)158    if orphans:159        print("translations without a key in the catalog (remove from translations-fr.json): %s" % orphans)160    for key in untranslated:161        print("UNTRANSLATED: %r" % key)162    for problem in problems:163        print("PROBLEM: %s" % problem)164    if untranslated or problems:165        sys.exit(1)166167168if __name__ == "__main__":169    main()170
Files main
Menu

Theme