2024-08-11 08:12:01 +03:00
|
|
|
import options from "./options.js";
|
2025-02-28 08:26:48 +01:00
|
|
|
import i18next from "i18next";
|
|
|
|
|
import i18nextHttpBackend from "i18next-http-backend";
|
2025-03-04 17:43:24 +02:00
|
|
|
import server from "./server.js";
|
2025-04-18 11:00:48 +03:00
|
|
|
import type { Locale } from "@triliumnext/commons";
|
2025-08-18 14:19:38 +03:00
|
|
|
import { initReactI18next } from "react-i18next";
|
2025-03-04 17:43:24 +02:00
|
|
|
|
|
|
|
|
let locales: Locale[] | null;
|
|
|
|
|
|
2025-07-27 21:47:30 +03:00
|
|
|
/**
|
|
|
|
|
* A deferred promise that resolves when translations are initialized.
|
|
|
|
|
*/
|
2025-07-28 18:58:26 +03:00
|
|
|
export let translationsInitializedPromise = $.Deferred();
|
2025-07-27 21:47:30 +03:00
|
|
|
|
2024-08-11 08:12:01 +03:00
|
|
|
export async function initLocale() {
|
2024-12-19 20:52:43 +02:00
|
|
|
const locale = (options.get("locale") as string) || "en";
|
2024-08-11 08:12:01 +03:00
|
|
|
|
2025-03-04 17:43:24 +02:00
|
|
|
locales = await server.get<Locale[]>("options/locales");
|
|
|
|
|
|
2025-08-18 14:19:38 +03:00
|
|
|
i18next.use(initReactI18next);
|
2025-01-09 18:07:02 +02:00
|
|
|
await i18next.use(i18nextHttpBackend).init({
|
|
|
|
|
lng: locale,
|
|
|
|
|
fallbackLng: "en",
|
|
|
|
|
backend: {
|
|
|
|
|
loadPath: `${window.glob.assetPath}/translations/{{lng}}/{{ns}}.json`
|
|
|
|
|
},
|
|
|
|
|
returnEmptyString: false
|
|
|
|
|
});
|
2025-07-27 21:47:30 +03:00
|
|
|
|
|
|
|
|
translationsInitializedPromise.resolve();
|
2024-08-11 08:12:01 +03:00
|
|
|
}
|
2024-07-20 09:42:55 +03:00
|
|
|
|
2025-03-04 17:43:24 +02:00
|
|
|
export function getAvailableLocales() {
|
|
|
|
|
if (!locales) {
|
2025-03-04 20:48:36 +02:00
|
|
|
throw new Error("Tried to load list of locales, but localization is not yet initialized.")
|
2025-03-04 17:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return locales;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 22:45:38 +02:00
|
|
|
/**
|
|
|
|
|
* Finds the given locale by ID.
|
|
|
|
|
*
|
|
|
|
|
* @param localeId the locale ID to search for.
|
|
|
|
|
* @returns the corresponding {@link Locale} or `null` if it was not found.
|
|
|
|
|
*/
|
2025-03-04 23:13:23 +02:00
|
|
|
export function getLocaleById(localeId: string | null | undefined) {
|
|
|
|
|
if (!localeId) return null;
|
|
|
|
|
return locales?.find((l) => l.id === localeId) ?? null;
|
2025-03-04 22:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-07 16:56:36 +08:00
|
|
|
export const t = i18next.t;
|
2025-02-28 08:30:35 +01:00
|
|
|
export const getCurrentLanguage = () => i18next.language;
|