mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 21:00:15 +08:00
补充官网多语言静态页面、管理端词条和动态提示,统一相关资源用量展示,保留表格导出等现有修改。 修正 RTL 布局与字体回退,并加入翻译契约、文案宽度、逻辑样式及浏览器回归检查。
111 lines
6.0 KiB
JavaScript
111 lines
6.0 KiB
JavaScript
import { resolve } from "node:path"
|
||
import { pathToFileURL } from "node:url"
|
||
import { ar } from "../src/i18n/locales/ar.ts"
|
||
import { deDE } from "../src/i18n/locales/de-DE.ts"
|
||
import { enUS } from "../src/i18n/locales/en-US.ts"
|
||
import { es419 } from "../src/i18n/locales/es-419.ts"
|
||
import { frFR } from "../src/i18n/locales/fr-FR.ts"
|
||
import { jaJP } from "../src/i18n/locales/ja-JP.ts"
|
||
import { koKR } from "../src/i18n/locales/ko-KR.ts"
|
||
import { ruRU } from "../src/i18n/locales/ru-RU.ts"
|
||
import { zhCN } from "../src/i18n/locales/zh-CN.ts"
|
||
import { zhTW } from "../src/i18n/locales/zh-TW.ts"
|
||
|
||
const locales = { "en-US": enUS, "zh-TW": zhTW, "ja-JP": jaJP, "ko-KR": koKR, "de-DE": deDE, "fr-FR": frFR, "es-419": es419, "ru-RU": ruRU, ar }
|
||
|
||
export function estimateWidth(value, latinWidth = 0.5) {
|
||
if (!Number.isFinite(latinWidth) || latinWidth <= 0) {
|
||
throw new Error("--latin-width 必须为正数")
|
||
}
|
||
const plain = value.replace(/<\/?(?:\d+|[A-Za-z][\w-]*)(?:\s[^<>]*?)?\s*\/?>/g, "").normalize("NFC")
|
||
return [...plain].reduce((sum, char) => {
|
||
if (/[\p{Mark}\p{Format}]/u.test(char)) return sum
|
||
// 半角片假名必须先判断;日文长音符“ー”属于 Common,不能只按 Script 判断。
|
||
if (/[\uff61-\uff9f]/u.test(char)) return sum + latinWidth
|
||
return sum + (/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303f\u3099-\u30ff\uff01-\uff60\uffe0-\uffe6]/u.test(char) ? 1 : latinWidth)
|
||
}, 0)
|
||
}
|
||
|
||
function flatten(value, prefix = "", result = {}) {
|
||
for (const [key, child] of Object.entries(value)) {
|
||
const path = prefix ? prefix + "." + key : key
|
||
if (typeof child === "string") result[path] = child
|
||
else if (child && typeof child === "object") flatten(child, path, result)
|
||
}
|
||
return result
|
||
}
|
||
|
||
function main(args) {
|
||
const latinWidthArg = args.find((arg) => arg.startsWith("--latin-width="))
|
||
const latinWidth = latinWidthArg ? Number(latinWidthArg.split("=")[1]) : 0.5
|
||
if (!Number.isFinite(latinWidth) || latinWidth <= 0) throw new Error("--latin-width 必须为正数")
|
||
const allShort = args.includes("--all-short")
|
||
const json = args.includes("--json")
|
||
const limitArg = args.find((arg) => arg.startsWith("--limit="))
|
||
const limit = limitArg ? Number(limitArg.split("=")[1]) : 40
|
||
if (!Number.isInteger(limit) || limit < 0) throw new Error("--limit 必须为非负整数")
|
||
const known = /^(--all-short|--json|--latin-width=[^=]+|--limit=[^=]+)$/
|
||
if (args.some((arg) => !known.test(arg))) throw new Error("支持参数:--all-short --json --latin-width=0.5 --limit=40")
|
||
|
||
const width = (value) => estimateWidth(value, latinWidth)
|
||
const baseline = flatten(zhCN)
|
||
const translated = Object.fromEntries(Object.entries(locales).map(([locale, value]) => [locale, flatten(value)]))
|
||
const navigation = [
|
||
"app.adminConsole",
|
||
"sections.statistics", "sections.usageRecords", "sections.aiResources",
|
||
"sections.billingManagement", "sections.systemSettings",
|
||
"pages.overview.title", "pages.realtimeStatus.title",
|
||
...["memberAnalysis", "taskAnalysis", "clientAnalysis", "resourceAnalysis"].map((part) => "pages.sessionReporting." + part + ".title"),
|
||
...["taskHistory", "conversationHistory", "models", "knowledgeBases", "skills", "experts", "tools", "rules", "resourceImports", "billingSettings", "billingDetails", "membersAndGroups", "feedback", "operationLogs", "security", "otherSettings"].map((part) => "pages." + part + ".title"),
|
||
"environmentAdmin.cloudHostsTitle",
|
||
]
|
||
const excluded = /description|hint|tooltip|placeholder|warning|error|message|success|failed|aria|prompt|empty|confirm.*description/i
|
||
const keys = allShort
|
||
? Object.keys(baseline).filter((key) => {
|
||
const value = baseline[key]
|
||
return value.trim() && width(value) <= 8 && !value.includes("{{") && !value.includes("\n") && !excluded.test(key)
|
||
})
|
||
: navigation
|
||
const missing = []
|
||
const rows = []
|
||
for (const key of keys) {
|
||
const source = baseline[key]
|
||
if (!source) { missing.push({ locale: "zh-CN", key }); continue }
|
||
for (const [locale, dictionary] of Object.entries(translated)) {
|
||
const value = dictionary[key]
|
||
if (typeof value !== "string" || !value.trim()) { missing.push({ locale, key }); continue }
|
||
if (value.includes("{{")) continue
|
||
const ratio = width(value) / width(source)
|
||
if (ratio < 0.6 - 1e-9 || ratio > 1.4 + 1e-9) rows.push({ key, locale, source, value, ratio: Number(ratio.toFixed(3)), direction: ratio < 0.6 ? "偏短" : "偏长" })
|
||
}
|
||
}
|
||
rows.sort((a, b) => Math.abs(b.ratio - 1) - Math.abs(a.ratio - 1) || a.key.localeCompare(b.key))
|
||
const summary = {
|
||
scope: allShort ? "静态短文案候选(中文估算宽度不超过8)" : "侧栏导航",
|
||
latinWidth,
|
||
allowedRatio: [0.6, 1.4],
|
||
keys: keys.length,
|
||
flaggedTranslations: rows.length,
|
||
flaggedKeys: new Set(rows.map((row) => row.key)).size,
|
||
byLocale: Object.fromEntries(Object.keys(locales).map((locale) => [locale, rows.filter((row) => row.locale === locale).length])),
|
||
missing,
|
||
note: "估算用于筛查,不等于浏览器排版验收;未模拟字体、阿拉伯语连写或容器宽度。自然词语和必要技术名称需人工判断,不能仅为达标而删减含义。",
|
||
}
|
||
if (json) console.log(JSON.stringify({ summary, rows }, null, 2))
|
||
else {
|
||
console.log(JSON.stringify(summary, null, 2))
|
||
console.log("\n资源管理对照(中文基准:" + baseline["sections.aiResources"] + ")")
|
||
for (const [locale, dictionary] of Object.entries(translated)) {
|
||
const value = dictionary["sections.aiResources"]
|
||
console.log(locale + "\t" + value + "\t" + (width(value) / width(baseline["sections.aiResources"]) * 100).toFixed(0) + "%")
|
||
}
|
||
console.log("\n需人工复核的文案(最多 " + limit + " 项)")
|
||
for (const row of rows.slice(0, limit)) console.log([row.locale, row.key, row.source, row.value, Math.round(row.ratio * 100) + "%", row.direction].join("\t"))
|
||
}
|
||
if (missing.length) process.exitCode = 1
|
||
}
|
||
|
||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
||
main(process.argv.slice(2))
|
||
}
|