2024-09-08 17:40:05 +03:00
|
|
|
import i18next from "i18next";
|
|
|
|
import Backend from "i18next-fs-backend";
|
2024-09-08 18:01:08 +03:00
|
|
|
import options from "./options.js";
|
2024-09-08 18:23:06 +03:00
|
|
|
import sql_init from "./sql_init.js";
|
2024-11-02 11:49:33 +02:00
|
|
|
import { join } from "path";
|
|
|
|
import { getResourceDir } from "./utils.js";
|
2024-11-30 10:13:39 +02:00
|
|
|
import hidden_subtree from "./hidden_subtree.js";
|
2024-09-08 17:40:05 +03:00
|
|
|
|
2025-03-04 20:48:36 +02:00
|
|
|
export interface Locale {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
/** `true` if the language is a right-to-left one, or `false` if it's left-to-right. */
|
|
|
|
rtl?: boolean;
|
|
|
|
}
|
|
|
|
|
2025-03-04 20:51:22 +02:00
|
|
|
const LOCALES: Locale[] = [
|
|
|
|
{
|
|
|
|
id: "en",
|
|
|
|
name: "English"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "de",
|
|
|
|
name: "Deutsch"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "es",
|
|
|
|
name: "Español"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "fr",
|
|
|
|
name: "Français"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "cn",
|
|
|
|
name: "简体中文"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "tw",
|
|
|
|
name: "繁體中文"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "ro",
|
|
|
|
name: "Română"
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Right to left languages
|
|
|
|
*
|
|
|
|
* Currently they are only for setting the language of text notes.
|
|
|
|
*/
|
|
|
|
{ // Arabic
|
|
|
|
id: "ar",
|
|
|
|
name: "اَلْعَرَبِيَّةُ",
|
|
|
|
rtl: true
|
|
|
|
},
|
|
|
|
{ // Hebrew
|
|
|
|
id: "he",
|
|
|
|
name: "עברית",
|
|
|
|
rtl: true
|
|
|
|
},
|
|
|
|
{ // Kurdish
|
|
|
|
id: "ku",
|
|
|
|
name: "کوردی",
|
|
|
|
rtl: true
|
|
|
|
},
|
|
|
|
{ // Persian
|
|
|
|
id: "fa",
|
|
|
|
name: "فارسی",
|
|
|
|
rtl: true
|
|
|
|
}
|
|
|
|
].sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
|
2024-09-08 17:40:05 +03:00
|
|
|
export async function initializeTranslations() {
|
2025-01-09 18:07:02 +02:00
|
|
|
const resourceDir = getResourceDir();
|
2024-10-13 14:24:46 +03:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
// Initialize translations
|
|
|
|
await i18next.use(Backend).init({
|
|
|
|
lng: getCurrentLanguage(),
|
|
|
|
fallbackLng: "en",
|
|
|
|
ns: "server",
|
|
|
|
backend: {
|
|
|
|
loadPath: join(resourceDir, "translations/{{lng}}/{{ns}}.json")
|
|
|
|
}
|
|
|
|
});
|
2024-09-08 18:01:08 +03:00
|
|
|
}
|
|
|
|
|
2025-03-04 20:48:36 +02:00
|
|
|
export function getLocales(): Locale[] {
|
2025-03-04 20:51:22 +02:00
|
|
|
return LOCALES;
|
2025-02-20 12:27:33 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 18:01:08 +03:00
|
|
|
function getCurrentLanguage() {
|
2025-01-09 18:07:02 +02:00
|
|
|
let language;
|
|
|
|
if (sql_init.isDbInitialized()) {
|
|
|
|
language = options.getOptionOrNull("locale");
|
|
|
|
}
|
2024-09-08 18:01:08 +03:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!language) {
|
|
|
|
console.info("Language option not found, falling back to en.");
|
|
|
|
language = "en";
|
|
|
|
}
|
2024-09-08 18:01:08 +03:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return language;
|
2024-10-23 19:56:06 +03:00
|
|
|
}
|
|
|
|
|
2024-11-30 10:13:39 +02:00
|
|
|
export async function changeLanguage(locale: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
await i18next.changeLanguage(locale);
|
|
|
|
hidden_subtree.checkHiddenSubtree(true, { restoreNames: true });
|
2024-10-23 19:56:06 +03:00
|
|
|
}
|