#!/usr/bin/env python3 """Copies copy review's strings into the catalog. native/scripts/sync-strings.py [path/to/copy-review/strings.json] Every id in copy review's file (git-ignored, in the main checkout's copy-review/; or $BASE_COPY_REVIEW) becomes, or replaces, the catalog entry of that key, with its text and its context as the comment. Keys the review doesn't list (the web's approved Ask copy, `ask.*`) are kept as they are. An id whose notes ask for a plural ("Plural in the String Catalog: one = '…', other = '…'") gets plural variations, `{count}` written `%lld`. check-strings.py then holds the two to each other. """ import json, os, pathlib, re, sys native = pathlib.Path(__file__).resolve().parent.parent repo = native.parent catalog_path = native / "apple/Sources/BaseKit/Resources/Localizable.xcstrings" given = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("BASE_COPY_REVIEW") candidates = [pathlib.Path(given)] if given else [repo / "copy-review/strings.json", repo.parent / "base/copy-review/strings.json"] source = next((p for p in candidates if p.is_file()), None) if not source: sys.exit("sync-strings: no copy-review/strings.json; pass its path") PLURAL = re.compile(r"Plural in the String Catalog: one = '([^']*)', other = '([^']*)'") catalog = json.loads(catalog_path.read_text()) for item in json.loads(source.read_text()): comment = item["context"] + (f" ({item['notes']})" if item.get("notes") else "") entry = {"comment": comment, "extractionState": "manual"} plural = PLURAL.search(item.get("notes", "")) if plural: one, other = (s.replace("{count}", "%lld") for s in plural.groups()) entry["localizations"] = {"en": {"variations": {"plural": { "one": {"stringUnit": {"state": "translated", "value": one}}, "other": {"stringUnit": {"state": "translated", "value": other}}, }}}} else: entry["localizations"] = {"en": {"stringUnit": {"state": "translated", "value": item["text"]}}} catalog["strings"][item["id"]] = entry catalog["strings"] = dict(sorted(catalog["strings"].items())) catalog_path.write_text(json.dumps(catalog, indent=2, ensure_ascii=False) + "\n") print(f"sync-strings: {catalog_path.relative_to(repo)} now holds copy review's {source}")