Files
kuentagym/frontend/scripts/check-locales.mjs
T
Duarte Santos 051a37abbf CI: actually run the tests, and hold the locales to one key set
There are 159 unit tests and nothing ran them. Both existing workflows fire on
push to main only, so the two outside pull requests merged so far were reviewed
by eye alone. This runs the suite, the build and a locale check on every pull
request.

The locale check exists because of a bug that shipped last week: a feature added
17 strings to de.js only, and the other ten languages fell back to English
mid-sentence with nothing failing anywhere. The checker compares every locale
against the union of all keys, so a string added to one file flags the other ten
instead of passing unnoticed.
2026-07-31 19:12:18 +02:00

58 lines
2.2 KiB
JavaScript

#!/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.
import { readdirSync } 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)
}
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)))
}
// 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
for (const [lang, keys] of locales) {
const missing = union.filter(k => !keys.has(k))
const orphans = union.filter(k => keys.has(k) && seen.get(k) === 1)
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)
}
console.log(`${locales.size} locales, ${union.length} keys each — in sync.`)