| 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 | |
| 25 | /** Top-level keys of the exported object literal (two-space indented `key:`). */ |
| 26 | function extractKeys(source) { |
| 27 | return new Set( |
| 28 | [...source.matchAll(/^ {2}(\w+):/gm)].map((m) => m[1]), |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | function extractTokens(source) { |
| 33 | const tokens = new Map(); |
| 34 | // Per-key token sets: walk `key: "…"` and array entries line by line. |
| 35 | let current = null; |
| 36 | for (const line of source.split("\n")) { |
| 37 | const keyMatch = line.match(/^ {2}(\w+):/); |
| 38 | if (keyMatch) current = keyMatch[1]; |
| 39 | for (const t of line.matchAll(/\{(\w+)\}/g)) { |
| 40 | if (!current) continue; |
| 41 | if (!tokens.has(current)) tokens.set(current, new Set()); |
| 42 | tokens.get(current).add(t[1]); |
| 43 | } |
| 44 | } |
| 45 | return tokens; |
| 46 | } |
| 47 | |
| 48 | let failed = false; |
| 49 | const fail = (msg) => { |
| 50 | console.error(`[check-locales] FAIL — ${msg}`); |
| 51 | failed = true; |
| 52 | }; |
| 53 | |
| 54 | const locales = readdirSync(DICT_DIR, { withFileTypes: true }) |
| 55 | .filter((d) => d.isDirectory() && d.name !== REFERENCE) |
| 56 | .map((d) => d.name) |
| 57 | .sort(); |
| 58 | |
| 59 | console.log(`[check-locales] reference ${REFERENCE}: ${FILES.join(", ")}`); |
| 60 | console.log(`[check-locales] locale dirs: ${locales.join(", ")}`); |
| 61 | |
| 62 | for (const file of FILES) { |
| 63 | const refPath = join(DICT_DIR, REFERENCE, file); |
| 64 | const refSource = readFileSync(refPath, "utf8"); |
| 65 | const refKeys = extractKeys(refSource); |
| 66 | const refTokens = extractTokens(refSource); |
| 67 | |
| 68 | for (const locale of locales) { |
| 69 | const path = join(DICT_DIR, locale, file); |
| 70 | if (!existsSync(path)) { |
| 71 | fail(`${locale}/${file}: missing (reference has ${refKeys.size} keys)`); |
| 72 | continue; |
| 73 | } |
| 74 | const source = readFileSync(path, "utf8"); |
| 75 | const keys = extractKeys(source); |
| 76 | const missing = [...refKeys].filter((k) => !keys.has(k)); |
| 77 | const extra = [...keys].filter((k) => !refKeys.has(k)); |
| 78 | if (missing.length) fail(`${locale}/${file}: missing keys: ${missing.join(", ")}`); |
| 79 | if (extra.length) fail(`${locale}/${file}: keys the reference lacks: ${extra.join(", ")}`); |
| 80 | |
| 81 | const tokens = extractTokens(source); |
| 82 | for (const [key, refSet] of refTokens) { |
| 83 | const got = tokens.get(key) ?? new Set(); |
| 84 | const dropped = [...refSet].filter((t) => !got.has(t)); |
| 85 | if (dropped.length) { |
| 86 | fail(`${locale}/${file}: ${key} dropped template token(s): ${dropped.join(", ")}`); |
| 87 | } |
| 88 | } |
| 89 | if (!missing.length && !extra.length) { |
| 90 | console.log(`[check-locales] ${locale}/${file}: ${keys.size}/${refKeys.size} keys — complete`); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (failed) { |
| 96 | console.error("[check-locales] FAIL"); |
| 97 | process.exit(1); |
| 98 | } |
| 99 | console.log("[check-locales] PASS"); |
| 100 |