返回 CodeWhale
check-locales.mjs
根目录 / web / scripts / check-locales.mjs
1 #!/usr/bin/env node
2 /**
3 * check-locales.mjs — CI gate against website dictionary drift (#3091).
4 *
5 * web/lib/i18n/dictionaries/en/ is the reference. Every other locale
6 * directory must define chrome.ts and home.ts with exactly the same
7 * top-level keys, and every `{token}` template placeholder in the English
8 * values must survive translation (call sites interpolate with `fill()`,
9 * so a dropped token renders literal braces on the page).
10 *
11 * This is the dependency-free half of the gate; web/lib/i18n/dictionaries.test.ts
12 * covers the same contract through the real module imports.
13 *
14 * Exits non-zero on any parity violation.
15 */
16 import { readFileSync, readdirSync, existsSync } from "node:fs";
17 import { fileURLToPath } from "node:url";
18 import { join } from "node:path";
19
20 const ROOT = fileURLToPath(new URL("..", import.meta.url));
21 const DICT_DIR = join(ROOT, "lib", "i18n", "dictionaries");
22 const REFERENCE = "en";
23 const FILES = ["chrome.ts", "home.ts"];
24 // Per-page dictionaries (#5337): optional per locale. English is the
25 // required reference; a locale that ships the file is held to the same
26 // key/token parity, and a locale without it falls back to English at
27 // lookup time — so absence is a valid state, never a failure.
28 const OPTIONAL_FILES = [
29 "docs-guide.ts",
30 "docs-shell.ts",
31 "docs-hooks.ts",
32 "docs-troubleshooting.ts",
33 "docs-configuration.ts",
34 "docs-constitution.ts",
35 "docs-fleet.ts",
36 "docs-mcp.ts",
37 "docs-modes.ts",
38 "docs-runtime-api.ts",
39 "docs-sandbox.ts",
40 "docs-subagents.ts",
41 "docs-web.ts",
42 "docs-computers.ts",
43 "docs-auth.ts",
44 "docs-trust.ts",
45 "states.ts",
46 "changelog.ts",
47 "computer-use.ts",
48 ];
49
50 /** Top-level keys of the exported object literal (two-space indented `key:`). */
51 function extractKeys(source) {
52 return new Set(
53 [...source.matchAll(/^ {2}(\w+):/gm)].map((m) => m[1]),
54 );
55 }
56
57 function extractTokens(source) {
58 const tokens = new Map();
59 // Per-key token sets: walk `key: "…"` and array entries line by line.
60 let current = null;
61 for (const line of source.split("\n")) {
62 const keyMatch = line.match(/^ {2}(\w+):/);
63 if (keyMatch) current = keyMatch[1];
64 for (const t of line.matchAll(/\{(\w+)\}/g)) {
65 if (!current) continue;
66 if (!tokens.has(current)) tokens.set(current, new Set());
67 tokens.get(current).add(t[1]);
68 }
69 }
70 return tokens;
71 }
72
73 let failed = false;
74 const fail = (msg) => {
75 console.error(`[check-locales] FAIL — ${msg}`);
76 failed = true;
77 };
78
79 const locales = readdirSync(DICT_DIR, { withFileTypes: true })
80 .filter((d) => d.isDirectory() && d.name !== REFERENCE)
81 .map((d) => d.name)
82 .sort();
83
84 console.log(`[check-locales] reference ${REFERENCE}: ${FILES.join(", ")}`);
85 console.log(`[check-locales] locale dirs: ${locales.join(", ")}`);
86
87 /** Compare one locale file against the English reference (parity + tokens). */
88 function checkLocaleFile(locale, file, refKeys, refTokens) {
89 const path = join(DICT_DIR, locale, file);
90 const source = readFileSync(path, "utf8");
91 const keys = extractKeys(source);
92 const missing = [...refKeys].filter((k) => !keys.has(k));
93 const extra = [...keys].filter((k) => !refKeys.has(k));
94 if (missing.length) fail(`${locale}/${file}: missing keys: ${missing.join(", ")}`);
95 if (extra.length) fail(`${locale}/${file}: keys the reference lacks: ${extra.join(", ")}`);
96
97 const tokens = extractTokens(source);
98 for (const [key, refSet] of refTokens) {
99 const got = tokens.get(key) ?? new Set();
100 const dropped = [...refSet].filter((t) => !got.has(t));
101 if (dropped.length) {
102 fail(`${locale}/${file}: ${key} dropped template token(s): ${dropped.join(", ")}`);
103 }
104 }
105 if (!missing.length && !extra.length) {
106 console.log(`[check-locales] ${locale}/${file}: ${keys.size}/${refKeys.size} keys — complete`);
107 }
108 }
109
110 for (const file of [...FILES, ...OPTIONAL_FILES]) {
111 const required = FILES.includes(file);
112 const refPath = join(DICT_DIR, REFERENCE, file);
113 if (!existsSync(refPath)) {
114 fail(`${REFERENCE}/${file}: reference file missing`);
115 continue;
116 }
117 const refSource = readFileSync(refPath, "utf8");
118 const refKeys = extractKeys(refSource);
119 const refTokens = extractTokens(refSource);
120
121 for (const locale of locales) {
122 if (!existsSync(join(DICT_DIR, locale, file))) {
123 if (required) {
124 fail(`${locale}/${file}: missing (reference has ${refKeys.size} keys)`);
125 } else {
126 console.log(`[check-locales] ${locale}/${file}: absent — falls back to English`);
127 }
128 continue;
129 }
130 checkLocaleFile(locale, file, refKeys, refTokens);
131 }
132 }
133
134 if (failed) {
135 console.error("[check-locales] FAIL");
136 process.exit(1);
137 }
138 console.log("[check-locales] PASS");
139
139 lines Plain Text