Skip to content

fix(i18n): 一处键差异被 checkKeyParity 报成两条,且 extra 的 locale 标签指向错误一侧 - #210

Merged
helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-i18n-parity-double-report
Oct 18, 2026
Merged

helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-i18n-parity-double-report

Conversation

@wxrbyte

@wxrbyte wxrbyte commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Closes #209

问题

checkKeyParity() 外层遍历全部 locale、内层再对 flat / otherFlat 各比较一次,导致同一处 en/zh
键差异产出两条 issue:一条 missing,一条 extra;而 extra 那条的 locale 填的是当前 locale
而非缺键的一侧,标签与事实相反。

scripts/i18n-check.ts:56-58 把 issues.length 直接当作 failures 打印,所以 CI 日志里的
i18n:check FAILED — N issue(s) 与真实差异处数不符。

改动

packages/i18n/src/resources.ts:改为以 SUPPORTED_LOCALES_FOR_RESOURCES[0] 为基准 locale 单向比较,
并把三类 issue 的 locale 统一为「有问题的那一侧」:

type locale 指向
missing 该 locale 缺少基准里存在的键
extra 该 locale 多出基准里没有的键
interpolation-mismatch 该 locale 的 {{var}} 集合与基准不一致

issues.length 因此恰好等于差异处数;locale 语义在三类之间一致;未来新增第三个 locale 时仍是
「以第一个为基准逐个比对」,无需再改结构。

对照证据(真实脚本)

在 packages/i18n/src/locales/zh-CN/navigation.ts 中删掉 today: '今日',(制造一处差异),
bun run i18n:check:

修复前(upstream main):

i18n:check
  supported locales: en-US, zh-CN
  ✗ [missing zh-CN] navigation.today
  ✗ [extra zh-CN] navigation.today
  en-US: 1487 keys · zh-CN: 1486 keys
i18n:check FAILED — 2 issue(s)

修复后(本 PR):

i18n:check
  supported locales: en-US, zh-CN
  ✗ [missing zh-CN] navigation.today
  en-US: 1487 keys · zh-CN: 1486 keys
i18n:check FAILED — 1 issue(s)

键对齐时两者一致:✓ key parity: en-US 1487 keys ≡ zh-CN 1487 keys (0 issues)、exit 0
(确认没有把「对齐」误报成问题)。

验证

TDD:先失败后通过

阶段 命令 结果
修复前(main 代码 + 新用例) bun test packages/i18n/src/resources.test.ts --isolate 5 pass / 3 fail
修复后 同上 8 pass / 0 fail

三条新用例失败时均为 Expected length: 1 / Received length: 2,分别覆盖:一处缺失键、一处多余键、
一处插值不一致。

回归与门禁(本机 Windows / bun 1.x,--isolate)

命令 结果
bun test packages/i18n --isolate 28 pass / 0 fail(4 files)
bun run i18n:check 0 issues,exit 0
bun run typecheck core / i18n / shared / ui / electron 五个工作区 exit 0

UI

无可见 UI 变化(仅 i18n 校验工具链的报错计数与标签语义;翻译资源本身未改动)。

checkKeyParity() 外层遍历全部 locale,内层再对 flat / otherFlat 各比较一次,
导致同一处 en/zh 键差异产出 missing + extra 两条 issue;其中 extra 那条的
locale 填的是当前 locale 而不是缺键的一侧,标签与事实相反。

i18n:check 把 issues.length 直接当作 failures 打印(scripts/i18n-check.ts:57),
所以 CI 日志里的 `i18n:check FAILED — N issue(s)` 与实际差异处数不符:一处差异
报成 2 条。assertKeyParity() 的样本额度(前 8 条)也被重复项占掉一半。

改为以 SUPPORTED_LOCALES_FOR_RESOURCES[0] 为基准 locale 单向比较,并把三类
issue 的 locale 统一为「有问题的那一侧」:missing = 该 locale 缺少基准键,
extra = 该 locale 多出基准没有的键,interpolation-mismatch = 该 locale 的
{{var}} 集合与基准不一致。

验证:
- 新增 3 条回归用例(一处缺失 / 一处多余 / 一处插值不一致)
- 修复前 bun test packages/i18n/src/resources.test.ts --isolate = 5 pass / 3 fail
  (三条均为 Expected length: 1 / Received length: 2)
- 修复后同文件 = 8 pass / 0 fail;packages/i18n 全目录 = 28 pass / 0 fail
- 真实脚本对照:删除 zh-CN 的 navigation.today 后,
  修复前 bun run i18n:check 输出 [missing zh-CN] + [extra zh-CN]、FAILED — 2 issue(s);
  修复后输出 [missing zh-CN]、FAILED — 1 issue(s)
- 键对齐时 bun run i18n:check 0 issues、exit 0;bun run typecheck 五个工作区 exit 0

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本轮独立源码审查及精确 HEAD focused 8/0 通过。最新main累计组合完整测试1959 pass / 7 skip / 0 fail,全workspace typecheck、i18n通过,secret扫描零命中。仅按本PR声明范围验收,不把离线回归当真实模型/生产API验收。

@helsome
helsome merged commit e067874 into helsome:main Oct 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(i18n): 一处键差异被 checkKeyParity 报成两条,且 extra 的 locale 标签指向错误一侧

2 participants