34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
/**
|
|
* Picks the interface's language and loads its messages into Lingui's global
|
|
* `i18n`, which the components' `t` and `Trans` read. `main.tsx` calls it once
|
|
* before rendering.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { i18n } from "@lingui/core";
|
|
import { isLocale, type Locale } from "../shared/locale.ts";
|
|
|
|
/**
|
|
* Returns the stored locale, else French for a French browser, else English.
|
|
*/
|
|
function detectLocale(): Locale {
|
|
const stored = localStorage.getItem("locale");
|
|
if (isLocale(stored)) return stored;
|
|
return navigator.language.startsWith("fr") ? "fr" : "en";
|
|
}
|
|
|
|
/**
|
|
* Loads the messages of `locale`, the detected one by default, then activates
|
|
* it and stores it for the next visit. There is no language switch in the
|
|
* interface yet, so the stored one comes from the browser's language.
|
|
*/
|
|
export async function loadCatalog(
|
|
locale: Locale = detectLocale(),
|
|
): Promise<void> {
|
|
const { messages } = await import(`../locales/${locale}.po`);
|
|
i18n.load(locale, messages);
|
|
i18n.activate(locale);
|
|
localStorage.setItem("locale", locale);
|
|
}
|