mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-05 14:41:26 +09:00
Plan 79 D1/D2.
extract-i18n.mjs walks the AST and regenerates en.json from the inline English
defaults, so en.json is output rather than a file anyone edits. That is what
makes a migration this size survivable: an unextracted key still renders
correct English, so a half-migrated page is never broken and never shows a raw
key path to a user. --check fails the build when it drifts.
check-i18n.mjs asserts what only a user in that language would otherwise
discover: no orphan keys, no dropped or invented {{placeholders}}, same leaf-
vs-namespace shape, and manifest/bundle agreement -- a language offered in the
selector with no bundle silently renders English, and the user cannot tell
that apart from "not translated yet".
The extractor immediately caught a real problem in its own author's code: the
format door wrapped i18next behind a helper called `translate()`, which made
its nine literal keys invisible to extraction -- copy that would have quietly
stopped being translatable. The wrapper is now named `t`, and the extractor
matches bare `t(...)` only, since `i18next.t(key)` re-dispatches a variable by
construction.
es.json is seeded now rather than at the end of the plan. A locale you never
switch to is a locale that does not work, and seeding it here means every
subsequent wave is provably bilingual as it lands instead of being validated
once at the finish.
All four guards mutation-checked: dropped placeholder, orphan key, missing
inline default, and stale en.json each fail as intended.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
121 lines
4.3 KiB
JavaScript
121 lines
4.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Plan 79 D2 — locale-file integrity.
|
|
//
|
|
// Four assertions, each guarding a failure that is invisible until a user in
|
|
// that language hits it:
|
|
//
|
|
// 1. No orphan keys. A key in es.json that no longer exists in en.json is
|
|
// dead weight at best, and at worst hides that the English moved on.
|
|
// 2. No missing interpolation placeholders. A translation that drops
|
|
// `{{count}}` renders a sentence with a hole in it; one that keeps a
|
|
// placeholder English no longer passes renders literal braces to a user.
|
|
// 3. Same shape. A leaf in one file and a namespace in another means one of
|
|
// the two renders "[object Object]".
|
|
// 4. The manifest and the shipped files agree. A language advertised in the
|
|
// selector with no bundle silently falls back to English, and the user
|
|
// cannot tell the difference between "not translated" and "broken".
|
|
//
|
|
// Coverage (how much of the app is keyed at all) is the separate
|
|
// check-i18n-literals ratchet; this script is about the files being coherent.
|
|
//
|
|
// Usage (from frontend/):
|
|
// node scripts/check-i18n.mjs
|
|
|
|
import { readFileSync, readdirSync } from 'node:fs';
|
|
import { basename, dirname, extname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const root = resolve(here, '..');
|
|
const localesDir = resolve(root, 'src', 'i18n', 'locales');
|
|
const manifestFile = resolve(root, 'src', 'i18n', 'languages.json');
|
|
|
|
const DEFAULT_LOCALE = 'en';
|
|
const PLACEHOLDER = /\{\{\s*(\w+)[^}]*\}\}/g;
|
|
|
|
function readJson(path) {
|
|
return JSON.parse(readFileSync(path, 'utf8'));
|
|
}
|
|
|
|
/** Flatten a nested locale tree into dotted key -> string. */
|
|
function flatten(node, prefix = '', out = new Map()) {
|
|
for (const [key, value] of Object.entries(node)) {
|
|
const path = prefix ? `${prefix}.${key}` : key;
|
|
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
flatten(value, path, out);
|
|
} else {
|
|
out.set(path, value);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function placeholders(value) {
|
|
return new Set([...String(value).matchAll(PLACEHOLDER)].map((match) => match[1]));
|
|
}
|
|
|
|
const problems = [];
|
|
|
|
const manifest = readJson(manifestFile);
|
|
const declared = manifest.languages.map((language) => language.code);
|
|
|
|
const shipped = readdirSync(localesDir)
|
|
.filter((name) => extname(name) === '.json')
|
|
.map((name) => basename(name, '.json'));
|
|
|
|
// 4. manifest <-> files
|
|
for (const code of declared) {
|
|
if (!shipped.includes(code)) {
|
|
problems.push(`languages.json advertises '${code}' but src/i18n/locales/${code}.json is missing`);
|
|
}
|
|
}
|
|
for (const code of shipped) {
|
|
if (!declared.includes(code)) {
|
|
problems.push(`src/i18n/locales/${code}.json ships but languages.json does not advertise '${code}'`);
|
|
}
|
|
}
|
|
|
|
const base = flatten(readJson(join(localesDir, `${DEFAULT_LOCALE}.json`)));
|
|
|
|
for (const code of shipped.filter((name) => name !== DEFAULT_LOCALE)) {
|
|
const translated = flatten(readJson(join(localesDir, `${code}.json`)));
|
|
|
|
for (const [key, value] of translated) {
|
|
// 1. orphans
|
|
if (!base.has(key)) {
|
|
problems.push(`${code}.json: '${key}' does not exist in ${DEFAULT_LOCALE}.json`);
|
|
continue;
|
|
}
|
|
// 3. shape
|
|
if (typeof value !== 'string') {
|
|
problems.push(`${code}.json: '${key}' is ${typeof value}, expected a string`);
|
|
continue;
|
|
}
|
|
// 2. placeholders
|
|
const expected = placeholders(base.get(key));
|
|
const actual = placeholders(value);
|
|
for (const name of expected) {
|
|
if (!actual.has(name)) {
|
|
problems.push(`${code}.json: '${key}' drops the {{${name}}} placeholder`);
|
|
}
|
|
}
|
|
for (const name of actual) {
|
|
if (!expected.has(name)) {
|
|
problems.push(`${code}.json: '${key}' introduces an unknown {{${name}}} placeholder`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (problems.length) {
|
|
console.error('\ni18n locale check failed:\n');
|
|
for (const problem of problems) console.error(` ${problem}`);
|
|
console.error('');
|
|
process.exit(1);
|
|
}
|
|
|
|
const translatedCount = shipped.filter((name) => name !== DEFAULT_LOCALE).length;
|
|
console.log(
|
|
`✓ i18n locales: ${base.size} keys, ${translatedCount} translated locale(s), no orphans or placeholder drift.`,
|
|
);
|