check-i18n-parity.mjs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // Verifies parity across locale files (en / de / fr / it / ja / pt-BR / zh-CN / zh-TW):
  2. // 1. Leaf-key sets are identical
  3. // 2. Each leaf's {{placeholder}} set is identical
  4. // 3. Plural suffixes: every en key ending in _plural / _one / _other must
  5. // exist in every other locale, and other locales must not introduce an
  6. // _one key that en does not have.
  7. // 4. NEW: leaves in a non-English locale must not be identical to en, unless
  8. // the value is a brand name / technical token / pure punctuation, OR the
  9. // key+locale pair is explicitly listed in IDENTICAL_TO_EN_ALLOWED below.
  10. // Catches the "copy English text into non-English locale to satisfy the
  11. // key-count parity gate" anti-pattern that accumulated 700+ shipped
  12. // strings of debt before the gate was tightened. Add an explicit entry
  13. // ONLY when the string is a real word/term in that target locale.
  14. // Malformed input (missing `export default`, parse errors, non-string leaves,
  15. // unsupported property kinds) fails loudly instead of silently passing the gate.
  16. // Exits 1 with a diagnostic report on any failure, else exits 0.
  17. import fs from 'node:fs';
  18. import path from 'node:path';
  19. import url from 'node:url';
  20. const scriptDir = path.dirname(url.fileURLToPath(import.meta.url));
  21. const frontendDir = path.resolve(scriptDir, '..');
  22. const localesDir = path.join(frontendDir, 'src/i18n/locales');
  23. const tsPath = path.join(frontendDir, 'node_modules/typescript/lib/typescript.js');
  24. const tsModule = await import(url.pathToFileURL(tsPath).href);
  25. const ts = tsModule.default ?? tsModule;
  26. function collectLeaves(node, prefix, leaves) {
  27. if (!ts.isObjectLiteralExpression(node)) return;
  28. for (const prop of node.properties) {
  29. if (!ts.isPropertyAssignment(prop)) {
  30. console.error(
  31. `Unsupported property kind ${ts.SyntaxKind[prop.kind]} at "${prefix}" ` +
  32. `(locale files must use plain \`key: value\` assignments — no spread, shorthand, methods, or accessors).`,
  33. );
  34. process.exit(1);
  35. }
  36. let name;
  37. if (ts.isIdentifier(prop.name)) name = prop.name.text;
  38. else if (ts.isStringLiteral(prop.name) || ts.isNoSubstitutionTemplateLiteral(prop.name)) name = prop.name.text;
  39. else if (ts.isComputedPropertyName(prop.name)) {
  40. console.error(`ComputedPropertyName not allowed in locale file at path "${prefix}"`);
  41. process.exit(1);
  42. } else {
  43. console.error(`Unsupported property-name kind ${ts.SyntaxKind[prop.name.kind]} at "${prefix}"`);
  44. process.exit(1);
  45. }
  46. const p = prefix ? `${prefix}.${name}` : name;
  47. if (ts.isObjectLiteralExpression(prop.initializer)) {
  48. collectLeaves(prop.initializer, p, leaves);
  49. } else {
  50. const value = extractStringValue(prop.initializer, p);
  51. leaves.set(p, value);
  52. }
  53. }
  54. }
  55. function extractStringValue(node, keyPath) {
  56. if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
  57. if (ts.isTemplateExpression(node)) {
  58. let out = node.head.text;
  59. for (const span of node.templateSpans) {
  60. out += '${' + span.expression.getText() + '}';
  61. out += span.literal.text;
  62. }
  63. return out;
  64. }
  65. console.error(
  66. `Non-string leaf at "${keyPath}" (kind=${ts.SyntaxKind[node.kind]}): ${node.getText()}\n` +
  67. `Locale files must only contain string or template literals as leaf values.`,
  68. );
  69. process.exit(1);
  70. }
  71. function loadLocale(filePath) {
  72. const src = fs.readFileSync(filePath, 'utf8');
  73. const sf = ts.createSourceFile(filePath, src, ts.ScriptTarget.Latest, true);
  74. if (sf.parseDiagnostics && sf.parseDiagnostics.length > 0) {
  75. console.error(`${filePath}: ${sf.parseDiagnostics.length} parse error(s):`);
  76. for (const d of sf.parseDiagnostics.slice(0, 10)) {
  77. const msg = typeof d.messageText === 'string' ? d.messageText : d.messageText.messageText;
  78. const { line, character } = sf.getLineAndCharacterOfPosition(d.start ?? 0);
  79. console.error(` ${line + 1}:${character + 1} ${msg}`);
  80. }
  81. process.exit(1);
  82. }
  83. const leaves = new Map();
  84. let foundExport = false;
  85. ts.forEachChild(sf, (n) => {
  86. if (ts.isExportAssignment(n)) {
  87. foundExport = true;
  88. collectLeaves(n.expression, '', leaves);
  89. }
  90. });
  91. if (!foundExport) {
  92. console.error(`${filePath}: no \`export default\` found — locale files must use \`export default { ... }\`.`);
  93. process.exit(1);
  94. }
  95. if (leaves.size === 0) {
  96. console.error(`${filePath}: \`export default\` resolved to zero leaves — file is empty or not a nested object.`);
  97. process.exit(1);
  98. }
  99. return leaves;
  100. }
  101. const placeholderRe = /\{\{[^{}]+\}\}/g;
  102. // Heuristic: values that are ALWAYS allowed to match en, regardless of locale.
  103. // Brand names, technical tokens, pure punctuation, very short strings, version
  104. // numbers, hex codes, and ALL-CAPS acronyms. Cognates that happen to be the
  105. // same word in a specific locale go in IDENTICAL_TO_EN_ALLOWED instead.
  106. function isAlwaysAllowedIdentical(value) {
  107. if (!value) return true;
  108. if (/^[\s\W_]+$/.test(value)) return true; // pure punctuation/whitespace
  109. if (value.length <= 2) return true; // single character or 2-char abbrev
  110. if (/^[A-Z][A-Z0-9_]+$/.test(value)) return true; // ALL_CAPS_TOKEN
  111. if (/^v?\d+(\.\d+)+/.test(value)) return true; // version-like
  112. if (/^#[0-9a-fA-F]{3,8}$/.test(value)) return true; // hex color
  113. if (/^\{\{[^}]+\}\}$/.test(value)) return true; // pure placeholder
  114. if (/^[0-9a-fA-F]{6}$/.test(value)) return true; // bare hex color
  115. if (/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i.test(value)) return true; // email
  116. if (/^https?:\/\//.test(value)) return true; // URL
  117. if (/^ON,\s+true,\s+1$/.test(value)) return true; // literal example "ON, true, 1"
  118. // Brand / technical names that ship verbatim everywhere.
  119. if (/^(Bambuddy|BamBuddy|SpoolBuddy|Bambu Lab|Bambu Studio|Bambu Studio 2\.6\+|Bambu Studio sidecar URL|OrcaSlicer|OrcaSlicer sidecar URL|MakerWorld|Spoolman|\(Spoolman\)|Spoolman URL|Tailscale|GitHub|GitLab|Gitea|Forgejo|Discord|MQTT|FTP|HTTPS?|JSON|YAML|RTSP|TLS|SSL|CSRF|OIDC|SSO|SSO \/ OIDC|LDAP|TOTP|2FA|MFA|API|AMS|CRC|SHA256|kWh|MB|GB|KB|RGBA?|HSL|RGB|UTC|ISO|UI|HTTP|HTTP Method|H2D|H2D Pro|X1C|X1E|P1S|P1P|A1|A1 Mini|H2C|N3F|N3S|PETG|PLA|ABS|PA|TPU|PEI|PA-CF|PVA|HIPS|ASA|PC|PETG-HF|G\.code|G-code|gcode|cm³|°C|°F|GCODE|SOURCE|ntfy|Pushover|Telegram|Webhook|Webhook URL|Home Assistant|Home Assistant URL|CallMeBot\/WhatsApp|Bambuddy URL|Cool Plate|Cool Plate SuperTack|Engineering Plate|High Temp Plate|Smooth PEI Plate|Textured PEI Plate|Ext-L|Ext-R|ISO \(YYYY-MM-DD\))$/.test(value)) return true;
  120. return false;
  121. }
  122. // Per-(locale, value) allow-list for strings that are a real word/term in
  123. // that target locale and so legitimately match en.ts. Curated — add an entry
  124. // here ONLY after verifying that the word is correct (not just a shortcut to
  125. // silence the check).
  126. //
  127. // Convention: same shape as the locales themselves — { de: Set, fr: Set, ... }.
  128. // Values are matched exactly. To allow a value across many locales, list it in
  129. // each one (verbosity is the point: every locale's allow-list is an explicit
  130. // translator decision).
  131. // German loanwords / cognates from English are extensive. Most short technical
  132. // UI labels are identical in DE. List below curates the legitimate ones.
  133. const DE_COGNATES = [
  134. 'Name', 'Status', 'Tag', 'Tags', 'Online', 'Offline', 'Standard', 'Modus',
  135. 'Stop', 'Reset', 'Test', 'Code', 'Token', 'Server', 'Port', 'Bug', 'Job',
  136. 'Pause', 'Power', 'System', 'Problem', 'Designer', 'Extruder', 'Firmware',
  137. 'Material', 'Original', 'Position', 'Webhook', 'Workflow', 'Slicer',
  138. 'Region', 'Normal', 'Orange', 'Branch', 'Budget', 'Commit', 'Global',
  139. 'Version', 'Slot', 'Live', 'Rate', 'Host', 'Trend', 'Min', 'Admin', 'Cloud',
  140. 'Filament', 'Filaments', 'Software', 'Hardware', 'Avatar', 'Pin', 'Modal',
  141. 'Active', 'Plate', 'Layer', 'Total', 'Plus', 'Pro', 'Mini', 'Studio',
  142. 'Temperatur', 'Process', 'Service', 'Cache', 'Color', 'Login', 'Logout',
  143. 'Action', 'Description', 'Sender', 'Setup', 'Bundle', 'Cluster', 'Tier',
  144. 'Standard (100%)', 'Sport (124%)', 'Ludicrous (166%)',
  145. 'Smart Plugs', 'Smart Switches', 'Smart Plug', 'High Flow',
  146. 'Optional', 'optional', 'Filter', 'Filters', 'optional)',
  147. 'Material:', 'Default:', 'Name *', '(System)', '(Inv)',
  148. 'Spoolman URL', 'Bundle', 'Slicer Bundles', 'Imported',
  149. 'STARTTLS (Port 587)', 'SSL/TLS (Port 465)', 'Sport', 'Standard',
  150. 'EC984C,#6CD4BC,A66EB9,D87694',
  151. 'Hex', 'Warm', 'Neutral', 'Navigation', 'Screenshot', 'Architecture',
  152. 'Backend & Auth', 'Stream Overlay', 'Bambuddy Backend URL',
  153. 'Material (optional)', 'Custom Headers (JSON)', '({{count}}/8)',
  154. 'AMS holder (30 × 15 mm)', 'Box label (62 × 29 mm)',
  155. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  156. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  157. 'China', 'Proxy', 'Start',
  158. ];
  159. // French cognates — many UI labels overlap with English exactly.
  160. const FR_COGNATES = [
  161. 'Status', 'Tag', 'Tags', 'Online', 'Offline', 'Standard', 'Filament',
  162. 'Filaments', 'Software', 'Hardware', 'Stop', 'Reset', 'Test', 'Code',
  163. 'Token', 'Server', 'Port', 'Plate', 'Layer', 'Active', 'Total', 'Avatar',
  164. 'Job', 'Modal', 'Pin', 'Pro', 'Mini', 'Studio', 'Excellent', 'Description',
  165. 'Action', 'Actions', 'Date', 'Type', 'Cache', 'Service', 'Configuration',
  166. 'Archives', 'Maintenance', 'Notifications', 'Notification', 'Position',
  167. 'Pause', 'Solution', 'Source', 'Version', 'Format', 'Documentation',
  168. 'Mode', 'Format', 'Default', 'Auto', 'Image', 'Audio', 'Video', 'Hex',
  169. 'Camera', 'Avatar', 'Information', 'Initialization', 'Inactive', 'Active',
  170. 'Print', 'Console', 'Cluster', 'Tier', 'Status URL',
  171. 'Smart Plugs', 'Smart Switches', 'Smart Plug', 'High Flow',
  172. 'Material:', 'Default:', 'Name *', '(System)', '(Inv)',
  173. 'Process', 'Service', 'Service', 'Connect', 'Network', 'Local',
  174. 'Sport (124%)', 'Ludicrous (166%)', 'Standard (100%)',
  175. 'STARTTLS (Port 587)', 'SSL/TLS (Port 465)',
  176. 'Bundle', 'Slicer Bundles', 'Imported',
  177. 'Page', 'Note', 'Tare', 'Est.', 'Cloud', 'Style', 'Notes', 'Stock',
  178. 'Accent', 'Orange', 'Global', 'Stable', 'Archive', 'visible', 'minutes',
  179. 'Message', 'Slicer', 'Rotation', 'Original', 'Direction', 'Architecture',
  180. 'notifications', 'Maintenance OK', 'total', 'Provider', 'Token name',
  181. '{{count}} filament', '{{count}} filaments', '{{count}} permissions',
  182. '{{count}} downloads', '{{count}} item', '{{count}} selected',
  183. '({{count}} item)', 'Provisioning...', 'Pressure Advance',
  184. 'AMS holder (30 × 15 mm)', 'Box label (62 × 29 mm)',
  185. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  186. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  187. '({{count}}/8)', 'Custom Headers (JSON)', 'Permissions',
  188. 'Expand dispatch details', 'Collapse dispatch details',
  189. 'Cancelling upload...', 'Backup in progress...', 'Searching directory...',
  190. 'EC984C,#6CD4BC,A66EB9,D87694',
  191. 'Proxy', 'Navigation', 'Budget', 'Commit', 'Designer',
  192. 'ntfy, Pushover, Discord, etc.',
  193. ];
  194. // Italian cognates.
  195. const IT_COGNATES = [
  196. 'Status', 'Tag', 'Tags', 'Online', 'Offline', 'Standard', 'Filament',
  197. 'Filaments', 'Software', 'Hardware', 'Stop', 'Reset', 'Test', 'Code',
  198. 'Token', 'Server', 'Port', 'Plate', 'Layer', 'Modal', 'Pin', 'Pro', 'Mini',
  199. 'Studio', 'Cache', 'Service', 'Avatar', 'Slicer', 'Action', 'Actions',
  200. 'Format', 'Modal', 'Login', 'Logout', 'Color', 'Plus', 'Job', 'Live',
  201. 'Position', 'Original', 'Material', 'Cluster', 'Tier', 'Auto', 'Hex',
  202. 'Bundle', 'Slicer Bundles', 'Imported', 'Smart Plugs', 'Smart Switches',
  203. 'Smart Plug', 'High Flow', 'Sport (124%)', 'Ludicrous (166%)',
  204. 'Standard (100%)', 'STARTTLS (Port 587)', 'SSL/TLS (Port 465)',
  205. 'Slot', 'Host', 'File', 'Cloud', 'Admin', 'Silk', '(Inv)', 'Slice',
  206. 'Backup', 'Legacy', 'Branch', 'Auto On', 'Display', 'Password',
  207. 'Auto Off', 'Dashboard', 'Timestamp', 'Pressure Advance', 'Provisioning...',
  208. '(25%, 50%, 75%)', 'Provider', 'Provider: {{type}}', 'Base: {{name}}',
  209. 'Slicing…', 'Designer', 'Firmware', 'Timelapse', 'Commit', 'Budget',
  210. '({{count}}/8)', 'Custom Headers (JSON)', 'ETA {{minutes}} min',
  211. '{{name}} - Timelapse', 'Box label (62 × 29 mm)',
  212. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  213. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  214. 'AMS holder (30 × 15 mm)', 'Hex: #{{hex}}',
  215. 'EC984C,#6CD4BC,A66EB9,D87694',
  216. 'Proxy', 'Designer',
  217. ];
  218. // Japanese: very few cognates because of script difference. Almost
  219. // everything needs translation. Only true loanwords / proper nouns stay.
  220. const JA_COGNATES = [
  221. 'OK', 'Bambu', 'Code',
  222. 'EU (DD/MM/YYYY)', 'US (MM/DD/YYYY)', 'ON, true, 1',
  223. '({{count}}/8)', 'Custom Headers (JSON)',
  224. 'AMS holder (30 × 15 mm)', 'Box label (62 × 29 mm)',
  225. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  226. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  227. 'EC984C,#6CD4BC,A66EB9,D87694',
  228. ];
  229. // Portuguese (BR) cognates.
  230. const PT_BR_COGNATES = [
  231. 'Status', 'Tag', 'Tags', 'Online', 'Offline', 'Standard', 'Filament',
  232. 'Software', 'Hardware', 'Stop', 'Reset', 'Test', 'Code', 'Token', 'Server',
  233. 'Port', 'Plate', 'Layer', 'Modal', 'Pin', 'Pro', 'Mini', 'Studio', 'Cache',
  234. 'Service', 'Avatar', 'Total', 'Active', 'Login', 'Logout', 'Color', 'Hex',
  235. 'Slot', 'Live', 'Rate', 'Host', 'Trend', 'Original', 'Auto', 'Bundle',
  236. 'Imported', 'Action', 'Actions', 'Slicer Bundles', 'Sport (124%)',
  237. 'Ludicrous (166%)', 'Standard (100%)', 'STARTTLS (Port 587)',
  238. 'SSL/TLS (Port 465)', 'Smart Plugs', 'Smart Switches', 'High Flow',
  239. 'Position', 'Mode', 'Setup', 'Modal',
  240. 'Local', 'Metal', 'China', 'Admin', 'Silk', 'Backup', '(Inv)', 'Branch',
  241. 'Normal', 'Material', 'Material:', 'Multicolor', 'Designer', 'Firmware',
  242. 'Timelapse', 'Est.', 'total', 'Commit', 'Global',
  243. 'Base: {{name}}', 'ETA {{minutes}} min', '{{count}} item',
  244. '{{count}} downloads', '({{count}} item)', '(25%, 50%, 75%)',
  245. '({{count}}/8)', 'Custom Headers (JSON)', '{{name}} - Timelapse',
  246. 'AMS holder (30 × 15 mm)', 'Box label (62 × 29 mm)',
  247. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  248. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  249. 'Cancelling upload...', 'EC984C,#6CD4BC,A66EB9,D87694',
  250. 'Expand dispatch details', 'Collapse dispatch details',
  251. 'e.g., Home Assistant, OctoPrint', 'ntfy, Pushover, Discord, etc.',
  252. 'Proxy', 'total: {{minutes}} min',
  253. ];
  254. // Chinese (Simplified): very few cognates beyond brand names.
  255. const ZH_CN_COGNATES = [
  256. 'OK', 'Bambu',
  257. '({{count}}/8)', 'Custom Headers (JSON)', 'AMS holder (30 × 15 mm)',
  258. 'Box label (62 × 29 mm)',
  259. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  260. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  261. 'EC984C,#6CD4BC,A66EB9,D87694',
  262. ];
  263. const ZH_TW_COGNATES = [
  264. 'OK', 'Bambu',
  265. '({{count}}/8)', 'Custom Headers (JSON)', 'AMS holder (30 × 15 mm)',
  266. 'Box label (62 × 29 mm)',
  267. 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  268. 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  269. 'EC984C,#6CD4BC,A66EB9,D87694',
  270. ];
  271. const IDENTICAL_TO_EN_ALLOWED = {
  272. de: new Set(DE_COGNATES),
  273. fr: new Set(FR_COGNATES),
  274. it: new Set(IT_COGNATES),
  275. ja: new Set(JA_COGNATES),
  276. 'pt-BR': new Set(PT_BR_COGNATES),
  277. 'zh-CN': new Set(ZH_CN_COGNATES),
  278. 'zh-TW': new Set(ZH_TW_COGNATES),
  279. };
  280. // Pure comparison logic, exported so tests can verify each failure mode
  281. // without going through file IO or the TypeScript parser.
  282. // Input: locales = { code: Map<leafKey, leafString> } (must contain 'en')
  283. // Output: { failed, reports: Array<{ label, items }> }
  284. export function compareLocales(locales) {
  285. if (!locales.en) throw new Error("compareLocales requires a locales.en entry");
  286. const reports = [];
  287. const add = (label, items) => {
  288. if (items.length) reports.push({ label, items });
  289. };
  290. const enKeys = new Set(locales.en.keys());
  291. // Check 1: key set equality
  292. for (const [code, map] of Object.entries(locales)) {
  293. if (code === 'en') continue;
  294. const keys = new Set(map.keys());
  295. const missing = [...enKeys].filter((k) => !keys.has(k)).sort();
  296. const extra = [...keys].filter((k) => !enKeys.has(k)).sort();
  297. add(`${code}: missing keys vs en`, missing);
  298. add(`${code}: extra keys vs en`, extra);
  299. }
  300. // Check 2: placeholder set equality per leaf
  301. for (const [code, map] of Object.entries(locales)) {
  302. if (code === 'en') continue;
  303. const mismatches = [];
  304. for (const [key, enValue] of locales.en) {
  305. const otherValue = map.get(key);
  306. if (otherValue === undefined) continue;
  307. const enPlaceholders = new Set((enValue.match(placeholderRe) ?? []));
  308. const otherPlaceholders = new Set((otherValue.match(placeholderRe) ?? []));
  309. const missingPh = [...enPlaceholders].filter((p) => !otherPlaceholders.has(p));
  310. const extraPh = [...otherPlaceholders].filter((p) => !enPlaceholders.has(p));
  311. if (missingPh.length || extraPh.length) {
  312. mismatches.push(`${key}: en=${[...enPlaceholders].join(',') || '∅'} vs ${code}=${[...otherPlaceholders].join(',') || '∅'}`);
  313. }
  314. }
  315. add(`${code}: placeholder mismatch vs en`, mismatches);
  316. }
  317. // Check 3: plural suffix presence + reverse _one guard
  318. for (const [code, map] of Object.entries(locales)) {
  319. if (code === 'en') continue;
  320. const pluralIssues = [];
  321. for (const key of enKeys) {
  322. if (key.endsWith('_plural') && !map.has(key)) pluralIssues.push(`missing _plural key: ${key}`);
  323. if (key.endsWith('_one') && !map.has(key)) pluralIssues.push(`missing _one key: ${key}`);
  324. if (key.endsWith('_other') && !map.has(key)) pluralIssues.push(`missing _other key: ${key}`);
  325. }
  326. for (const key of map.keys()) {
  327. if (key.endsWith('_one') && !enKeys.has(key)) {
  328. pluralIssues.push(`unexpected _one not present in en: ${key}`);
  329. }
  330. }
  331. add(`${code}: plural key mismatch`, pluralIssues);
  332. }
  333. // Check 4: identical-to-en leaks. A non-English leaf whose value exactly
  334. // matches en.ts must either pass the always-allowed heuristic OR be listed
  335. // in IDENTICAL_TO_EN_ALLOWED[code]. Otherwise it's almost certainly an
  336. // untranslated English string that slipped through past parity gates.
  337. for (const [code, map] of Object.entries(locales)) {
  338. if (code === 'en') continue;
  339. const allowed = IDENTICAL_TO_EN_ALLOWED[code] ?? new Set();
  340. const leaks = [];
  341. for (const [key, enValue] of locales.en) {
  342. const localeValue = map.get(key);
  343. if (localeValue === undefined) continue;
  344. if (localeValue !== enValue) continue;
  345. if (isAlwaysAllowedIdentical(enValue)) continue;
  346. if (allowed.has(enValue)) continue;
  347. const preview = enValue.length > 60 ? `${enValue.slice(0, 57)}...` : enValue;
  348. leaks.push(`${key}: "${preview}"`);
  349. }
  350. add(`${code}: leaves identical to en (untranslated?)`, leaks);
  351. }
  352. return { failed: reports.length > 0, reports };
  353. }
  354. // en is the reference locale; every other locale discovered in the locales
  355. // directory is checked identically and a drift in any of them fails CI.
  356. // Skip file IO / process.exit when imported as a library (e.g. from tests).
  357. const isMainModule = import.meta.url === url.pathToFileURL(process.argv[1] ?? '').href;
  358. if (isMainModule) {
  359. const discovered = fs
  360. .readdirSync(localesDir)
  361. .filter((f) => f.endsWith('.ts'))
  362. .map((f) => f.slice(0, -3))
  363. .sort();
  364. if (!discovered.includes('en')) {
  365. console.error(`No en.ts found in ${localesDir} — cannot run parity check without a reference locale.`);
  366. process.exit(1);
  367. }
  368. const codes = ['en', ...discovered.filter((c) => c !== 'en')];
  369. const locales = Object.fromEntries(
  370. codes.map((c) => [c, loadLocale(path.join(localesDir, `${c}.ts`))]),
  371. );
  372. const MAX_REPORT = 20;
  373. const { reports } = compareLocales(locales);
  374. if (reports.length) {
  375. console.error(`\n=== Locale parity failures (en is the reference) ===`);
  376. for (const { label, items } of reports) {
  377. console.error(`\n[${label}] (${items.length})`);
  378. items.slice(0, MAX_REPORT).forEach((i) => console.error(` ${i}`));
  379. if (items.length > MAX_REPORT) console.error(` ... and ${items.length - MAX_REPORT} more`);
  380. }
  381. }
  382. console.log('\nLocale leaf counts:');
  383. for (const [code, map] of Object.entries(locales)) {
  384. const tier = code === 'en' ? 'ref' : 'locale';
  385. console.log(` ${code.padEnd(6)} ${String(map.size).padEnd(6)} [${tier}]`);
  386. }
  387. if (reports.length > 0) {
  388. console.error(`\n❌ i18n parity check failed.`);
  389. process.exit(1);
  390. }
  391. const others = codes.filter((c) => c !== 'en');
  392. console.log(`\n✓ All locales in parity with en (${others.join(' / ')}).`);
  393. }