#!/usr/bin/env node // Guards the one invariant every locale in src/locales/ shares: the same key set. // English is the source language and has no locale file, so a key that exists in only // some locales falls back to English silently, mid-sentence, with nothing failing // anywhere. This catches that at review time instead of in the app. // // node scripts/check-locales.mjs // // The reference is the union of all locales, not one blessed file: a key added to a // single locale then flags the other ten instead of passing unnoticed. // // Keys listed in src/locales/PENDING.md are reported and counted, not failed on. That file exists // because a guard that is red for a known reason is a guard nobody reads: the moment it always // fails, the next genuine drift it catches looks exactly like the noise. Keeping the pending set // written down means this check still goes red for anything *new*, and the debt stays a list // somebody can work through rather than a permanent red line. import { readdirSync, readFileSync, existsSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const localesDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'src', 'locales') const files = readdirSync(localesDir).filter(f => f.endsWith('.js')).sort() if (!files.length) { console.error(`No locale files found in ${localesDir}`) process.exit(1) } // One pending key per line prefixed with "- ", the shape a Markdown list already has. const pendingPath = join(localesDir, 'PENDING.md') const pending = new Set( existsSync(pendingPath) ? readFileSync(pendingPath, 'utf8').split('\n') .filter(line => line.trim().startsWith('- ')) .map(line => line.trim().slice(2).trim()) : [] ) const locales = new Map() for (const file of files) { const { default: dict } = await import(pathToFileURL(join(localesDir, file)).href) if (!dict || typeof dict !== 'object') { console.error(`${file}: no default-exported object`) process.exit(1) } locales.set(file.replace(/\.js$/, ''), new Set(Object.keys(dict))) } // A key every locale already carries cannot be pending: it would let a locale drop it silently. const stalePending = [...pending].filter(k => [...locales.values()].every(keys => keys.has(k))) if (stalePending.length) { console.error('\nThese keys are in every locale already and must come out of PENDING.md:') for (const k of stalePending) console.error(` ${JSON.stringify(k)}`) process.exit(1) } // How many locales carry each key — 1 means the key was added to a single file only, // which is the usual shape of the bug and worth naming separately from plain gaps. const seen = new Map() for (const keys of locales.values()) for (const k of keys) seen.set(k, (seen.get(k) || 0) + 1) const union = [...seen.keys()] let failed = false const waiting = new Set() for (const [lang, keys] of locales) { const gaps = union.filter(k => !keys.has(k)) const missing = gaps.filter(k => !pending.has(k)) for (const k of gaps) if (pending.has(k)) waiting.add(k) // A key only one locale carries is never "pending": it means that locale invented a string // nobody else has, which is a mistake rather than a translation waiting to happen. const orphans = union.filter(k => keys.has(k) && seen.get(k) === 1 && !pending.has(k)) if (missing.length || orphans.length) { failed = true console.error(`\n${lang}.js: ${keys.size}/${union.length} keys`) for (const k of missing) console.error(` missing: ${JSON.stringify(k)}`) for (const k of orphans) console.error(` only here: ${JSON.stringify(k)}`) } } if (failed) { console.error('\nLocale key sets differ. Every locale must carry the same keys.') process.exit(1) } const tail = waiting.size ? `${waiting.size} key${waiting.size === 1 ? '' : 's'} pending translation in ${locales.size - 1} locales (see src/locales/PENDING.md)` : 'in sync' console.log(`${locales.size} locales, ${union.length} keys each — ${tail}.`)