| 1 | #!/usr/bin/env python3 |
| 2 | """Keep the string catalog whole and in Equipo's voice. |
| 3 | |
| 4 | scripts/strings.json is the English source, reviewed by Paul on |
| 5 | 2026-11-19; new or changed strings are draft until reviewed. This writes it into the String Catalog with --write, and checks: |
| 6 | every key the Swift sources use has English text, no text has an em dash |
| 7 | or an exclamation mark, and the catalog matches strings.json.""" |
| 8 | import json, re, sys |
| 9 | from pathlib import Path |
| 10 | |
| 11 | native = Path(__file__).resolve().parents[1] |
| 12 | source = json.loads((native / 'scripts/strings.json').read_text()) |
| 13 | # EquipoKit's catalog, and the same words for EquipoActivity, which the |
| 14 | # iPhone's widget extension draws the Live Activity with on its own. |
| 15 | catalog_paths = [native / 'apple/Sources' / target / 'Resources/Localizable.xcstrings' for target in ('EquipoKit', 'EquipoActivity')] |
| 16 | |
| 17 | catalog_paths.append(native / 'share/Sources/EquipoShareUI/Resources/Localizable.xcstrings') |
| 18 | |
| 19 | plurals = json.loads((native / 'scripts/plurals.json').read_text()) |
| 20 | |
| 21 | def catalog(): |
| 22 | return {'sourceLanguage': 'en', 'version': '1.0', 'strings': { |
| 23 | k: {**({'comment': ('COPY APPROVED: Luna, 2026-11-27.' if k == 'native.share.earlier_unconfirmed' or k.startswith('native.mac.') else 'COPY APPROVED: Luna, 2026-11-26.')} if k.startswith(('native.diff.', 'native.changes.', 'native.share.', 'native.screenshot.', 'native.mac.')) or k in ('api.preview_changed', 'api.preview_expired') else {}), 'extractionState': 'manual', 'localizations': {'en': plurals.get(k, {'stringUnit': {'state': 'translated', 'value': v}})}} |
| 24 | for k, v in sorted(source.items())}} |
| 25 | |
| 26 | def used_keys(): |
| 27 | keys = set() |
| 28 | for f in [*(native / 'apple/Sources').rglob('*.swift'), *(native / 'share/Sources').rglob('*.swift')]: |
| 29 | text = f.read_text() |
| 30 | for m in re.finditer(r'(?:String\(localized:|(?:EquipoStrings|ShareWords)\.text\()\s*"((?:[^"\\]|\\.|\\\([^)]*\))*)"', text): |
| 31 | raw = m.group(1) |
| 32 | # Interpolations become format specifiers: Int as %lld, the rest %@. |
| 33 | # A count (count, x.count, $0.count) is an Int. |
| 34 | raw = re.sub(r'\\\((?:[\w$]+\.)?count\)', '%lld', raw) |
| 35 | raw = re.sub(r'\\\((?:[^()]|\([^()]*\))*\)', '%@', raw) |
| 36 | keys.add(raw) |
| 37 | return keys |
| 38 | |
| 39 | problems = [] |
| 40 | # The plural catalog's ordinary forms must stay identical to the reviewed |
| 41 | # English source. Singular forms are exercised by native localization tests. |
| 42 | for key, value in plurals.items(): |
| 43 | ordinary = value['stringUnit']['value'] |
| 44 | for name, substitution in value['substitutions'].items(): |
| 45 | ordinary = ordinary.replace('%#@' + name + '@', substitution['variations']['plural']['other']['stringUnit']['value']) |
| 46 | ordinary = re.sub(r'%[0-9]+\$', '%', ordinary) |
| 47 | if ordinary != source.get(key): |
| 48 | problems.append(f'{key}: plural catalog differs from reviewed source') |
| 49 | for match in re.findall(r'"value": "([^"]*)"', json.dumps(value, ensure_ascii=False)): |
| 50 | if '—' in match or '!' in match: problems.append(f'{key}: unreviewed punctuation in plural form') |
| 51 | |
| 52 | for k in sorted(used_keys() - source.keys()): problems.append(f'no English text for key: {k}') |
| 53 | for k, v in source.items(): |
| 54 | if '—' in v: problems.append(f'em dash in {k}') |
| 55 | if '!' in v: problems.append(f'exclamation mark in {k}') |
| 56 | for catalog_path in catalog_paths: |
| 57 | if '--write' in sys.argv: |
| 58 | catalog_path.write_text(json.dumps(catalog(), indent=2, ensure_ascii=False) + '\n') |
| 59 | elif not catalog_path.is_file() or json.loads(catalog_path.read_text()) != catalog(): |
| 60 | problems.append(f'{catalog_path.relative_to(native)} is out of date: run native/scripts/check-strings.py --write') |
| 61 | for p in problems: print(p) |
| 62 | if problems: sys.exit(1) |
| 63 | # Facts look their words up by a key made from the wire value |
| 64 | # (TaskReport.facts); FactWordsTests checks each one resolves. |
| 65 | computed = {k for k in source if k.startswith(('checks.', 'review.', 'chat.card.', 'work.tl.', 'work.reason.', 'work.needs.'))} |
| 66 | unused = sorted(source.keys() - used_keys() - computed) |
| 67 | print(f'{len(source)} strings, all used keys present' + (f'; unused: {unused}' if unused else '')) |
| 68 | |