SPB Git

Repository Public

check-localization.py 2.5 KB 66 lines python

1#!/usr/bin/env python32"""Verifies that every string passed to L("...") has a French translation.34Usage: scripts/check-localization.py [--list]   (--list prints the keys found in the sources)5Exit status is non-zero when a key is missing, a translation is empty, or format specifiers differ.6"""7import pathlib, re, sys89ROOT = pathlib.Path(__file__).resolve().parent.parent10KEY = re.compile(r'\bL\(\s*"((?:[^"\\]|\\.)*)"')11PAIR = re.compile(r'^"((?:[^"\\]|\\.)*)"\s*=\s*"((?:[^"\\]|\\.)*)";\s*$')12SPEC = re.compile(r'%(?:\d+\$)?(?:@|d|ld|lld|f|\.\d+f)')1314def source_keys():15    keys = {}16    sources = sorted((ROOT / "SPBrowser").rglob("*.swift")) + sorted((ROOT / "Mobile").rglob("*.swift"))17    for path in sources:18        if "Tests" in path.parts or "UITests" in path.parts:19            continue20        for match in KEY.finditer(path.read_text(encoding="utf-8")):21            keys.setdefault(match.group(1), path.relative_to(ROOT))22    return keys2324def table(lang):25    result = {}26    path = ROOT / "SPBrowser" / "Resources" / f"{lang}.lproj" / "Localizable.strings"27    for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):28        line = line.strip()29        if not line or line.startswith("/*") or line.startswith("//"):30            continue31        match = PAIR.match(line)32        if not match:33            print(f"{path.name}:{number}: unreadable line: {line[:60]}")34            sys.exit(2)35        if match.group(1) in result:36            print(f"{path.name}:{number}: duplicate key: {match.group(1)[:60]}")37            sys.exit(2)38        result[match.group(1)] = match.group(2)39    return result4041def main():42    keys = source_keys()43    if "--list" in sys.argv:44        print("\n".join(keys))45        return 046    french = table("fr")47    problems = 048    for key, origin in keys.items():49        if key not in french:50            print(f"MISSING  {origin}: {key}")51            problems += 152        elif not french[key].strip():53            print(f"EMPTY    {key}")54            problems += 155        elif sorted(s.replace("ld", "d") for s in SPEC.findall(key)) != sorted(s.replace("ld", "d") for s in SPEC.findall(french[key])):56            print(f"FORMAT   {key!r} -> {french[key]!r}")57            problems += 158    unused = [k for k in french if k not in keys]59    for key in unused:60        print(f"UNUSED   {key}")61    print(f"{len(keys)} keys in sources, {len(french)} French translations, {problems} problems, {len(unused)} unused")62    return 1 if problems or unused else 06364if __name__ == "__main__":65    sys.exit(main())66
Files main
Menu

Theme