fix(i18n): restore locale validation on Windows #304

Closed
muel wants to merge 0 commits from muel/gitlink-cli:fix/i18n-locale-eol into master
Contributor

本 PR 修复当前最新 master 上 i18n 本地校验在 Windows 环境下容易失败的问题。

主要修复在开发时发现的两类失败:

  1. .gitattributes 固定 locale JSON 为 LF,修复 go run ./internal/i18n/cmd/check 的格式校验失败。
  2. 补充 cmd.ignore.short,修复 go run ./internal/i18n/cmd/check --scan-code 的缺 key 失败。

问题现象

1. check 格式校验失败

在 Windows 环境下执行:

go run ./internal/i18n/cmd/check

报错:

internal\i18n\locales\en-US.json: locale JSON is not formatted; run go run ./internal/i18n/cmd/check --fix
exit status 1

该问题是 Windows 工作区行尾可能被检出为 CRLF,导致 i18n check 工具与 Go JSON formatter 输出的 LF 格式发生字节级差异。

2. --scan-code 缺少 locale key

执行:

go run ./internal/i18n/cmd/check --scan-code

报错:

missing i18n key references: cmd.ignore.short
exit status 1

当前 shortcuts/register.go 已引用:

tr.T("cmd.ignore.short")

internal/i18n/locales/en-US.jsoninternal/i18n/locales/zh-CN.json 中缺少对应 key,因此 scan-code 会失败。

修复内容

1. 固定 i18n locale JSON 的行尾格式

新增 .gitattributes

