中英文切换
This commit is contained in:
64
tmp_inject.cjs
Normal file
64
tmp_inject.cjs
Normal file
@@ -0,0 +1,64 @@
|
||||
// Inject missing keys into zh/index.ts and en/index.ts, preserving structure.
|
||||
const fs = require('fs');
|
||||
|
||||
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;
|
||||
}
|
||||
function esc(v) {
|
||||
return String(v).replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
||||
}
|
||||
|
||||
const adds = require('./tmp_missing.json');
|
||||
const zhAdd = adds.zhAdd, enAdd = adds.enAdd;
|
||||
|
||||
function inject(file, addsMap, label) {
|
||||
const text = fs.readFileSync(file, 'utf8');
|
||||
const lines = text.split('\n');
|
||||
// find namespaces present
|
||||
const present = new Set();
|
||||
for (const ns of Object.keys(addsMap)) {
|
||||
let nsStart = -1;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (/^\t[A-Za-z_][A-Za-z0-9_]*:\s*\{/.test(lines[i]) && lines[i].trim().startsWith(ns + ':')) { nsStart = i; break; }
|
||||
}
|
||||
if (nsStart < 0) { console.log(' [WARN] namespace not found, skip: ' + ns); continue; }
|
||||
// compute closing brace line by brace depth
|
||||
let depth = 0; let closeLine = -1;
|
||||
for (let i = nsStart; i < lines.length; i++) {
|
||||
const op = (lines[i].match(/\{/g) || []).length;
|
||||
const cl = (lines[i].match(/\}/g) || []).length;
|
||||
depth += op - cl;
|
||||
if (depth === 0) { closeLine = i; break; }
|
||||
}
|
||||
if (closeLine < 0) { console.log(' [WARN] no close for ' + ns); continue; }
|
||||
// build insertion block (sorted)
|
||||
const keys = Object.keys(addsMap[ns]).sort();
|
||||
const block = keys.map(k => '\t\t' + k + ": '" + esc(addsMap[ns][k]) + "',").join('\n');
|
||||
lines.splice(closeLine, 0, block);
|
||||
console.log(' ' + ns + ': +' + keys.length + ' keys');
|
||||
}
|
||||
fs.writeFileSync(file, lines.join('\n'), 'utf8');
|
||||
// verify parse
|
||||
const obj = load(file);
|
||||
const map = flat(obj);
|
||||
console.log(' ' + label + ' parsed OK, total flat keys: ' + Object.keys(map).length);
|
||||
}
|
||||
|
||||
console.log('Injecting into zh/index.ts ...');
|
||||
inject('src/locales/zh/index.ts', zhAdd, 'zh');
|
||||
console.log('Injecting into en/index.ts ...');
|
||||
inject('src/locales/en/index.ts', enAdd, 'en');
|
||||
console.log('Done.');
|
||||
Reference in New Issue
Block a user