fix(webui): follow browser language on first visit (#6591)

Co-authored-by: ovo-agentx <351813579+ovo-agentx@users.noreply.github.com>
This commit is contained in:
chengyongruandovo-agentx authored and GitHub committed 2026-11-07 00:46:09 +08:00
1 parent 0ee302253f
commit 12c7d116a0
6 files changed
+108 -8

No files matched your search

+19 -2
View File
@@ -2,7 +2,7 @@ import { useState } from "react";
import { useTranslation } from "react-i18next";
import { currentLocale, setAppLanguage } from "@/i18n";
import { supportedLocales, type SupportedLocale } from "@/i18n/config";
import { persistLocale, supportedLocales, type SupportedLocale } from "@/i18n/config";
import { cn } from "@/lib/utils";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
@@ -27,7 +27,24 @@ export function LanguageSwitcher({ className }: { className?: string }) {
onEscapeKeyDown={() => setPointerFocus(false)}
>
{supportedLocales.map((option) => (
<SelectItem key={option.code} value={option.code}>{option.nativeLabel}</SelectItem>
<SelectItem
key={option.code}
value={option.code}
onPointerUp={(event) => {
if (event.pointerType === "mouse" && option.code === currentLocale()) persistLocale(option.code);
}}
onClick={() => {
if (option.code === currentLocale()) persistLocale(option.code);
}}
onKeyDown={(event) => {
if (option.code !== currentLocale()) return;
if (event.key === "Enter") persistLocale(option.code);
if (event.key === " ") {
// Radix prevents the space key only when it selects, not during typeahead.
queueMicrotask(() => { if (event.defaultPrevented) persistLocale(option.code); });
}
}}
>{option.nativeLabel}</SelectItem>
))}
</SelectContent>
</Select>
+3 -1
View File
@@ -62,7 +62,9 @@ function readStoredLocale(): SupportedLocale | null {
}
export function resolveInitialLocale(): SupportedLocale {
return readStoredLocale() ?? defaultLocale;
return readStoredLocale() ?? normalizeLocale(
typeof navigator === "undefined" ? null : navigator.languages?.[0] || navigator.language,
);
}
export function persistLocale(locale: SupportedLocale): void {
+1 -1
View File
@@ -122,12 +122,12 @@ export async function setAppLanguage(locale: SupportedLocale): Promise<void> {
await initializeI18n();
await loadLocaleResources(locale);
await i18n.changeLanguage(locale);
persistLocale(locale);
}
function syncLocaleSideEffects(language: string) {
const locale = normalizeLocale(language);
applyDocumentLocale(locale);
persistLocale(locale);
}
export default i18n;
+24 -4
View File
@@ -478,12 +478,32 @@ describe("webui i18n", () => {
}
});
it("defaults to English until the user chooses another language", () => {
it("follows the browser until the user chooses another language", () => {
localStorage.removeItem(LOCALE_STORAGE_KEY);
expect(resolveInitialLocale()).toBe("en");
vi.stubGlobal("navigator", { languages: ["zh-CN"], language: "zh-CN" });
try {
expect(resolveInitialLocale()).toBe("zh-CN");
localStorage.setItem(LOCALE_STORAGE_KEY, "fr");
expect(resolveInitialLocale()).toBe("fr");
} finally {
vi.unstubAllGlobals();
}
});
localStorage.setItem(LOCALE_STORAGE_KEY, "zh-CN");
expect(resolveInitialLocale()).toBe("zh-CN");
it("falls back to English for an unsupported browser language", () => {
localStorage.removeItem(LOCALE_STORAGE_KEY);
vi.stubGlobal("navigator", { languages: ["de-DE"], language: "de-DE" });
try {
expect(resolveInitialLocale()).toBe("en");
} finally {
vi.unstubAllGlobals();
}
});
it("persists an explicit selection even when it matches the current language", async () => {
localStorage.removeItem(LOCALE_STORAGE_KEY);
await setAppLanguage("en");
expect(localStorage.getItem(LOCALE_STORAGE_KEY)).toBe("en");
});
it("lists each language by its native name", async () => {