Skip to content

feat: backend i18n - #2842

Merged
Calcium-Ion merged 4 commits into
mainfrom
feat/backend-i18n
Feb 4, 2026
Merged

feat: backend i18n#2842
Calcium-Ion merged 4 commits into
mainfrom
feat/backend-i18n

Conversation

@Calcium-Ion

@Calcium-Ion Calcium-Ion commented Feb 4, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

Release Notes

  • New Features
    • Added comprehensive internationalization support enabling the app to display in multiple languages (Chinese, English, French, Japanese, Russian, Vietnamese).
    • Added language preference settings that users can configure and synchronize across all their devices.
    • API responses and error messages now display in the user's preferred language.

…ference

- Add go-i18n library for internationalization
- Create i18n package with translation keys and YAML locale files (zh/en)
- Implement i18n middleware for language detection from user settings and Accept-Language header
- Add Language field to UserSetting DTO
- Update API response helpers with i18n support (ApiErrorI18n, ApiSuccessI18n)
- Migrate hardcoded messages in token, redemption, and user controllers
- Add frontend language preference settings component
- Sync language preference across header selector and user settings
- Auto-restore user language preference on login
- Change default language fallback to English instead of Chinese
- Add ErrRedeemFailed typed error for model layer translation
- Migrate remaining hardcoded messages in controller/user.go
- Add translation keys: redeem.failed, user.create_default_token_error, common.uuid_duplicate, common.invalid_input
The i18n middleware runs before UserAuth, so user settings weren't
available when language was detected. Now GetLangFromContext checks
user settings first (set by UserAuth) before falling back to the
language set by middleware or Accept-Language header.
@coderabbitai

coderabbitai Bot commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Implements comprehensive internationalization (i18n) support with go-i18n backend integration, context-aware language detection, API response helpers for localized messages, controller refactoring to use i18n, and frontend language preference settings with synchronization to user profile.

Changes

Cohort / File(s) Summary
Backend i18n Infrastructure
i18n/i18n.go, i18n/keys.go, i18n/locales/...yaml
Introduces complete i18n system with bundle setup, localizer caching, language detection from user settings/headers, and 150+ translation constants across domains. Supports zh/en with lazy initialization and thread-safe access.
API Response Helpers
common/gin.go, constant/context_key.go
Adds ApiErrorI18n and ApiSuccessI18n functions for localized responses; introduces global TranslateMessage hook and ContextKeyLanguage constant for language storage in request context.
Controller Refactoring
controller/token.go, controller/redemption.go, controller/user.go
Replaces hardcoded error messages with i18n-based ApiErrorI18n calls; centralizes validation (validateExpiredTime signature changed to accept context and return translated message); updates 50+ error paths to localized responses.
Middleware & Model Support
middleware/i18n.go, model/user_cache.go, model/redemption.go, main.go
Adds i18n middleware for language detection priority (user setting → Accept-Language header → default); helper GetUserLanguage for caching; initializes i18n system and registers language loader during startup.
Frontend Language Settings
web/src/components/settings/personal/cards/PreferencesSettings.jsx, web/src/context/User/index.jsx, web/src/hooks/common/useHeaderBar.js
New PreferencesSettings component with language selector; syncs user language preference to backend via PUT /api/user/self; extends UserProvider to sync saved language with i18n on load; updates handleLanguageChange to persist preference.
Frontend Localization Integration
web/src/hooks/.../useTokensData.jsx, web/src/hooks/.../useRedemptionsData.jsx, web/src/hooks/.../useUsersData.jsx, web/src/hooks/.../useChannelsData.jsx
Replaces hardcoded success messages with translated strings using i18n t() function across token, redemption, user, and channel management hooks.
Locale Files
web/src/i18n/locales/en.json, web/src/i18n/locales/zh.json, web/src/i18n/locales/fr.json, web/src/i18n/locales/ja.json, web/src/i18n/locales/ru.json, web/src/i18n/locales/vi.json
Adds 28+ translations across multiple languages for language preferences, performance monitoring controls, and system feature labels; existing keys remain unchanged.
Data Model & Dependencies
dto/user_settings.go, go.mod
Extends UserSetting struct with Language field (json: language,omitempty); adds go-i18n v2.6.1 and samber/hot v0.11.0 dependencies; bumps indirect modules for text/sync/yaml support.

Sequence Diagram

sequenceDiagram
    participant Client as Client<br/>(Browser)
    participant Server as Server<br/>(Gin)
    participant i18n as i18n<br/>Module
    participant DB as Database
    participant Loader as UserLangLoader<br/>(Cache)

    Client->>Server: Request (Accept-Language header)
    Server->>i18n: I18n Middleware: DetectLanguage()
    
    alt User Setting Available
        i18n->>DB: Retrieve from UserContext.Setting
        DB-->>i18n: Language (zh/en)
    else Check User Cache
        i18n->>Loader: GetUserLanguage(userId)
        Loader->>DB: Query user cache
        DB-->>Loader: Cached language
        Loader-->>i18n: Language string
    else Parse Accept-Language
        i18n->>i18n: ParseAcceptLanguage(header)
        i18n-->>i18n: Normalized lang (zh/en)
    else Default
        i18n-->>i18n: DefaultLang (en)
    end
    
    i18n->>Server: Store lang in context (ContextKeyLanguage)
    Server-->>Client: Process request with language
    
    Server->>i18n: T(context, "token.name_too_long", args)
    i18n->>i18n: GetLocalizer(detectedLang)
    i18n-->>Server: Translated message
    Server-->>Client: JSON response with localized message

    Client->>Server: PUT /api/user/self {language: "zh"}
    Server->>DB: Update user.setting.language
    DB-->>Server: Confirmed
    Server-->>Client: Success response
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

  • PR #1341 — Refactors user settings into typed dto.UserSetting and adds context helpers; this PR extends that DTO with Language field and integrates i18n context handling, building directly on those foundational changes.
  • PR #2808 — Extends i18n system with subscription-related translations and integrates i18n usage across controllers; overlaps on i18n infrastructure expansion and locale key additions.
  • PR #1976 — Adds i18n translation keys and locale files for topup/billing features; both PRs expand translation coverage and controller i18n integration in the same domains.

Suggested Labels

enhancement, i18n, localization

Suggested Reviewers

  • creamlike1024

Poem

🐰 A rabbit hops through languages bright,
From East to West, with translations in sight,
Each message now speaks in many a tongue,
The i18n song of the global is sung! 🌍✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/backend-i18n

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Calcium-Ion
Calcium-Ion merged commit 3c7687f into main Feb 4, 2026
1 check was pending
@Calcium-Ion
Calcium-Ion deleted the feat/backend-i18n branch February 22, 2026 07:57
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Apr 28, 2026
11 tasks
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.

1 participant