Files
coucou/scripts/test-localization.py
Claudeandshawn-t516 a8dcdfc2be Traditional Chinese on the Mac (from #314)
shawn-t516's Traditional Chinese translation:
- Localizable.xcstrings: the "zh-Hant" localization of every key, from
  #314 (plus the update.* and step.needs-input strings main gained since),
  and 繁體中文 in the language picker after 简体中文;
  CFBundleLocalizations of both Mac targets.
- #314's wrapping of hard-coded English in String(localized:): outfit
  names (MochiWardrobe), PillCatalog category titles and subtitles, and the
  DeepSeek, Qwen and Moonshot "key saved" messages. Pill ids are unchanged.
  The catalog gains #314's new keys (those, plus literals the code already
  shows that the catalog lacked, and the plan card's Change plan, Auto mode
  and Manual buttons), each translated in every language; the
  ones Windows already translates in extra.json reuse those translations
  (and #701/#700's German and Turkish) and leave extra.json, since the Mac
  catalog now has them. Brand names and URLs keep their English text.
- scripts/test-localization.py and its CONTRIBUTING note, now also run by
  the macOS Build workflow.

Co-Authored-By: shawn-t516 <180199675+shawn-t516@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
2026-10-20 02:23:33 +02:00

80 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Check macOS Traditional Chinese coverage, plural paths, and format arguments."""
from collections import Counter
import json
from pathlib import Path
import re
import sys
ROOT = Path(__file__).resolve().parent.parent
CATALOG = ROOT / "NotchBuddy/Resources/Localizable.xcstrings"
# shortcut: No catalog placeholder uses the space flag; support it when one is added.
FORMAT = re.compile(r"%(?:\d+\$)?[-+#0]*(?:\d+|\*)?(?:\.(?:\d+|\*))?(?:hh|ll|[hljztL])?[@diuoxXfFeEgGaAcCsSp]")
def unique_keys(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"Duplicate catalog key: {key!r}")
result[key] = value
return result
def units(node, path=()):
if isinstance(node, dict):
if "stringUnit" in node:
yield path, node["stringUnit"]
else:
for key, value in node.items():
yield from units(value, path + (key,))
def arguments(value):
# A literal %% is not an interpolation argument.
return Counter(FORMAT.findall(value.replace("%%", "")))
def main():
catalog = json.loads(CATALOG.read_text(), object_pairs_hook=unique_keys)
errors = []
if catalog["sourceLanguage"] != "en":
errors.append("Expected English source language")
for key, entry in catalog["strings"].items():
localizations = entry.get("localizations", {})
source = dict(units(localizations.get("en", {"stringUnit": {"value": key}})))
translated = dict(units(localizations.get("zh-Hant", {})))
if set(source) != set(translated):
errors.append(f"{key!r}: missing translation or mismatched plural/substitution paths")
continue
for path, unit in source.items():
target = translated[path]
if target.get("state") != "translated" or not target.get("value", "").strip():
errors.append(f"{key!r} {path}: incomplete translation")
if arguments(unit["value"]) != arguments(target.get("value", "")):
errors.append(f"{key!r} {path}: format arguments differ")
project = (ROOT / "NotchBuddy/project.yml").read_text().split("\ntargets:\n", 1)[1]
for name in ("NotchBuddy", "CoucouAppStore"):
target = re.search(rf"^ {name}:\n(.*?)(?=^ \w+:|\Z)", project, re.M | re.S)
if not target or not re.search(r"^ - zh-Hant$", target[1], re.M):
errors.append(f"{name}: zh-Hant is missing from bundle localizations")
for path in (ROOT / "NotchBuddy/Sources").rglob("*.swift"):
if any(part in {"Phone", "Widgets", "NotificationContent"} for part in path.parts):
continue
for literal in re.findall(r'(?:String\(localized:\s*|(?:Text|Button|Toggle|SecureField|TextField|Picker|Label|GroupBox|help|accessibilityLabel|navigationTitle)\(\s*)"((?:\\.|[^"\\])*)"', path.read_text()):
if "\\(" in literal: # Xcode generates interpolation keys; format arguments are checked above.
continue
literal = re.sub(r"\\u\{([0-9a-fA-F]+)\}", lambda match: json.dumps(chr(int(match[1], 16)))[1:-1], literal)
key = json.loads('"' + literal + '"')
if key.strip() and not key.isdecimal() and key not in catalog["strings"]:
errors.append(f"{path.name}: localized key {key!r} is missing from the catalog")
if errors:
print("\n".join(errors), file=sys.stderr)
return 1
print(f"PASS: {len(catalog['strings'])} zh-Hant entries; plural paths and format arguments match; both macOS targets declare zh-Hant.")
return 0
if __name__ == "__main__":
sys.exit(main())