返回 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-fleet.ts",
35 "docs-mcp.ts",
36 "docs-modes.ts",
37 "docs-runtime-api.ts",
38 "docs-sandbox.ts",
39 "docs-subagents.ts",
40 "docs-web.ts",
41 "docs-computers.ts",
42 "docs-auth.ts",
43 "docs-trust.ts",
44 "docs-work.ts",
45 "docs-vocabulary.ts",
46 "states.ts",
47 "changelog.ts",
48 "legal-terms.ts",
49 "legal-privacy.ts",
50 "digest.ts",
51 "feed.ts",
52 "community.ts",
53 "faq.ts",
54 "roadmap.ts",
55 "contribute.ts",
56 "constitution.ts",
57 "runtime.ts",
58 "computer-use.ts",
59 ];
60
61 /** Top-level keys of the exported object literal (two-space indented `key:`). */
62 function extractKeys(source) {
63 return new Set(
64 [...source.matchAll(/^ {2}(\w+):/gm)].map((m) => m[1]),
65 );
66 }
67
68 function extractTokens(source) {
69 const tokens = new Map();
70 // Per-key token sets: walk `key: "…"` and array entries line by line.
71 let current = null;
72 for (const line of source.split("\n")) {
73 const keyMatch = line.match(/^ {2}(\w+):/);
74 if (keyMatch) current = keyMatch[1];
75 for (const t of line.matchAll(/\{(\w+)\}/g)) {
76 if (!current) continue;
77 if (!tokens.has(current)) tokens.set(current, new Set());
78 tokens.get(current).add(t[1]);
79 }
80 }
81 return tokens;
82 }
83
84 let failed = false;
85 const fail = (msg) => {
86 console.error(`[check-locales] FAIL — ${msg}`);
87 failed = true;
88 };
89
90 const locales = readdirSync(DICT_DIR, { withFileTypes: true })
91 .filter((d) => d.isDirectory() && d.name !== REFERENCE)
92 .map((d) => d.name)
93 .sort();
94
95 console.log(`[check-locales] reference ${REFERENCE}: ${FILES.join(", ")}`);
96 console.log(`[check-locales] locale dirs: ${locales.join(", ")}`);
97
98 /** Compare one locale file against the English reference (parity + tokens). */
99 function checkLocaleFile(locale, file, refKeys, refTokens) {
100 const path = join(DICT_DIR, locale, file);
101 const source = readFileSync(path, "utf8");
102 const keys = extractKeys(source);
103 const missing = [...refKeys].filter((k) => !keys.has(k));
104 const extra = [...keys].filter((k) => !refKeys.has(k));
105 if (missing.length) fail(`${locale}/${file}: missing keys: ${missing.join(", ")}`);
106 if (extra.length) fail(`${locale}/${file}: keys the reference lacks: ${extra.join(", ")}`);
107
108 const tokens = extractTokens(source);
109 for (const [key, refSet] of refTokens) {
110 const got = tokens.get(key) ?? new Set();
111 const dropped = [...refSet].filter((t) => !got.has(t));
112 if (dropped.length) {
113 fail(`${locale}/${file}: ${key} dropped template token(s): ${dropped.join(", ")}`);
114 }
115 }
116 if (!missing.length && !extra.length) {
117 console.log(`[check-locales] ${locale}/${file}: ${keys.size}/${refKeys.size} keys — complete`);
118 }
119 }
120
121 for (const file of [...FILES, ...OPTIONAL_FILES]) {
122 const required = FILES.includes(file);
123 const refPath = join(DICT_DIR, REFERENCE, file);
124 if (!existsSync(refPath)) {
125 fail(`${REFERENCE}/${file}: reference file missing`);
126 continue;
127 }
128 const refSource = readFileSync(refPath, "utf8");
129 const refKeys = extractKeys(refSource);
130 const refTokens = extractTokens(refSource);
131
132 for (const locale of locales) {
133 if (!existsSync(join(DICT_DIR, locale, file))) {
134 if (required) {
135 fail(`${locale}/${file}: missing (reference has ${refKeys.size} keys)`);
136 } else {
137 console.log(`[check-locales] ${locale}/${file}: absent — falls back to English`);
138 }
139 continue;
140 }
141 checkLocaleFile(locale, file, refKeys, refTokens);
142 }
143 }
144
145 if (failed) {
146 console.error("[check-locales] FAIL");
147 process.exit(1);
148 }
149 console.log("[check-locales] PASS");
150
150 lines Plain Text