feat(i18n): localize middleware and quota-notify user-facing messages#6247
feat(i18n): localize middleware and quota-notify user-facing messages#6247gaga-rgb wants to merge 1 commit into
Conversation
Migrate 16 hardcoded Chinese user-facing messages to the existing i18n
framework (common.TranslateMessage), resolving per-request via the
user's saved language setting, then Accept-Language, then default.
- i18n/keys.go: add 16 message-key constants (auth, rate limit,
email verification, secure verify, turnstile)
- i18n/locales/{en,zh-CN,zh-TW}.yaml: add translations for all new
keys; zh-CN values are byte-identical to the original hardcoded
strings, so Chinese users see exactly the same text as before
- middleware/auth.go: 5 OpenAI-format API error messages
- middleware/model-rate-limit.go: 2 HTTP 429 messages
- middleware/email-verification-rate-limit.go: 2 messages
- middleware/secure_verification.go: 4 messages (frontend detects
these by code field, not message text)
- middleware/turnstile-check.go: 3 messages
- service/quota.go: quota-warning notification runs in an async
goroutine without gin context and its strings contain the notify
system's {{value}} placeholders (which collide with go-i18n
templates), so it uses a simple switch on the user's saved
language setting instead of the bundle
WalkthroughMiddleware error responses now use localized message keys across authentication, rate limiting, secure verification, and Turnstile checks. Quota notifications also select English templates when the user language starts with ChangesMiddleware localization
Quota notification localization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
middleware/turnstile-check.go (1)
51-53: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUse
common.DecodeJsonfor the Turnstile response body.
This middleware is business code, so it should follow the shared JSON wrapper guideline instead of callingjson.NewDecoderdirectly.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@middleware/turnstile-check.go` around lines 51 - 53, Replace the direct json.NewDecoder call in the Turnstile response handling with the shared common.DecodeJson helper, passing the response body and res destination while preserving the existing error handling.Source: Coding guidelines
middleware/auth.go (1)
472-473: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueReplace the hardcoded Chinese string in the internal error.
Although the user-facing HTTP response is successfully localized, the internally returned
errorcontinues to use a hardcoded Chinese string. Consider replacing it with an English error string to fully standardize internal errors and align with other errors in this file (e.g.,"token is nil").♻️ Proposed refactor
- abortWithOpenAiMessage(c, http.StatusForbidden, common.TranslateMessage(c, i18n.MsgAuthSpecificChannelDenied)) - return fmt.Errorf("普通用户不支持指定渠道") + abortWithOpenAiMessage(c, http.StatusForbidden, common.TranslateMessage(c, i18n.MsgAuthSpecificChannelDenied)) + return errors.New("specific channel denied for normal users")🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@middleware/auth.go` around lines 472 - 473, Update the error returned alongside abortWithOpenAiMessage in the affected authorization branch to use an English message instead of the hardcoded Chinese string, matching the internal error style used elsewhere in the middleware while leaving the localized user-facing response unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@middleware/auth.go`:
- Around line 472-473: Update the error returned alongside
abortWithOpenAiMessage in the affected authorization branch to use an English
message instead of the hardcoded Chinese string, matching the internal error
style used elsewhere in the middleware while leaving the localized user-facing
response unchanged.
In `@middleware/turnstile-check.go`:
- Around line 51-53: Replace the direct json.NewDecoder call in the Turnstile
response handling with the shared common.DecodeJson helper, passing the response
body and res destination while preserving the existing error handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b8ca7583-8834-40d4-9737-c9e457e15180
📒 Files selected for processing (10)
i18n/keys.goi18n/locales/en.yamli18n/locales/zh-CN.yamli18n/locales/zh-TW.yamlmiddleware/auth.gomiddleware/email-verification-rate-limit.gomiddleware/model-rate-limit.gomiddleware/secure_verification.gomiddleware/turnstile-check.goservice/quota.go
📝 变更描述 / Description
English
Motivation
The backend already has an i18n framework (
common.TranslateMessage+i18n/locales/{en,zh-CN,zh-TW}.yaml, resolving per request as: user's saved language setting →Accept-Languageheader → default), but 16 user-facing messages in the middleware layer bypassed it and were hardcoded in Chinese. They were returned in Chinese regardless of the caller's language — which matters for API consumers (themiddleware/auth.gomessages are OpenAI-format API error responses seen by end customers) and for non-Chinese deployments.What changed (10 files, +130/−17)
i18n/keys.goi18n/locales/en.yaml/zh-CN.yaml/zh-TW.yaml{{.Group}},{{.Minutes}},{{.Count}},{{.Seconds}})middleware/auth.gofmt.Errorfreturn value was deliberately left unchanged (not user-visible)middleware/model-rate-limit.gomiddleware/email-verification-rate-limit.gomiddleware/secure_verification.gomiddleware/turnstile-check.goservice/quota.goBehavior guarantees
zh-CN.yamlvalues are the exact original hardcoded strings, so Chinese-language users see byte-identical text — nothing was removed or reworded.secure_verification.goresponses by thecodefield, not by message text (verified inweb/classic/src/helpers/secureApiCall.js), so translating those messages is safe.Why
service/quota.gouses a language switch instead of the bundleThe quota-warning notification runs in an async goroutine with no gin context, and its strings contain the notify system's
{{value}}placeholders, which would collide with go-i18n's{{.Var}}template syntax. It therefore uses a simple switch onuserSetting.Language: English only when the user's saved setting starts withen, otherwise the original Chinese — preserving the default behavior exactly.Verification
mainpackage's embed ofweb/*/distrequires a frontend build — pre-existing, unrelated to this change)go vet ./i18n/ ./middleware/ ./service/cleango test ./middleware/ ./service/passgofmtapplied中文
动机
后端已有 i18n 框架(
common.TranslateMessage+i18n/locales/{en,zh-CN,zh-TW}.yaml,按请求解析顺序:用户保存的语言设置 →Accept-Language请求头 → 默认语言),但中间件层有 16 条面向用户的消息绕过了该框架,直接硬编码为中文。无论调用方使用什么语言,这些消息都以中文返回——这对 API 消费者(middleware/auth.go中的消息是终端客户可见的 OpenAI 格式 API 错误响应)以及非中文部署环境影响较大。变更内容(10 个文件,+130/−17)
i18n/keys.goi18n/locales/en.yaml/zh-CN.yaml/zh-TW.yaml{{.Group}}、{{.Minutes}}、{{.Count}}、{{.Seconds}})middleware/auth.gofmt.Errorf返回值有意保持不变(用户不可见)middleware/model-rate-limit.gomiddleware/email-verification-rate-limit.gomiddleware/secure_verification.gomiddleware/turnstile-check.goservice/quota.go行为保证
zh-CN.yaml中的值与原硬编码字符串逐字节一致,中文用户看到的文本与之前完全相同——没有删除或改写任何内容。code字段而非消息文本识别secure_verification.go的响应(已在web/classic/src/helpers/secureApiCall.js中核实),因此翻译这些消息是安全的。为什么
service/quota.go使用语言 switch 而不是 i18n bundle额度预警通知在异步 goroutine 中运行,没有 gin context,且其字符串包含通知系统自身的
{{value}}占位符,会与 go-i18n 的{{.Var}}模板语法冲突。因此改为对userSetting.Language做简单判断:仅当用户保存的语言设置以en开头时输出英文,否则输出原有中文——默认行为完全保持不变。验证
main包 embedweb/*/dist需要前端构建——为既有情况,与本变更无关)go vet ./i18n/ ./middleware/ ./service/无警告go test ./middleware/ ./service/通过gofmt🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
Summary by CodeRabbit
New Features
Bug Fixes