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:
1 parent
0ee302253f
commit
12c7d116a0
6 files changed
+108
-8
No files matched your search
@@ -45,6 +45,40 @@ for (const locale of locales) {
|
||||
});
|
||||
}
|
||||
|
||||
for (const key of ["Enter", "Space"]) {
|
||||
test(`reselecting the detected language with ${key} saves it`, async ({ page }) => {
|
||||
await page.goto("/gallery.html");
|
||||
expect(await page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBeNull();
|
||||
const switcher = page.locator(".gallery-header").getByRole("combobox");
|
||||
await switcher.focus();
|
||||
await page.keyboard.press("Enter");
|
||||
await page.keyboard.press(key);
|
||||
await expect.poll(() => page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBe("en");
|
||||
await page.reload();
|
||||
await expect(switcher).toHaveText("English");
|
||||
});
|
||||
}
|
||||
|
||||
test.describe("touch language selection", () => {
|
||||
test.use({ hasTouch: true });
|
||||
|
||||
test("reselecting the detected language saves it", async ({ page }) => {
|
||||
await page.goto("/gallery.html");
|
||||
await page.locator(".gallery-header").getByRole("combobox").tap();
|
||||
await page.getByRole("option", { name: "English", exact: true }).tap();
|
||||
await expect.poll(() => page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBe("en");
|
||||
});
|
||||
});
|
||||
|
||||
test("typing a space in the language picker does not save a preference", async ({ page }) => {
|
||||
await page.goto("/gallery.html");
|
||||
const switcher = page.locator(".gallery-header").getByRole("combobox");
|
||||
await switcher.focus();
|
||||
await page.keyboard.press("Enter");
|
||||
await page.keyboard.type("e ");
|
||||
expect(await page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBeNull();
|
||||
});
|
||||
|
||||
test("gallery localizes an existing error and retries in the selected language", async ({ page }) => {
|
||||
await page.route("**/__nanobot_components", (route) => route.fulfill({ status: 503, body: "unavailable" }));
|
||||
await page.goto("/gallery.html");
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
test.use({ locale: "zh-CN" });
|
||||
|
||||
test("startup follows the browser without saving an automatic preference", async ({ page }) => {
|
||||
let release!: () => void;
|
||||
const pending = new Promise<void>(resolve => { release = resolve; });
|
||||
await page.route("**/src/main.tsx", async route => {
|
||||
await pending;
|
||||
await route.continue();
|
||||
});
|
||||
try {
|
||||
await page.goto("/", { waitUntil: "commit" });
|
||||
await expect(page.locator("html")).toHaveAttribute("lang", "zh-CN");
|
||||
await expect(page.locator("[data-boot-copy]")).toHaveText("正在加载 nanobot…");
|
||||
release();
|
||||
await expect(page.locator("[data-boot-copy]")).toHaveCount(0);
|
||||
await expect(page.locator("html")).toHaveAttribute("lang", "zh-CN");
|
||||
expect(await page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBeNull();
|
||||
await page.reload();
|
||||
await expect(page.locator("[data-boot-copy]")).toHaveCount(0);
|
||||
await expect(page.locator("html")).toHaveAttribute("lang", "zh-CN");
|
||||
expect(await page.evaluate(() => localStorage.getItem("nanobot.locale"))).toBeNull();
|
||||
} finally {
|
||||
release();
|
||||
}
|
||||
});
|
||||
@@ -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>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in new issue
Block a user