Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/* eslint-disable */
|
||||
// Review 辅助:统计各语种译文对「源码实际引用的中文 key」的覆盖率,并按页面区域分组。
|
||||
// 目的:字典序推进的翻译会让覆盖率在页面间高度不均,产出「半韩半中」的界面。
|
||||
// 语种:--langs=ja,vi 或 I18N_LANGS,默认 ko,th;区域画像按第一个语种统计。
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const LOCALES_DIR = path.join(__dirname, '..', 'src', 'i18n', 'locales');
|
||||
const SRC_DIR = path.join(__dirname, '..', 'src');
|
||||
const LANGS = require('./i18n_langs.cjs');
|
||||
|
||||
const load = (n) =>
|
||||
JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, `${n}.json`), 'utf8')).translation;
|
||||
|
||||
const T_CALL_RE = /\bt\(\s*(['"`])((?:\\.|(?!\1).)*?)\1/g;
|
||||
const CJK_RE = /[一-鿿]/;
|
||||
|
||||
function walk(dir, files = []) {
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (['node_modules', 'dist'].includes(entry.name) || entry.name.startsWith('.')) continue;
|
||||
walk(full, files);
|
||||
} else if (/\.(jsx?|tsx?)$/.test(entry.name)) {
|
||||
files.push(full);
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
function unescape(raw) {
|
||||
return raw
|
||||
.replace(/\\'/g, "'")
|
||||
.replace(/\\"/g, '"')
|
||||
.replace(/\\\\/g, '\\')
|
||||
.replace(/\\n/g, '\n');
|
||||
}
|
||||
|
||||
const dicts = Object.fromEntries(LANGS.map((l) => [l, load(l)]));
|
||||
const primary = dicts[LANGS[0]];
|
||||
const files = walk(SRC_DIR).filter((f) => !f.includes(path.join('src', 'i18n')));
|
||||
|
||||
const byArea = new Map();
|
||||
const allUsed = new Set();
|
||||
|
||||
for (const file of files) {
|
||||
const content = fs.readFileSync(file, 'utf8');
|
||||
const rel = path.relative(SRC_DIR, file).split(path.sep);
|
||||
// 粒度必须与 i18n_kt_batches.cjs 的分层一致:components 也要拆到子目录,
|
||||
// 否则 2800 多个 key 全折叠成一个 'components',看不出各子区域是否打满。
|
||||
const area =
|
||||
rel[0] === 'pages' || rel[0] === 'components' ? `${rel[0]}/${rel[1]}` : rel[0];
|
||||
let m;
|
||||
T_CALL_RE.lastIndex = 0;
|
||||
while ((m = T_CALL_RE.exec(content)) !== null) {
|
||||
if (!CJK_RE.test(m[2])) continue;
|
||||
const key = unescape(m[2]);
|
||||
allUsed.add(key);
|
||||
if (!byArea.has(area)) byArea.set(area, new Set());
|
||||
byArea.get(area).add(key);
|
||||
}
|
||||
}
|
||||
|
||||
const used = [...allUsed];
|
||||
console.log(`源码实际引用的中文 key:${used.length}`);
|
||||
for (const l of LANGS) {
|
||||
const d = used.filter((k) => dicts[l][k]).length;
|
||||
console.log(` ${l} 已译:${d} (${Math.round((d / used.length) * 100)}%)`);
|
||||
}
|
||||
const absent = used.filter((k) => !(k in primary)).length;
|
||||
console.log(` 不在 ${LANGS[0]}.json 中(en.json 也缺,运行时回退中文):${absent}`);
|
||||
|
||||
const stats = [...byArea.entries()]
|
||||
.map(([area, set]) => {
|
||||
const arr = [...set];
|
||||
const done = arr.filter((k) => primary[k]).length;
|
||||
return { area, total: arr.length, done, pct: Math.round((done / arr.length) * 100) };
|
||||
})
|
||||
.filter((r) => r.total >= 25)
|
||||
.sort((a, b) => a.pct - b.pct);
|
||||
|
||||
console.log(`\n按区域覆盖率(${LANGS[0]},仅列 >=25 key 的区域,升序):`);
|
||||
for (const r of stats) {
|
||||
console.log(
|
||||
` ${String(r.pct).padStart(3)}% ${String(r.done).padStart(4)}/${String(r.total).padEnd(5)}${r.area}`,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user