| 1 | #!/usr/bin/env python3 |
| 2 | """The string catalog's gate. |
| 3 | |
| 4 | 1. Every entry in apple/Sources/BaseKit/Resources/Localizable.xcstrings has |
| 5 | its English text. |
| 6 | 2. Every key a Swift source asks for (BaseText.text / format / plural, or a |
| 7 | key literal handed to a helper) is in |
| 8 | the catalog, and every catalog entry is asked for somewhere or listed as |
| 9 | waiting for a later task (WAITING below). |
| 10 | 3. No view hard-codes copy: Text, Button, Label and friends take catalog |
| 11 | text or `verbatim:`, never a bare literal. |
| 12 | 4. With copy review's file (copy-review/strings.json, git-ignored, in the |
| 13 | main checkout; or $BASE_COPY_REVIEW), every id there is in the catalog |
| 14 | with exactly its text. Without it, this step says so and is skipped. |
| 15 | """ |
| 16 | import json, os, pathlib, re, sys |
| 17 | |
| 18 | native = pathlib.Path(__file__).resolve().parent.parent |
| 19 | repo = native.parent |
| 20 | catalog_path = native / "apple/Sources/BaseKit/Resources/Localizable.xcstrings" |
| 21 | sources = native / "apple/Sources" |
| 22 | failures = [] |
| 23 | |
| 24 | catalog = json.loads(catalog_path.read_text()) |
| 25 | if catalog.get("sourceLanguage") != "en": |
| 26 | failures.append("the catalog's source language is not en") |
| 27 | texts = {} |
| 28 | plurals = {} |
| 29 | for key, entry in catalog["strings"].items(): |
| 30 | value = entry.get("localizations", {}).get("en", {}).get("stringUnit", {}).get("value") |
| 31 | plural = entry.get("localizations", {}).get("en", {}).get("variations", {}).get("plural") |
| 32 | if plural: |
| 33 | # Plurals select on %lld; copy review writes it {count}. |
| 34 | forms = {k: v["stringUnit"]["value"].replace("%lld", "{count}") for k, v in plural.items()} |
| 35 | if "other" not in forms: |
| 36 | failures.append(f"{key}: a plural with no 'other' form") |
| 37 | plurals[key] = forms |
| 38 | value = forms.get("other") |
| 39 | if value is None: |
| 40 | failures.append(f"{key}: no English text") |
| 41 | texts[key] = value |
| 42 | |
| 43 | # Strings copy review approved for screens later tasks draw (Tasks 12 to |
| 44 | # 14). They stay in the catalog so the review's text is the app's text. |
| 45 | WAITING = re.compile(r"^native\.(signin|sidebar|account|menubar|settings|phone|colophon|offline)\b") |
| 46 | |
| 47 | used = set() |
| 48 | for path in sources.rglob("*.swift"): |
| 49 | text = path.read_text() |
| 50 | for match in re.finditer(r'BaseText\.(?:text|format|plural)\(\s*"([^"]+)"', text): |
| 51 | if match.group(1) != "…": used.add(match.group(1)) |
| 52 | # A key handed to a helper (lede("ask.connect.lede", …)) is used too. |
| 53 | for match in re.finditer(r'"((?:ask|native|chat)\.[a-z0-9_.]+)"', text): |
| 54 | used.add(match.group(1)) |
| 55 | for n, line in enumerate(text.splitlines(), 1): |
| 56 | if re.search(r'\b(Text|Button|Label|Toggle|Section|LabeledContent|Picker|TextField|Link|Menu)\(\s*"(?!")', line) and "verbatim" not in line: |
| 57 | failures.append(f"{path.relative_to(native)}:{n}: copy hard-coded outside the catalog: {line.strip()}") |
| 58 | |
| 59 | # The Colophon's words are copy too: its JSON names them by catalog key, |
| 60 | # and holds no words of its own beyond names, addresses and licences. |
| 61 | colophon = json.loads((native / "apple/Sources/BaseKit/Resources/Colophon.json").read_text()) |
| 62 | for section in colophon["sections"]: |
| 63 | for entry in section["entries"]: |
| 64 | for field in ("blurb", "usage"): |
| 65 | if field in entry: |
| 66 | failures.append(f"Colophon.json: {entry['id']}'s {field} is words, not a catalog key") |
| 67 | key = entry.get(field + "Key") |
| 68 | if not key: |
| 69 | failures.append(f"Colophon.json: {entry['id']} has no {field}Key") |
| 70 | else: |
| 71 | used.add(key) |
| 72 | |
| 73 | for key in sorted(used - texts.keys()): |
| 74 | failures.append(f"{key}: used in Swift but not in the catalog") |
| 75 | for key in sorted(texts.keys() - used): |
| 76 | if not WAITING.match(key): |
| 77 | failures.append(f"{key}: in the catalog but never used") |
| 78 | |
| 79 | review = os.environ.get("BASE_COPY_REVIEW") |
| 80 | candidates = [pathlib.Path(review)] if review else [repo / "copy-review/strings.json", repo.parent / "base/copy-review/strings.json"] |
| 81 | found = next((p for p in candidates if p.is_file()), None) |
| 82 | if found: |
| 83 | for item in json.loads(found.read_text()): |
| 84 | if item["id"] not in texts: |
| 85 | failures.append(f"{item['id']}: in copy review but not in the catalog") |
| 86 | elif texts[item["id"]] != item["text"]: |
| 87 | failures.append(f"{item['id']}: the catalog says {texts[item['id']]!r}, copy review {item['text']!r}") |
| 88 | plural = re.search(r"Plural in the String Catalog: one = '([^']*)', other = '([^']*)'", item.get("notes", "")) |
| 89 | if plural and plurals.get(item["id"]) != {"one": plural.group(1), "other": plural.group(2)}: |
| 90 | failures.append(f"{item['id']}: copy review asks for the plural {plural.groups()}, the catalog has {plurals.get(item['id'])}") |
| 91 | print(f"strings: the catalog matches copy review ({found})") |
| 92 | else: |
| 93 | print("strings: copy review's strings.json is not here (git-ignored); its comparison skipped") |
| 94 | |
| 95 | if failures: |
| 96 | print("\n".join(failures), file=sys.stderr) |
| 97 | sys.exit(1) |
| 98 | print(f"strings: {len(texts)} in the catalog, {len(used)} used") |
| 99 | |