#!/usr/bin/env python3 """The string catalog's gate. 1. Every entry in apple/Sources/BaseKit/Resources/Localizable.xcstrings has its English text. 2. Every key a Swift source asks for (BaseText.text / format / plural, or a key literal handed to a helper) is in the catalog, and every catalog entry is asked for somewhere or listed as waiting for a later task (WAITING below). 3. No view hard-codes copy: Text, Button, Label and friends take catalog text or `verbatim:`, never a bare literal. 4. With copy review's file (copy-review/strings.json, git-ignored, in the main checkout; or $BASE_COPY_REVIEW), every id there is in the catalog with exactly its text. Without it, this step says so and is skipped. """ 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" sources = native / "apple/Sources" failures = [] catalog = json.loads(catalog_path.read_text()) if catalog.get("sourceLanguage") != "en": failures.append("the catalog's source language is not en") texts = {} plurals = {} for key, entry in catalog["strings"].items(): value = entry.get("localizations", {}).get("en", {}).get("stringUnit", {}).get("value") plural = entry.get("localizations", {}).get("en", {}).get("variations", {}).get("plural") if plural: # Plurals select on %lld; copy review writes it {count}. forms = {k: v["stringUnit"]["value"].replace("%lld", "{count}") for k, v in plural.items()} if "other" not in forms: failures.append(f"{key}: a plural with no 'other' form") plurals[key] = forms value = forms.get("other") if value is None: failures.append(f"{key}: no English text") texts[key] = value # Strings copy review approved for screens later tasks draw (Tasks 12 to # 14). They stay in the catalog so the review's text is the app's text. WAITING = re.compile(r"^native\.(signin|sidebar|account|menubar|settings|phone|colophon|offline)\b") used = set() for path in sources.rglob("*.swift"): text = path.read_text() for match in re.finditer(r'BaseText\.(?:text|format|plural)\(\s*"([^"]+)"', text): if match.group(1) != "…": used.add(match.group(1)) # A key handed to a helper (lede("ask.connect.lede", …)) is used too. for match in re.finditer(r'"((?:ask|native|chat)\.[a-z0-9_.]+)"', text): used.add(match.group(1)) for n, line in enumerate(text.splitlines(), 1): if re.search(r'\b(Text|Button|Label|Toggle|Section|LabeledContent|Picker|TextField|Link|Menu)\(\s*"(?!")', line) and "verbatim" not in line: failures.append(f"{path.relative_to(native)}:{n}: copy hard-coded outside the catalog: {line.strip()}") # The Colophon's words are copy too: its JSON names them by catalog key, # and holds no words of its own beyond names, addresses and licences. colophon = json.loads((native / "apple/Sources/BaseKit/Resources/Colophon.json").read_text()) for section in colophon["sections"]: for entry in section["entries"]: for field in ("blurb", "usage"): if field in entry: failures.append(f"Colophon.json: {entry['id']}'s {field} is words, not a catalog key") key = entry.get(field + "Key") if not key: failures.append(f"Colophon.json: {entry['id']} has no {field}Key") else: used.add(key) for key in sorted(used - texts.keys()): failures.append(f"{key}: used in Swift but not in the catalog") for key in sorted(texts.keys() - used): if not WAITING.match(key): failures.append(f"{key}: in the catalog but never used") review = os.environ.get("BASE_COPY_REVIEW") candidates = [pathlib.Path(review)] if review else [repo / "copy-review/strings.json", repo.parent / "base/copy-review/strings.json"] found = next((p for p in candidates if p.is_file()), None) if found: for item in json.loads(found.read_text()): if item["id"] not in texts: failures.append(f"{item['id']}: in copy review but not in the catalog") elif texts[item["id"]] != item["text"]: failures.append(f"{item['id']}: the catalog says {texts[item['id']]!r}, copy review {item['text']!r}") plural = re.search(r"Plural in the String Catalog: one = '([^']*)', other = '([^']*)'", item.get("notes", "")) if plural and plurals.get(item["id"]) != {"one": plural.group(1), "other": plural.group(2)}: failures.append(f"{item['id']}: copy review asks for the plural {plural.groups()}, the catalog has {plurals.get(item['id'])}") print(f"strings: the catalog matches copy review ({found})") else: print("strings: copy review's strings.json is not here (git-ignored); its comparison skipped") if failures: print("\n".join(failures), file=sys.stderr) sys.exit(1) print(f"strings: {len(texts)} in the catalog, {len(used)} used")