95 lines
3.8 KiB
JavaScript
95 lines
3.8 KiB
JavaScript
/* eslint-disable */
|
||
// Review 辅助(R2-KT2):把「固定宽度约束」与「该位置的韩/泰译文实际长度」对撞,
|
||
// 找出会溢出的具体位置。控制台是围绕中文两字标签设计的,泰语平均膨胀 3.16x,
|
||
// 短标签最高 9x,密集表格首当其冲。
|
||
//
|
||
// 判据:Semi 的 Table column `width: N`(px)在 14px 字号下大致可容
|
||
// 中文 ≈ N/14 字,拉丁/泰文 ≈ N/7.5 字符(泰文含上下叠加符号,按拉丁估更保守)。
|
||
// 超出即报,按超出比例排序。这是静态估算,不替代真机目视走查。
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const SRC_DIR = path.join(__dirname, '..', 'src');
|
||
const LOCALES_DIR = path.join(__dirname, '..', 'src', 'i18n', 'locales');
|
||
const load = (n) =>
|
||
JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, `${n}.json`), 'utf8')).translation;
|
||
const LANGS = require('./i18n_langs.cjs');
|
||
const DICTS = LANGS.map((l) => [l, load(l)]);
|
||
|
||
const PX_PER_CHAR = 7.5; // 拼音文字/韩/泰在 14px 下的保守平均字宽
|
||
// 日文假名与汉字、繁体中文都是全角,按字号整宽计
|
||
const PX_PER_CHAR_BY_LANG = { ja: 14, 'zh-TW': 14 };
|
||
const PADDING = 24; // Semi 表头左右内边距 + 排序图标余量
|
||
|
||
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;
|
||
}
|
||
|
||
// 匹配 { title: t('中文'), ... width: 120 } 这类列定义(title 与 width 同一对象内,
|
||
// 允许中间隔若干属性;跨度限制在 400 字符内避免误配到相邻列)
|
||
const COL_RE =
|
||
/title:\s*t\(\s*(['"`])((?:\\.|(?!\1).)*?)\1[^{}]{0,400}?width:\s*(\d+)/gs;
|
||
// 反向顺序:width 在 title 之前
|
||
const COL_RE2 =
|
||
/width:\s*(\d+)[^{}]{0,400}?title:\s*t\(\s*(['"`])((?:\\.|(?!\2).)*?)\2/gs;
|
||
|
||
const CJK = /[一-鿿]/;
|
||
const rows = [];
|
||
|
||
for (const file of walk(SRC_DIR).filter((f) => !f.includes(path.join('src', 'i18n')))) {
|
||
const c = fs.readFileSync(file, 'utf8');
|
||
const rel = path.relative(SRC_DIR, file);
|
||
const push = (key, width) => {
|
||
if (!CJK.test(key)) return;
|
||
const k = key.replace(/\\'/g, "'").replace(/\\"/g, '"');
|
||
for (const [lng, dict] of DICTS) {
|
||
const v = dict[k];
|
||
if (!v) continue; // 未译则回退中文,不构成本次风险
|
||
const ppc = PX_PER_CHAR_BY_LANG[lng] || PX_PER_CHAR;
|
||
const avail = (Number(width) - PADDING) / ppc;
|
||
if (v.length > avail) {
|
||
rows.push({
|
||
file: rel,
|
||
key: k,
|
||
lng,
|
||
text: v,
|
||
width: Number(width),
|
||
need: Math.ceil(v.length * ppc + PADDING),
|
||
ratio: v.length / avail,
|
||
});
|
||
}
|
||
}
|
||
};
|
||
let m;
|
||
COL_RE.lastIndex = 0;
|
||
while ((m = COL_RE.exec(c))) push(m[2], m[3]);
|
||
COL_RE2.lastIndex = 0;
|
||
while ((m = COL_RE2.exec(c))) push(m[3], m[1]);
|
||
}
|
||
|
||
rows.sort((a, b) => b.ratio - a.ratio);
|
||
const seen = new Set();
|
||
const uniq = rows.filter((r) => {
|
||
const id = `${r.file}|${r.key}|${r.lng}`;
|
||
if (seen.has(id)) return false;
|
||
seen.add(id);
|
||
return true;
|
||
});
|
||
|
||
const perLang = LANGS.map((l) => `${l} ${uniq.filter((r) => r.lng === l).length}`).join(' / ');
|
||
console.log(`固定列宽装不下译文的位置:${uniq.length} 处(${perLang})\n`);
|
||
const LIMIT = Number(process.argv[2]) || 30; // 可传参查看全部
|
||
for (const r of uniq.slice(0, LIMIT)) {
|
||
console.log(
|
||
` ${r.ratio.toFixed(1)}x 超 [${r.lng}] width=${r.width}px 需≈${r.need}px 「${r.key}」→「${r.text}」\n ${r.file}`,
|
||
);
|
||
}
|
||
if (uniq.length > LIMIT) console.log(` ...另有 ${uniq.length - LIMIT} 处`);
|