Repository Public
1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/build-string-catalog.py4# Description: Generates Sources/SPNotesFeatures/Resources/Localizable.xcstrings (source language fr, English translations)5# from the L("…") / LF("…", …) literals of the SPNotesFeatures sources and the English dictionaries6# Localization/en/*.json ({"texte français": "English text"}). `--check` fails when a literal has no English7# or when the committed catalogue is out of date.8#9# Author: Simon-Pierre Boucher10# Contact: contact@spboucher.ai11import glob, json, os, re, sys1213root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))14sources = os.path.join(root, "Sources", "SPNotesFeatures")15catalog = os.path.join(sources, "Resources", "Localizable.xcstrings")16literal = re.compile(r'\bLF?\(\s*"((?:[^"\\]|\\.)*)"')1718def unescape(text):19 return text.replace('\\"', '"').replace("\\n", "\n").replace("\\\\", "\\")2021keys = set()22for path in glob.glob(os.path.join(sources, "**", "*.swift"), recursive=True):23 with open(path, encoding="utf-8") as handle:24 code = "\n".join(line for line in handle.read().split("\n") if not line.lstrip().startswith("//"))25 for match in literal.finditer(code):26 keys.add(unescape(match.group(1)))2728english = {}29for path in sorted(glob.glob(os.path.join(root, "Localization", "en", "*.json"))):30 with open(path, encoding="utf-8") as handle:31 english.update(json.load(handle))3233missing = sorted(key for key in keys if not english.get(key))34strings = {}35for key in sorted(keys):36 entry = {}37 if english.get(key):38 entry["localizations"] = {"en": {"stringUnit": {"state": "translated", "value": english[key]}}}39 strings[key] = entry40text = json.dumps({"sourceLanguage": "fr", "strings": strings, "version": "1.0"}, ensure_ascii=False, indent=2, sort_keys=True) + "\n"4142if "--check" in sys.argv:43 current = open(catalog, encoding="utf-8").read() if os.path.exists(catalog) else ""44 problems = []45 if missing: problems.append("%d string(s) without English: %s" % (len(missing), "; ".join(missing[:20])))46 if current != text: problems.append("Localizable.xcstrings is out of date: run Scripts/build-string-catalog.py")47 if problems:48 print("\n".join(problems)); sys.exit(1)49 print("%d strings, all translated, catalogue up to date" % len(keys)); sys.exit(0)5051os.makedirs(os.path.dirname(catalog), exist_ok=True)52with open(catalog, "w", encoding="utf-8") as handle:53 handle.write(text)54print("%d strings written, %d without English" % (len(keys), len(missing)))55for key in missing: print(" MISSING en:", key)56