import fs from 'node:fs/promises' import path from 'node:path' import process from 'node:process' import { fileURLToPath } from 'node:url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) function isPlainObject(v) { return v != null && typeof v === 'object' && !Array.isArray(v) } function flattenKeys(obj, prefix = '') { const out = new Set() if (!isPlainObject(obj)) return out for (const k of Object.keys(obj)) { const next = prefix ? `${prefix}.${k}` : k const v = obj[k] if (isPlainObject(v)) { for (const sub of flattenKeys(v, next)) out.add(sub) } else { out.add(next) } } return out } function diff(a, b) { // items in a but not in b const out = [] for (const x of a) if (!b.has(x)) out.push(x) out.sort() return out } async function readJson(p) { const raw = await fs.readFile(p, 'utf8') return JSON.parse(raw) } async function main() { const root = path.resolve(__dirname, '../src/locales') const files = { zh: path.join(root, 'zh.json'), en: path.join(root, 'en.json'), ja: path.join(root, 'ja.json') } const [zh, en, ja] = await Promise.all([readJson(files.zh), readJson(files.en), readJson(files.ja)]) const keysZh = flattenKeys(zh) const keysEn = flattenKeys(en) const keysJa = flattenKeys(ja) const missingEn = diff(keysZh, keysEn) const missingJa = diff(keysZh, keysJa) const extraEn = diff(keysEn, keysZh) const extraJa = diff(keysJa, keysZh) const problems = [] if (missingEn.length) problems.push({ title: 'en 缺少 key', items: missingEn }) if (missingJa.length) problems.push({ title: 'ja 缺少 key', items: missingJa }) if (extraEn.length) problems.push({ title: 'en 多余 key', items: extraEn }) if (extraJa.length) problems.push({ title: 'ja 多余 key', items: extraJa }) if (problems.length) { console.error('[i18n-check] locales key mismatch found:') for (const p of problems) { console.error(`\n- ${p.title}(${p.items.length})`) for (const it of p.items) console.error(` - ${it}`) } process.exit(1) } console.log(`[i18n-check] OK: ${keysZh.size} keys in sync (zh/en/ja).`) } main().catch((err) => { console.error('[i18n-check] Failed:', err) process.exit(1) })