.gitattributes text eol=lf
internal/i18n/locales/*.json text eol=lf

作用:

  • 固定 .gitattributes 自身使用 LF。
  • 固定 internal/i18n/locales/*.json 使用 LF。
  • 避免 Windows core.autocrlf=true 时将 locale JSON 检出为 CRLF。
  • 保证 go run ./internal/i18n/cmd/check 在 Windows / Linux / macOS 下格式判断一致。

2. 补充缺失的 cmd.ignore.short locale key

英文 locale 新增:

"cmd.ignore.short": "Ignore-file template operations"

中文 locale 新增:

"cmd.ignore.short": "忽略文件模板操作"

用于匹配当前代码中的:

tr.T("cmd.ignore.short")

从而修复:

go run ./internal/i18n/cmd/check --scan-code

中的缺 key 报错。

修改范围

本 PR 仅修改:

.gitattributes
internal/i18n/locales/en-US.json
internal/i18n/locales/zh-CN.json

当前情况

1. .gitattributes 修复 check 格式失败

修复前:

go run ./internal/i18n/cmd/check

会在 Windows 环境下报:

internal\i18n\locales\en-US.json: locale JSON is not formatted; run go run ./internal/i18n/cmd/check --fix
exit status 1

修复后,通过 .gitattributes 固定 locale JSON 为 LF,避免 CRLF 与 formatter LF 输出不一致导致的格式失败。

2. 补充 cmd.ignore.short 修复 scan-code 缺 key 失败

修复前:

go run ./internal/i18n/cmd/check --scan-code

会报:

missing i18n key references: cmd.ignore.short
exit status 1

修复后,en-US.jsonzh-CN.json 都补充了 cmd.ignore.short,与 shortcuts/register.go 中的引用保持一致。

验证结果

本地已执行并通过:

go run ./internal/i18n/cmd/check
go run ./internal/i18n/cmd/check --scan-code
go test ./...
go build .
go vet ./...

说明

这个问题不是翻译 key schema 设计问题,也不是 locale JSON 内容损坏。

实际包含两个独立原因:

  1. Windows 工作区行尾 CRLF 与 i18n check 工具生成的 LF 格式结果做字节级比较时不一致。
  2. 代码中已经引用 cmd.ignore.short,但两个 locale JSON 中没有补齐该 key。

本 PR 分别通过 .gitattributes 固定行尾格式,以及补充缺失 locale key,使 i18n 校验在 Windows / Linux / macOS 下保持一致。

本 PR 修复当前最新 `master` 上 i18n 本地校验在 Windows 环境下容易失败的问题。 主要修复在开发时发现的两类失败: 1. `.gitattributes` 固定 locale JSON 为 LF,修复 `go run ./internal/i18n/cmd/check` 的格式校验失败。 2. 补充 `cmd.ignore.short`,修复 `go run ./internal/i18n/cmd/check --scan-code` 的缺 key 失败。 ## 问题现象 ### 1. `check` 格式校验失败 在 Windows 环境下执行: ```bash go run ./internal/i18n/cmd/check ``` 报错: ```text internal\i18n\locales\en-US.json: locale JSON is not formatted; run go run ./internal/i18n/cmd/check --fix exit status 1 ``` 该问题是 Windows 工作区行尾可能被检出为 CRLF,导致 i18n check 工具与 Go JSON formatter 输出的 LF 格式发生字节级差异。 ### 2. `--scan-code` 缺少 locale key 执行: ```bash go run ./internal/i18n/cmd/check --scan-code ``` 报错: ```text missing i18n key references: cmd.ignore.short exit status 1 ``` 当前 `shortcuts/register.go` 已引用: ```go tr.T("cmd.ignore.short") ``` 但 `internal/i18n/locales/en-US.json` 和 `internal/i18n/locales/zh-CN.json` 中缺少对应 key,因此 scan-code 会失败。 ## 修复内容 ### 1. 固定 i18n locale JSON 的行尾格式 新增 `.gitattributes`: ```text .gitattributes text eol=lf internal/i18n/locales/*.json text eol=lf ``` 作用: * 固定 `.gitattributes` 自身使用 LF。 * 固定 `internal/i18n/locales/*.json` 使用 LF。 * 避免 Windows `core.autocrlf=true` 时将 locale JSON 检出为 CRLF。 * 保证 `go run ./internal/i18n/cmd/check` 在 Windows / Linux / macOS 下格式判断一致。 ### 2. 补充缺失的 `cmd.ignore.short` locale key 英文 locale 新增: ```json "cmd.ignore.short": "Ignore-file template operations" ``` 中文 locale 新增: ```json "cmd.ignore.short": "忽略文件模板操作" ``` 用于匹配当前代码中的: ```go tr.T("cmd.ignore.short") ``` 从而修复: ```bash go run ./internal/i18n/cmd/check --scan-code ``` 中的缺 key 报错。 ## 修改范围 本 PR 仅修改: ```text .gitattributes internal/i18n/locales/en-US.json internal/i18n/locales/zh-CN.json ``` ## 当前情况 ### 1. `.gitattributes` 修复 check 格式失败 修复前: ```bash go run ./internal/i18n/cmd/check ``` 会在 Windows 环境下报: ```text internal\i18n\locales\en-US.json: locale JSON is not formatted; run go run ./internal/i18n/cmd/check --fix exit status 1 ``` 修复后,通过 `.gitattributes` 固定 locale JSON 为 LF,避免 CRLF 与 formatter LF 输出不一致导致的格式失败。 ### 2. 补充 `cmd.ignore.short` 修复 scan-code 缺 key 失败 修复前: ```bash go run ./internal/i18n/cmd/check --scan-code ``` 会报: ```text missing i18n key references: cmd.ignore.short exit status 1 ``` 修复后,`en-US.json` 和 `zh-CN.json` 都补充了 `cmd.ignore.short`,与 `shortcuts/register.go` 中的引用保持一致。 ## 验证结果 本地已执行并通过: ```bash go run ./internal/i18n/cmd/check go run ./internal/i18n/cmd/check --scan-code go test ./... go build . go vet ./... ``` ## 说明 这个问题不是翻译 key schema 设计问题,也不是 locale JSON 内容损坏。 实际包含两个独立原因: 1. Windows 工作区行尾 CRLF 与 i18n check 工具生成的 LF 格式结果做字节级比较时不一致。 2. 代码中已经引用 `cmd.ignore.short`,但两个 locale JSON 中没有补齐该 key。 本 PR 分别通过 `.gitattributes` 固定行尾格式,以及补充缺失 locale key,使 i18n 校验在 Windows / Linux / macOS 下保持一致。
muel added 1 commit 2026-06-26 22:21:10 +08:00
wbtiger closed this pull request 2026-07-14 22:03:54 +08:00
wbtiger reopened this pull request 2026-07-14 23:03:33 +08:00
wbtiger closed this pull request 2026-07-14 23:09:08 +08:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Gitlink/gitlink-cli#304
No description provided.