| 1 | #!/usr/bin/env python3 |
| 2 | """Copies copy review's strings into the catalog. |
| 3 | |
| 4 | native/scripts/sync-strings.py [path/to/copy-review/strings.json] |
| 5 | |
| 6 | Every id in copy review's file (git-ignored, in the main checkout's |
| 7 | copy-review/; or $BASE_COPY_REVIEW) becomes, or replaces, the catalog entry |
| 8 | of that key, with its text and its context as the comment. Keys the review |
| 9 | doesn't list (the web's approved Ask copy, `ask.*`) are kept as they are. |
| 10 | An id whose notes ask for a plural ("Plural in the String Catalog: one = |
| 11 | '…', other = '…'") gets plural variations, `{count}` written `%lld`. |
| 12 | check-strings.py then holds the two to each other. |
| 13 | """ |
| 14 | import json, os, pathlib, re, sys |
| 15 | |
| 16 | native = pathlib.Path(__file__).resolve().parent.parent |
| 17 | repo = native.parent |
| 18 | catalog_path = native / "apple/Sources/BaseKit/Resources/Localizable.xcstrings" |
| 19 | given = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("BASE_COPY_REVIEW") |
| 20 | candidates = [pathlib.Path(given)] if given else [repo / "copy-review/strings.json", repo.parent / "base/copy-review/strings.json"] |
| 21 | source = next((p for p in candidates if p.is_file()), None) |
| 22 | if not source: |
| 23 | sys.exit("sync-strings: no copy-review/strings.json; pass its path") |
| 24 | |
| 25 | PLURAL = re.compile(r"Plural in the String Catalog: one = '([^']*)', other = '([^']*)'") |
| 26 | catalog = json.loads(catalog_path.read_text()) |
| 27 | for item in json.loads(source.read_text()): |
| 28 | comment = item["context"] + (f" ({item['notes']})" if item.get("notes") else "") |
| 29 | entry = {"comment": comment, "extractionState": "manual"} |
| 30 | plural = PLURAL.search(item.get("notes", "")) |
| 31 | if plural: |
| 32 | one, other = (s.replace("{count}", "%lld") for s in plural.groups()) |
| 33 | entry["localizations"] = {"en": {"variations": {"plural": { |
| 34 | "one": {"stringUnit": {"state": "translated", "value": one}}, |
| 35 | "other": {"stringUnit": {"state": "translated", "value": other}}, |
| 36 | }}}} |
| 37 | else: |
| 38 | entry["localizations"] = {"en": {"stringUnit": {"state": "translated", "value": item["text"]}}} |
| 39 | catalog["strings"][item["id"]] = entry |
| 40 | catalog["strings"] = dict(sorted(catalog["strings"].items())) |
| 41 | catalog_path.write_text(json.dumps(catalog, indent=2, ensure_ascii=False) + "\n") |
| 42 | print(f"sync-strings: {catalog_path.relative_to(repo)} now holds copy review's {source}") |
| 43 | |