oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

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