中英文切换
This commit is contained in:
75
tmp_inject2.cjs
Normal file
75
tmp_inject2.cjs
Normal file
@@ -0,0 +1,75 @@
|
||||
// Inject the hardcoded-Chinese keys (tmp_newkeys.json) into locale files,
|
||||
// preserving formatting by inserting before each namespace's closing brace.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function load(p) {
|
||||
let s = fs.readFileSync(p, 'utf8');
|
||||
s = s.replace(/const\s+\w+\s*=\s*\{/, 'var obj = {');
|
||||
s = s.replace(/export\s+default\s+\w+;?/, '');
|
||||
s = s.replace(/\bconst\b/g, 'var');
|
||||
return new Function(s + '\nreturn obj;')();
|
||||
}
|
||||
function flat(o, p, r) { p = p || ''; r = r || {}; for (const k of Object.keys(o)) { const key = p ? p + '.' + k : k; if (o[k] && typeof o[k] === 'object' && !Array.isArray(o[k])) flat(o[k], key, r); else r[key] = o[k]; } return r; }
|
||||
|
||||
const newKeys = JSON.parse(fs.readFileSync('tmp_newkeys.json', 'utf8'));
|
||||
const zhFlat = flat(load('src/locales/zh/index.ts'));
|
||||
const enFlat = flat(load('src/locales/en/index.ts'));
|
||||
|
||||
function esc(v) { return String(v).replace(/\\/g, '\\\\').replace(/'/g, "\\'"); }
|
||||
|
||||
function insertIntoNs(text, ns, entries, indentUnit) {
|
||||
// entries: [[base, value], ...] for keys NOT already present
|
||||
const re = new RegExp('(\\n[ \\t]*)' + ns + ':\\s*\\{');
|
||||
const m = re.exec(text);
|
||||
if (!m) return null;
|
||||
let i = m.index + m[0].length;
|
||||
let depth = 1, j = i;
|
||||
while (j < text.length && depth > 0) { const c = text[j]; if (c === '{') depth++; else if (c === '}') depth--; j++; }
|
||||
const closeIdx = j - 1;
|
||||
const indent = m[1];
|
||||
const childIndent = indent + indentUnit;
|
||||
const qk = (k) => /^[0-9]/.test(k) ? "'" + k + "'" : k;
|
||||
const block = entries.map(([k, v]) => childIndent + qk(k) + ": '" + esc(v) + "',").join('\n');
|
||||
return text.slice(0, closeIdx) + block + '\n' + text.slice(closeIdx);
|
||||
}
|
||||
|
||||
function addNamespace(text, ns, entries, indentUnit) {
|
||||
// find top-level const X = { ... } closing brace
|
||||
const m = /const\s+\w+\s*=\s*\{/.exec(text);
|
||||
let i = m.index + m[0].length;
|
||||
let depth = 1, j = i;
|
||||
while (j < text.length && depth > 0) { const c = text[j]; if (c === '{') depth++; else if (c === '}') depth--; j++; }
|
||||
const closeIdx = j - 1;
|
||||
const indent = '\n' + indentUnit;
|
||||
const childIndent = indent + indentUnit;
|
||||
const qk = (k) => /^[0-9]/.test(k) ? "'" + k + "'" : k;
|
||||
const block = entries.map(([k, v]) => childIndent + qk(k) + ": '" + esc(v) + "',").join('\n');
|
||||
const nsBlock = indent + ns + ': {' + '\n' + block + '\n' + indent + '}';
|
||||
return text.slice(0, closeIdx) + ',' + nsBlock + text.slice(closeIdx);
|
||||
}
|
||||
|
||||
for (const file of ['src/locales/zh/index.ts', 'src/locales/en/index.ts']) {
|
||||
let text = fs.readFileSync(file, 'utf8');
|
||||
const flatMap = file.includes('zh') ? zhFlat : enFlat;
|
||||
const src = file.includes('zh') ? newKeys.zh : newKeys.en;
|
||||
let added = 0;
|
||||
for (const ns of Object.keys(src)) {
|
||||
const entries = [];
|
||||
for (const base of Object.keys(src[ns])) {
|
||||
const key = ns + '.' + base;
|
||||
if (key in flatMap) continue; // already present
|
||||
entries.push([base, src[ns][base]]);
|
||||
}
|
||||
if (!entries.length) continue;
|
||||
const before = text;
|
||||
let res = insertIntoNs(text, ns, entries, '\t');
|
||||
if (res === null) {
|
||||
// namespace missing (e.g., 'page') -> create it
|
||||
res = addNamespace(text, ns, entries, '\t');
|
||||
}
|
||||
if (res !== null && res !== before) { text = res; added += entries.length; }
|
||||
}
|
||||
fs.writeFileSync(file, text, 'utf8');
|
||||
console.log(file + ' -> added ' + added + ' keys');
|
||||
}
|
||||
Reference in New Issue
Block a user