-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: browser language detection (#301)
* feat: browser language detection works for chromium, did not work for firefox. closes #270 * only set if not null * do it properly with tests * prettier * refactor: rename i18nCached to i18nConfigured --------- Co-authored-by: michael1011 <[email protected]>
- Loading branch information
1 parent
b8c9911
commit 8308f8c
Showing
6 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import log from "loglevel"; | ||
import locales from "./i18n"; | ||
import { defaultLanguage } from "../config"; | ||
import { i18nConfigured, setI18n } from "../signals"; | ||
|
||
export const getNavigatorLanguage = (language) => { | ||
if (language === undefined) { | ||
log.info( | ||
`browser language undefined; using default: ${defaultLanguage}`, | ||
); | ||
return defaultLanguage; | ||
} | ||
|
||
const lang = language.split("-")[0]; | ||
if (!Object.keys(locales).includes(lang)) { | ||
log.info( | ||
`browser language "${lang}" not found; using default: ${defaultLanguage}`, | ||
); | ||
return defaultLanguage; | ||
} | ||
|
||
log.info(`detected browser language ${lang}`); | ||
return lang; | ||
}; | ||
|
||
export const detectLanguage = () => { | ||
if (i18nConfigured() === null) { | ||
setI18n(getNavigatorLanguage(navigator.language)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { createMemo } from "solid-js"; | ||
import { translator, flatten, resolveTemplate } from "@solid-primitives/i18n"; | ||
import dict from "./i18n"; | ||
import { i18n } from "../signals"; | ||
import { defaultLanguage } from "../config"; | ||
import { i18n, i18nConfigured, setI18n } from "../signals"; | ||
|
||
const dictLocale = createMemo(() => flatten(dict[i18n()])); | ||
createMemo(() => setI18n(i18nConfigured())); | ||
|
||
const dictLocale = createMemo(() => flatten(dict[i18n() || defaultLanguage])); | ||
|
||
export default translator(dictLocale, resolveTemplate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe, expect } from "vitest"; | ||
import { defaultLanguage } from "../../src/config"; | ||
import { getNavigatorLanguage } from "../../src/i18n/detect"; | ||
|
||
describe("detect", () => { | ||
test.each` | ||
navigatorLanguage | expected | ||
${"en-US"} | ${"en"} | ||
${"en-UK"} | ${"en"} | ||
${"de-DE"} | ${"de"} | ||
${"de-AT"} | ${"de"} | ||
${"de-CH"} | ${"de"} | ||
${"de-LUX"} | ${"de"} | ||
${"es-ES"} | ${"es"} | ||
${"de"} | ${"de"} | ||
${"none-DE"} | ${defaultLanguage} | ||
${"ro-RO"} | ${defaultLanguage} | ||
${undefined} | ${defaultLanguage} | ||
`( | ||
"getNavigatorLanguage $navigatorLanguage <=> $expected", | ||
({ navigatorLanguage, expected }) => { | ||
expect(getNavigatorLanguage(navigatorLanguage)).toEqual(expected); | ||
}, | ||
); | ||
}); |
8308f8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
webapp – ./
webapp-boltz.vercel.app
webapp-nine-nu.vercel.app
staging.boltz.exchange
webapp-git-main-boltz.vercel.app
8308f8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
webapp-mainnet – ./
webapp-mainnet-git-main-boltz.vercel.app
webapp-mainnet-boltz.vercel.app
webapp-mainnet.vercel.app
beta.boltz.exchange