#!/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; }