Repository navigation
localization: check Swift defaultValue literals against their catalog en value #16396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
teamleaderleo
merged 6 commits into
manaflow-ai:main
from
yuminn-k:fix/localization-default-parity
Oct 2, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60c32a0
localization: compare Swift defaultValue literals with their catalog …
yuminn-k b78769c
ci: run the Swift defaultValue parity check with the static recipe
yuminn-k 3c5e3c2
localization: keep intra-file default conflicts out of the defaultVal…
yuminn-k 361a35a
localization: skip a Swift default the signature parser rejects
yuminn-k c49aefe
Merge main (920ff39ff7cd) into fix/localization-default-parity
yuminn-k 731a3cb
Merge main (30226ce5bf26) into cx-bl-outside-prs-16396
teamleaderleo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "cloudTree.error.renameTerminalUnavailable": "Swift passes the terminal id to String(format:) but the catalog copy has no %@ and is terminal-scoped on a path that renames any tab kind; needs a copy decision (#15864).", | ||
| "remoteDaemon.upload.installFailedWithDetail": "Catalog copy ends in ': %@' but the Swift call site neither includes the specifier in its defaultValue nor formats the detail; fix the call site, then remove this entry (#15864).", | ||
| "remoteDaemon.bootstrap.removeCorruptFailedWithDetail": "Catalog copy ends in ': %@' but the Swift call site neither includes the specifier in its defaultValue nor formats the detail; fix the call site, then remove this entry (#15864)." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #!/usr/bin/env python3 | ||
| """Compare Swift `defaultValue` literals with the catalog English they localize (Python 3.9+). | ||
|
|
||
| `localization_catalog.py check` takes its baseline from the catalog itself: every | ||
| locale is compared with the entry's own `en` value. A catalog `en` value that lost | ||
| or gained a format specifier relative to the Swift `defaultValue` therefore passes, | ||
| because every locale agrees with `en` and nothing reads the Swift source. At | ||
| runtime `String(format:)` then silently drops the argument, or reads past the | ||
| argument list. This check reads the Swift source and compares the two signatures. | ||
|
|
||
| Known mismatches that are waiting on a copy decision live in | ||
| scripts/localization-default-mismatches.json, keyed by catalog key with a reason. | ||
| An entry that no longer mismatches is an error, so the list only shrinks. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import importlib.util | ||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
| ALLOWLIST = "scripts/localization-default-mismatches.json" | ||
| # Test targets and fixtures use catalog keys with their own default text on | ||
| # purpose; vendored and generated trees are not cmux copy. | ||
| SKIP_DIRS = frozenset(( | ||
| ".build", ".git", "DerivedData", "Examples", "Prototypes", "Tests", "artifacts", | ||
| "cmuxCLITestSupport", "cmuxCLITests", "cmuxTests", "cmuxUITests", "experiments", | ||
| "node_modules", "scripts", "tests", "tests_v2", "vendor", | ||
| )) | ||
|
|
||
|
|
||
| def load_changes_module(): | ||
| spec = importlib.util.spec_from_file_location("cmux_localize_changes", ROOT / "scripts/localize_changes.py") | ||
| if spec is None or spec.loader is None: | ||
| raise RuntimeError("unable to load scripts/localize_changes.py") | ||
| module = importlib.util.module_from_spec(spec) | ||
| sys.modules[spec.name] = module | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| CHANGES = load_changes_module() | ||
| CATALOG = CHANGES.CATALOG | ||
|
|
||
|
|
||
| def swift_files(root: Path) -> list[Path]: | ||
| return sorted(path for path in root.rglob("*.swift") | ||
| if SKIP_DIRS.isdisjoint(path.relative_to(root).parts[:-1])) | ||
|
|
||
|
|
||
| def swift_defaults(root: Path) -> dict[str, tuple[str, str]]: | ||
| """Map each key to (defaultValue, Swift path). | ||
|
|
||
| Call sites that disagree about a key's default are dropped, as are calls the | ||
| shared extractor cannot read (interpolated or unsupported literals); both are | ||
| already reported by localize_changes.py, and neither yields one signature. | ||
| """ | ||
| defaults: dict[str, tuple[str, str]] = {} | ||
| conflicts: set[str] = set() | ||
| for path in swift_files(root): | ||
| relative = path.relative_to(root).as_posix() | ||
| messages, _ = CHANGES.parse_swift_messages(relative, path.read_text(encoding="utf-8"), conflicts=conflicts) | ||
| for key, message in messages.items(): | ||
| previous = defaults.get(key) | ||
| if previous is not None and previous[0] != message.source: | ||
| conflicts.add(key) | ||
| else: | ||
| defaults[key] = (message.source, relative) | ||
| for key in conflicts: | ||
| defaults.pop(key, None) | ||
| return defaults | ||
|
|
||
|
|
||
| def catalog_index(root: Path) -> dict[str, list[tuple[str, object]]]: | ||
| index: dict[str, list[tuple[str, object]]] = {} | ||
| for path in CATALOG.discover(root): | ||
| for entry in CATALOG.catalog_entries(path.read_text(encoding="utf-8")): | ||
| index.setdefault(entry.key, []).append((path.relative_to(root).as_posix(), entry.value)) | ||
| return index | ||
|
|
||
|
|
||
| def load_allowlist(path: Path) -> dict[str, str]: | ||
| if not path.is_file(): | ||
| return {} | ||
| data = json.loads(path.read_text(encoding="utf-8")) | ||
| if not isinstance(data, dict) or not all(isinstance(reason, str) and reason.strip() for reason in data.values()): | ||
| raise ValueError(f"{path}: expected an object mapping catalog keys to a non-empty reason") | ||
| return data | ||
|
|
||
|
|
||
| def arguments(text: str) -> list[tuple[int, str]]: | ||
| """The (argument, specifier) pairs a format string consumes, in argument order.""" | ||
| return sorted(CATALOG.signature(text)) | ||
|
|
||
|
|
||
| def check(root: Path, allowlist: dict[str, str]) -> tuple[list[str], int]: | ||
| """Return (errors, comparison count); a key carried by several catalogs is compared with each.""" | ||
| errors: list[str] = [] | ||
| index = catalog_index(root) | ||
| compared = 0 | ||
| still_mismatched: set[str] = set() | ||
| for key, (default, swift_path) in sorted(swift_defaults(root).items()): | ||
| catalogs = [] | ||
| for catalog_path, entry in index.get(key, []): | ||
|
teamleaderleo marked this conversation as resolved.
|
||
| try: | ||
| catalogs.append((catalog_path, arguments(CATALOG.source(entry)))) | ||
| except ValueError: | ||
| continue # localization_catalog.py check already names a malformed entry | ||
| try: | ||
| expected = arguments(default) | ||
| except ValueError: | ||
| continue # e.g. a %#@name@ substitution in Swift text; localize_changes.py owns that review | ||
| for catalog_path, actual in catalogs: | ||
| compared += 1 | ||
| if expected == actual: | ||
| continue | ||
| if key in allowlist: | ||
| still_mismatched.add(key) | ||
| continue | ||
| # The same key can live in more than one product's catalog with | ||
| # different copy; name the siblings so the fix lands in the right one. | ||
| siblings = "".join(f"; {other} carries {sibling!r}" for other, sibling in catalogs if other != catalog_path) | ||
| errors.append( | ||
| f"{catalog_path}:{key}: catalog en placeholders {actual!r} != " | ||
| f"Swift defaultValue {expected!r} ({swift_path}){siblings}" | ||
| ) | ||
| for key in sorted(set(allowlist) - still_mismatched): | ||
| errors.append(f"{ALLOWLIST}: {key} no longer mismatches its Swift defaultValue; remove the entry") | ||
| return errors, compared | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | ||
| parser.add_argument("--root", type=Path, default=ROOT) | ||
| parser.add_argument("--allowlist", type=Path, help=f"default: <root>/{ALLOWLIST}") | ||
| parser.add_argument("--limit", type=int, default=40, help="maximum printed diagnostics; 0 prints all") | ||
| args = parser.parse_args(argv) | ||
| if args.limit < 0: | ||
| parser.error("limit must be nonnegative") | ||
| root = args.root.resolve() | ||
| errors, compared = check(root, load_allowlist(args.allowlist or root / ALLOWLIST)) | ||
| for error in errors[:args.limit or None]: | ||
| print(error, file=sys.stderr) | ||
| noun = "mismatch" if len(errors) == 1 else "mismatches" | ||
| print(f"{compared} Swift defaultValue/catalog en comparisons: {len(errors)} {noun}") | ||
| return int(bool(errors)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| raise SystemExit(main()) | ||
| except (OSError, ValueError, KeyError, TypeError) as error: | ||
| print(f"error: {error}", file=sys.stderr) | ||
| raise SystemExit(2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.