Files
usage-zh/apply-zh.mjs
T
cat-shark 50740b4d08 feat: /usage 用量面板中文化(上游 0.10.2)
基于 @tmustier/pi-usage-extension 0.10.2(github.com/tmustier/pi-extensions 的
usage-extension/),仅改文案与 3 处中文宽度/排版补丁。

- zh-map.mjs:100 条词表(103 处替换),覆盖视图/周期/表头/快捷键/加载/洞察全部文案
- apply-zh.mjs:幂等应用器,上游改文案时报 MISS 而非静默漏译
- 本地补丁:x 轴日期改 ASCII M/D、分组横线用 visibleWidth、中文括号前不补空格
- 数据层:辅助用量伪渠道改 工具/摘要,CACHE_VERSION 6→7(名字会 intern 进缓存)
- 保持英文:CSV 表头、JSON 键名、导出文件名,避免下游表格/脚本失配
- scripts/:渲染与数据/导出冒烟脚本
2026-11-18 14:46:30 +08:00

98 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// 把 zh-map.mjs 的词表 + 本文件底部的本地补丁应用到扩展源码(默认就地替换)。
//
// 用法:
// node apply-zh.mjs # 就地汉化(幂等:已汉化的条目记为 DONE)
// node apply-zh.mjs --dry # 只报告命中/未命中,不写文件
//
// 升级上游时的流程:
// 1. 用新版 @tmustier/pi-usage-extension 的 index.ts/data.ts/graph.ts 覆盖本目录
// 2. node apply-zh.mjs
// 3. 报 MISS 的条目说明上游改了文案 → 更新 zh-map.mjs / PATCHES 后重跑
import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { MAP } from "./zh-map.mjs";
// 中文环境下必须的本地补丁(与语言无关的宽度/排版修正)。
// 键为文件,值为 [英文原文, 替换文本, 说明]。
const PATCHES = {
"index.ts": [
[
'return d.toLocaleDateString(undefined, { day: "numeric", month: "short" });\n\t\t};\n\n\t\tconst chartHeight = 12;',
[
"\t\t\t// 图表 x 轴用 ASCII 月/日:renderChart 用字符串长度算轴位,",
'\t\t\t// 中文 locale 的 "8月14日" 显示宽度是长度的两倍,会把末尾标签挤掉。',
"\t\t\tconst mm = d.getMonth() + 1;",
"\t\t\tconst dd = d.getDate();",
'\t\t\treturn d.getFullYear() === new Date().getFullYear() ? `${mm}/${dd}` : `${String(d.getFullYear()).slice(2)}/${mm}/${dd}`;',
"\t\t};",
"",
"\t\tconst chartHeight = 12;",
].join("\n"),
"x 轴日期改 ASCII,避免 CJK 宽度错位",
],
[
'const rule = "─".repeat(Math.max(contentWidth - label.length - 1, 4));',
'const rule = "─".repeat(Math.max(contentWidth - visibleWidth(label) - 1, 4));',
"分组横线按显示宽度计算(中文标签 width ≠ length)",
],
[
"const headline = match ? `${match[1]} ${th.fg(\"dim\", match[2]!)}` : insight.headline;",
"const headline = match ? `${match[1]}${th.fg(\"dim\", match[2]!)}` : insight.headline;",
"中文括号前不留空格",
],
],
};
const here = dirname(fileURLToPath(import.meta.url));
const dry = process.argv.includes("--dry");
let missing = 0;
const misses = [];
function applyPairs(src, pairs, file, kind) {
let done = 0;
let already = 0;
for (const [en, zh] of pairs) {
const count = src.split(en).length - 1;
if (count > 0) {
src = src.split(en).join(zh);
done += count;
continue;
}
if (zh && src.includes(zh)) {
already++;
continue;
}
misses.push(`[${file}/${kind}] ${en.slice(0, 100)}`);
}
return { src, done, already };
}
const files = new Set([...Object.keys(MAP), ...Object.keys(PATCHES)]);
for (const file of files) {
const path = join(here, file);
let src = readFileSync(path, "utf8");
const t = applyPairs(src, MAP[file] ?? [], file, "词表");
src = t.src;
const p = applyPairs(src, (PATCHES[file] ?? []).map(([en, zh]) => [en, zh]), file, "补丁");
src = p.src;
if (!dry) writeFileSync(path, src);
missing = misses.length;
console.log(
`[${file}] 词表命中 ${t.done} 处(已汉化 ${t.already}),补丁命中 ${p.done} 处(已应用 ${p.already})`
);
}
console.log(`\n未命中 ${missing} 条${dry ? "(dry-run,未写盘)" : ""}`);
for (const m of misses) console.log(" MISS " + m);
if (missing > 0) {
console.log("提示:MISS 多为上游文案变更;对照 zh-map.mjs / PATCHES 更新后重跑。");
process.exitCode = 1;
}