Skip to content

Commit a3c47f7

Browse files
committed
Stronger locale detection
Fixes #38
1 parent 813aed0 commit a3c47f7

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@lingui/core": "^5.5.1",
20+
"@lingui/detect-locale": "^5.5.1",
2021
"@lingui/react": "^5.5.1",
2122
"@tailwindcss/vite": "^4.1.15",
2223
"@vercel/analytics": "^1.5.0",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/get_language.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { detect, fromStorage, fromNavigator } from "@lingui/detect-locale";
2+
13
import supportedLocales, { type SupportedLocale } from "./supported_locales";
24

5+
const DEFAULT_FALLBACK = "en";
6+
37
export function isSupportedLocale(locale: string): locale is SupportedLocale {
48
return (supportedLocales as readonly string[]).includes(locale);
59
}
610

711
export function getLanguage(): SupportedLocale {
8-
const lang = navigator.language;
12+
const lang =
13+
detect(fromStorage("lang"), fromNavigator(), DEFAULT_FALLBACK) ??
14+
DEFAULT_FALLBACK;
915
const sanitizedLang = lang.split("-")[0];
10-
return isSupportedLocale(sanitizedLang) ? sanitizedLang : "en";
16+
return isSupportedLocale(sanitizedLang) ? sanitizedLang : DEFAULT_FALLBACK;
1117
}

0 commit comments

Comments
 (0)