Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
LangZhCN = "zh-CN"
LangZhTW = "zh-TW"
LangEn = "en"
LangPt = "pt"
DefaultLang = LangEn // Fallback to English if language not supported
)

Expand All @@ -40,7 +41,7 @@ func Init() error {
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)

// Load embedded translation files
files := []string{"locales/zh-CN.yaml", "locales/zh-TW.yaml", "locales/en.yaml"}
files := []string{"locales/zh-CN.yaml", "locales/zh-TW.yaml", "locales/en.yaml", "locales/pt.yaml"}
for _, file := range files {
_, err := bundle.LoadMessageFileFS(localeFS, file)
if err != nil {
Expand All @@ -53,6 +54,7 @@ func Init() error {
localizers[LangZhCN] = i18n.NewLocalizer(bundle, LangZhCN)
localizers[LangZhTW] = i18n.NewLocalizer(bundle, LangZhTW)
localizers[LangEn] = i18n.NewLocalizer(bundle, LangEn)
localizers[LangPt] = i18n.NewLocalizer(bundle, LangPt)

// Set the TranslateMessage function in common package
common.TranslateMessage = T
Expand Down Expand Up @@ -207,6 +209,8 @@ func normalizeLang(lang string) string {
return LangZhTW
case strings.HasPrefix(lang, "zh"):
return LangZhCN
case strings.HasPrefix(lang, "pt"):
return LangPt
case strings.HasPrefix(lang, "en"):
return LangEn
default:
Expand All @@ -216,7 +220,7 @@ func normalizeLang(lang string) string {

// SupportedLanguages returns a list of supported language codes
func SupportedLanguages() []string {
return []string{LangZhCN, LangZhTW, LangEn}
return []string{LangZhCN, LangZhTW, LangEn, LangPt}
}

// IsSupported checks if a language code is supported
Expand Down
265 changes: 265 additions & 0 deletions i18n/locales/pt.yaml

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion web/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"build:check": "tsc -b && rsbuild build",
"typecheck": "tsc -b",
"lint": "eslint .",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"preview": "rsbuild preview",
"format:check": "prettier --check .",
"format": "prettier --write .",
Expand Down Expand Up @@ -81,22 +84,28 @@
"@tanstack/react-query-devtools": "^5.100.14",
"@tanstack/react-router-devtools": "^1.167.0",
"@tanstack/router-plugin": "^1.168.11",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.2",
"@testing-library/user-event": "^14.6.1",
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
"@types/node": "^25.9.1",
"@types/react": "^19.2.15",
"@types/react-dom": "^19.2.3",
"@vitest/coverage-v8": "^3.2.4",
"@xyflow/react": "^12.10.2",
"embla-carousel-react": "^8.6.0",
"eslint": "^10.4.0",
"eslint-plugin-react-hooks": "^7.1.1",
"eslint-plugin-react-refresh": "^0.5.2",
"globals": "^17.6.0",
"jsdom": "^29.1.1",
"knip": "^6.14.2",
"prettier": "^3.8.3",
"prettier-plugin-tailwindcss": "^0.8.0",
"shadcn": "^4.8.0",
"typescript": "~6.0.3",
"typescript-eslint": "^8.59.4"
"typescript-eslint": "^8.59.4",
"vitest": "^3.2.4"
},
"overrides": {
"brace-expansion": "2.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (C) 2023-2026 QuantumNous

Integration regression test for the bug:
- User selects "Português" in the top nav menu
- i18next stores "pt" (the canonical code) in localStorage
- User navigates to /profile
- The <Select> showed "English" because normalizeInterfaceLanguage
was case-sensitive: it lowercased the input to "pt" and then compared
against option codes case-sensitively, but the options array uses
mixed-case canonical codes. Fixed: the function now matches
case-insensitively and returns the canonical mixed-case code.
*/
import { describe, expect, test, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import { I18nextProvider } from 'react-i18next'
import { LanguagePreferencesCard } from '../components/language-preferences-card'
import { loadI18n } from '../../../test/setup'

vi.mock('@/stores/auth-store', () => ({
useAuthStore: () => ({ auth: { user: { id: 1, username: 'giovanni' } } }),
}))
vi.mock('../api', () => ({
updateUserLanguage: vi.fn(async () => ({ success: true })),
}))
vi.mock('../lib', () => ({
parseUserSettings: (s?: Record<string, unknown> | null) => s ?? {},
}))

describe('LanguagePreferencesCard integration: case-insensitive normalize', () => {
test('Select trigger shows "Português" when i18n.language is "pt"', async () => {
// Simulate the user state: i18n.language is "pt" (the canonical code)
const i18n = await loadI18n('pt')
expect(i18n.language).toBe('pt')

// Render the card. props.profile is null so the savedLanguage
// falls back to i18n.language.
render(
<I18nextProvider i18n={i18n}>
<LanguagePreferencesCard profile={null} onProfileUpdate={() => {}} />
</I18nextProvider>,
)

// The select trigger should display the Portuguese option label,
// NOT fall back to "English". Base UI's <Select.Trigger> renders
// a <button role="combobox"> without an accessible name; query
// by role and assert its visible text content.
const selectTrigger = screen.getByRole('combobox')
expect(selectTrigger).toHaveTextContent('Português')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright (C) 2023-2026 QuantumNous
*/
import { describe, expect, test } from 'vitest'
import { normalizeInterfaceLanguage } from '../languages'

describe('normalizeInterfaceLanguage (case-insensitive matching)', () => {
test('returns canonical code for exact matches', () => {
expect(normalizeInterfaceLanguage('en')).toBe('en')
expect(normalizeInterfaceLanguage('pt')).toBe('pt')
expect(normalizeInterfaceLanguage('zh')).toBe('zh')
})

test('returns canonical code for case-insensitive matches (i18next-style)', () => {
// i18next's browser-languagedetector may store the language in mixed
// case (e.g. 'Pt' or 'PT'); the function must resolve to the canonical
// option code regardless of input casing.
expect(normalizeInterfaceLanguage('PT')).toBe('pt')
expect(normalizeInterfaceLanguage('Pt')).toBe('pt')
})

test('handles zh variants', () => {
expect(normalizeInterfaceLanguage('zh-CN')).toBe('zh')
expect(normalizeInterfaceLanguage('zh-TW')).toBe('zh')
expect(normalizeInterfaceLanguage('ZH')).toBe('zh')
})

test('falls back to "en" for unknown languages', () => {
expect(normalizeInterfaceLanguage('xx')).toBe('en')
expect(normalizeInterfaceLanguage('klingon')).toBe('en')
})

test('handles empty / null / undefined', () => {
expect(normalizeInterfaceLanguage()).toBe('en')
expect(normalizeInterfaceLanguage(null)).toBe('en')
expect(normalizeInterfaceLanguage(undefined)).toBe('en')
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
})
22 changes: 22 additions & 0 deletions web/default/src/i18n/__tests__/pt-fallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright (C) 2023-2026 QuantumNous

Verifies that i18next's region-less fallback resolves to the canonical
'pt' entry. This is the explicit test of the fallback machinery described
in src/i18n/config.ts (supportedLngs includes 'pt').
*/
import { describe, expect, test } from 'vitest'
import { loadI18n } from '../../test/setup'

describe('i18next fallback resolves region-less codes to canonical', () => {
test('changeLanguage("pt") lands on "pt" exactly (no lowercasing)', async () => {
const i18n = await loadI18n('en')
await i18n.changeLanguage('pt')
expect(i18n.language).toBe('pt')
})

test('loadI18n("pt") lands on "pt" exactly', async () => {
const i18n = await loadI18n('pt')
expect(i18n.language).toBe('pt')
})
})
4 changes: 3 additions & 1 deletion web/default/src/i18n/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { initReactI18next } from 'react-i18next'
import en from './locales/en.json'
import fr from './locales/fr.json'
import ja from './locales/ja.json'
import pt from './locales/pt.json'
import ru from './locales/ru.json'
import vi from './locales/vi.json'
import zh from './locales/zh.json'
Expand All @@ -32,6 +33,7 @@ export const resources = {
fr,
ru,
ja,
pt,
vi,
} as const

Expand All @@ -41,7 +43,7 @@ i18n
.init({
resources,
fallbackLng: 'en',
supportedLngs: ['en', 'zh', 'fr', 'ru', 'ja', 'vi'],
supportedLngs: ['en', 'zh', 'fr', 'ru', 'ja', 'pt', 'vi'],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
load: 'languageOnly', // Convert zh-CN -> zh
nsSeparator: false, // Allow literal colons in keys (e.g., URLs, labels)
debug: import.meta.env.DEV,
Expand Down
11 changes: 8 additions & 3 deletions web/default/src/i18n/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const INTERFACE_LANGUAGE_OPTIONS = [
{ code: 'ru', label: 'Русский' },
{ code: 'ja', label: '日本語' },
{ code: 'vi', label: 'Tiếng Việt' },
{ code: 'pt', label: 'Português' },
] as const

export type InterfaceLanguageCode =
Expand All @@ -35,7 +36,11 @@ export function normalizeInterfaceLanguage(value?: string | null): string {
const normalized = value.trim().replace(/_/g, '-').toLowerCase()
if (normalized.startsWith('zh')) return 'zh'

return INTERFACE_LANGUAGE_OPTIONS.some((lang) => lang.code === normalized)
? normalized
: 'en'
// Match case-insensitively against the option codes so users with
// mixed-case stored values (e.g. 'pt' from i18next) still resolve to
// the canonical option code.
const matched = INTERFACE_LANGUAGE_OPTIONS.find(
(lang) => lang.code.toLowerCase() === normalized,
)
return matched ? matched.code : 'en'
}
Loading