142 lines
5.7 KiB
JavaScript
142 lines
5.7 KiB
JavaScript
/* eslint-disable */
|
|
// 由「locale 现状 + 新批次回填」增量合成 locales/{ko,th}.json(语种见 i18n_langs.cjs)。
|
|
//
|
|
// base = locales/{ko,th}.json 自身 (历史已译,读出后原地增量合并)
|
|
// overlay = scripts/kt_out/{ko,th}_NNN.json (新批次译文,纯字符串数组,
|
|
// 长度与顺序须与同号 batch 一致)
|
|
//
|
|
// 强约束:
|
|
// 1. 产物 key 集合与顺序必须与 en.json 完全一致(一一对应)。
|
|
// 2. 每个 kt_out 数组长度必须与同号 batch 一致,否则整批拒绝(不静默截断)。
|
|
// 3. 译文中的 {{placeholder}} 必须是 zh∪en 已提供占位符的子集——调用侧常同时
|
|
// 传入多个(如 {{rate}} 与 {{percent}}),译文可按目标语习惯择一使用,但绝不
|
|
// 允许出现调用侧没有提供的占位符(运行时会原样漏出 "{{x}}")。
|
|
// 未提供译文的 key 一律写 "",运行时因 returnEmptyString:false 回退中文。
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const LOCALES_DIR = path.join(__dirname, '..', 'src', 'i18n', 'locales');
|
|
const BATCH_DIR = path.join(__dirname, 'kt_batches');
|
|
const OUT_DIR = path.join(__dirname, 'kt_out');
|
|
const LANGS = require('./i18n_langs.cjs');
|
|
|
|
const manifest = JSON.parse(
|
|
fs.readFileSync(path.join(BATCH_DIR, '_manifest.json'), 'utf8'),
|
|
);
|
|
// kt_out/ 按批次号对位,换一组语种重切批次后,旧语种的 {lng}_NNN.json 仍留在目录里,
|
|
// 却对应的是上一轮的批次内容;若靠长度校验兜底,同长批次会被静默错位回填。
|
|
// 这里要求本次语种必须属于当前批次清单的语种(旧清单无 langs 字段时按 ko,th)。
|
|
const manifestLangs = manifest.langs || ['ko', 'th'];
|
|
const foreign = LANGS.filter((l) => !manifestLangs.includes(l));
|
|
if (foreign.length) {
|
|
console.error(
|
|
`当前 kt_batches 是按 ${manifestLangs.join(',')} 切的,不能回填 ${foreign.join(',')};先用同一组 --langs 重跑 i18n_kt_batches.cjs`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
const enKeys = Object.keys(
|
|
JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, 'en.json'), 'utf8'))
|
|
.translation,
|
|
);
|
|
|
|
const PH = /\{\{[^}]*\}\}/g;
|
|
const placeholders = (s) =>
|
|
new Set((String(s).match(PH) || []).map((x) => x.replace(/\s+/g, '')));
|
|
|
|
let hardFail = false;
|
|
|
|
for (const lng of LANGS) {
|
|
const localePath = path.join(LOCALES_DIR, `${lng}.json`);
|
|
const prev = fs.existsSync(localePath)
|
|
? JSON.parse(fs.readFileSync(localePath, 'utf8')).translation || {}
|
|
: {};
|
|
const out = {};
|
|
for (const k of Object.keys(prev)) if (prev[k]) out[k] = prev[k];
|
|
const baseCount = Object.keys(out).length;
|
|
const problems = [];
|
|
let added = 0;
|
|
|
|
for (const b of manifest.batches) {
|
|
const idx = b.file.match(/batch_(\d+)\.json/)[1];
|
|
const batch = JSON.parse(
|
|
fs.readFileSync(path.join(BATCH_DIR, b.file), 'utf8'),
|
|
);
|
|
const outFile = path.join(OUT_DIR, `${lng}_${idx}.json`);
|
|
if (!fs.existsSync(outFile)) continue;
|
|
|
|
const vals = JSON.parse(fs.readFileSync(outFile, 'utf8'));
|
|
if (!Array.isArray(vals) || vals.length !== batch.length) {
|
|
console.error(
|
|
`[${lng}] ${path.basename(outFile)} 长度 ${Array.isArray(vals) ? vals.length : 'NaN'} != batch ${batch.length},整批拒绝`,
|
|
);
|
|
hardFail = true;
|
|
continue;
|
|
}
|
|
batch.forEach((item, i) => {
|
|
const v = typeof vals[i] === 'string' ? vals[i] : '';
|
|
if (!v) return;
|
|
const allowed = new Set([
|
|
...placeholders(item.zh),
|
|
...placeholders(item.en),
|
|
]);
|
|
const unknown = [...placeholders(v)].filter((x) => !allowed.has(x));
|
|
if (unknown.length) {
|
|
problems.push(
|
|
`${path.basename(outFile)}[${i}] ${JSON.stringify(item.key)} 出现调用侧未提供的占位符 ${unknown.join(',')} -> ${JSON.stringify(v)}`,
|
|
);
|
|
}
|
|
if (!out[item.key]) added += 1;
|
|
out[item.key] = v;
|
|
});
|
|
}
|
|
|
|
// 人工覆盖,最后应用:批次翻译按语义直译,但表格列头有宽度预算,
|
|
// 长句式译文会撑破按中文两字标签定的窄列。这里存放收敛过的列头用词,
|
|
// 必须在批次回填之后生效,否则下次合并会被直译值冲掉。
|
|
const ovPath = path.join(__dirname, `kt_overrides_${lng}.json`);
|
|
let overridden = 0;
|
|
if (fs.existsSync(ovPath)) {
|
|
const ov = JSON.parse(fs.readFileSync(ovPath, 'utf8'));
|
|
for (const [k, v] of Object.entries(ov)) {
|
|
if (!v) continue;
|
|
if (k.startsWith('_')) continue; // "_" 前缀留给文件内注释
|
|
if (!enKeys.includes(k)) {
|
|
console.error(
|
|
`[${lng}] 覆盖项 ${JSON.stringify(k)} 不在 en.json 中,已跳过`,
|
|
);
|
|
hardFail = true;
|
|
continue;
|
|
}
|
|
out[k] = v;
|
|
overridden += 1;
|
|
}
|
|
}
|
|
|
|
// 严格按 en.json 的 key 顺序重排,保证与其它 locale 逐行对照
|
|
const ordered = {};
|
|
for (const k of enKeys) ordered[k] = out[k] || '';
|
|
const filled = enKeys.filter((k) => ordered[k]).length;
|
|
|
|
const okKeys =
|
|
Object.keys(ordered).length === enKeys.length &&
|
|
Object.keys(ordered).every((k, i) => k === enKeys[i]);
|
|
|
|
fs.writeFileSync(
|
|
localePath,
|
|
JSON.stringify({ translation: ordered }, null, 2) + '\n',
|
|
'utf8',
|
|
);
|
|
|
|
console.log(
|
|
`[${lng}] keys=${enKeys.length} 已译=${filled} (原有${baseCount} + 新增${added} + 覆盖${overridden}) 待译=${enKeys.length - filled} key与en一一对应=${okKeys ? 'YES' : 'NO'}`,
|
|
);
|
|
if (!okKeys) hardFail = true;
|
|
if (problems.length) {
|
|
hardFail = true;
|
|
console.error(`[${lng}] 占位符异常 ${problems.length} 条:`);
|
|
problems.slice(0, 30).forEach((p) => console.error(' ' + p));
|
|
}
|
|
}
|
|
|
|
process.exit(hardFail ? 1 : 0);
|