Skip to content

Commit 9162300

Browse files
committed
Some TypeScript magic
1 parent d8c3f9e commit 9162300

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

src/main.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import { i18n } from "@lingui/core";
77
import App from "./App.tsx";
88

99
import "./index.css";
10-
import { dynamicActivate } from "./utils/i18nLocaleModuleLoading.ts";
11-
import { getSanitizedBrowserLanguageIfSupportedOrEnglish } from "./utils/getSanitizedBrowserLanguageIfSupportedOrEnglish.ts";
10+
import { dynamicActivate } from "./utils/i18n_loader.ts";
11+
import { getLanguage } from "./utils/get_language.ts";
1212

13-
const sanitizedBrowserLanguageIfSupportedOrEnglish =
14-
getSanitizedBrowserLanguageIfSupportedOrEnglish();
13+
const sanitizedBrowserLanguageIfSupportedOrEnglish = getLanguage();
1514
await dynamicActivate(sanitizedBrowserLanguageIfSupportedOrEnglish);
1615

1716
const isAnalyticsEnabled =

src/utils/getSanitizedBrowserLanguageIfSupportedOrEnglish.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/utils/get_language.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import supportedLocales, { type SupportedLocale } from "./supported_locales";
2+
3+
export function isSupportedLocale(locale: string): locale is SupportedLocale {
4+
return (supportedLocales as readonly string[]).includes(locale);
5+
}
6+
7+
export function getLanguage(): SupportedLocale {
8+
const lang = navigator.language;
9+
const sanitizedLang = lang.split("-")[0];
10+
return isSupportedLocale(sanitizedLang) ? sanitizedLang : "en";
11+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { i18n, type Messages } from "@lingui/core";
2+
import type { SupportedLocale } from "./supported_locales";
23

3-
export async function dynamicActivate(locale: string) {
4+
export async function dynamicActivate(locale: SupportedLocale) {
45
const { messages } = (await import(`./../locales/${locale}/messages.po`)) as {
56
messages: Messages;
67
};

src/utils/supportedLocales.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/utils/supported_locales.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const supportedLocales = ["en", "fr"] as const;
2+
export type SupportedLocale = (typeof supportedLocales)[number];
3+
4+
export default supportedLocales;

0 commit comments

Comments
 (0)