Skip to content

Commit

Permalink
fix: default browser language detection (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 9, 2025
1 parent c76bd47 commit 34edb7b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
44 changes: 44 additions & 0 deletions e2e/locale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect, test } from "@playwright/test";

import dict from "../src/i18n/i18n";

test.describe("Locales", () => {
[
{
locale: "en-GB",
dict: dict.en,
},
{
locale: "en-US",
dict: dict.en,
},
{
locale: "de-DE",
dict: dict.de,
},
{
locale: "es-ES",
dict: dict.es,
},
{
locale: "zh-Hans-CN",
dict: dict.zh,
},
{
locale: "ja-JP",
dict: dict.ja,
},
].forEach(({ locale, dict }) => {
test(`Default language for locale ${locale}`, async ({ browser }) => {
const context = await browser.newContext({
locale,
});
const page = await context.newPage();

await page.goto("/");
await expect(page.getByTestId("create-swap-title")).toHaveText(
dict.create_swap,
);
});
});
});
2 changes: 1 addition & 1 deletion src/context/Global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ const GlobalProvider = (props: { children: JSX.Element }) => {

// i18n
createEffect(() => {
setI18n(i18nConfigured() || i18nUrl());
setI18n(detectLanguage(i18nConfigured() || i18nUrl()));
});
const dictLocale = createMemo(
() => flatten(dict[i18n() || config.defaultLanguage]) as never,
Expand Down
28 changes: 15 additions & 13 deletions src/i18n/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@ export const getNavigatorLanguage = (language: string): string => {

export const detectLanguage = (
i18nConfigured: string | null,
i18nUrl: string | null,
setI18nUrl: Setter<string>,
i18nUrl?: string | null,
setI18nUrl?: Setter<string>,
): string => {
if (i18nConfigured !== null) {
return i18nConfigured;
}

const urlParam = getUrlParam("lang");
if (urlParam) {
if (isValidLang(urlParam)) {
log.info("Using language URL parameter:", urlParam);
setI18nUrl(urlParam);
return urlParam;
} else {
log.warn("Invalid language URL parameter:", urlParam);
if (i18nUrl !== undefined) {
const urlParam = getUrlParam("lang");
if (urlParam) {
if (isValidLang(urlParam)) {
log.info("Using language URL parameter:", urlParam);
setI18nUrl(urlParam);
return urlParam;
} else {
log.warn("Invalid language URL parameter:", urlParam);
}
}
}

if (i18nUrl !== null) {
return i18nUrl;
if (i18nUrl !== null && i18nUrl !== undefined) {
return i18nUrl;
}
}

return getNavigatorLanguage(navigator.language);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ const Create = () => {
<Show when={wasmSupported()} fallback={<ErrorWasm />}>
<div class="frame">
<SettingsCog />
<h2>{t("create_swap")}</h2>
<h2 data-testid="create-swap-title">{t("create_swap")}</h2>
<p>
{t("create_swap_subline")} <br />
{t("send")} {t("min")}:{" "}
Expand Down

0 comments on commit 34edb7b

Please sign in to comment.