mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-28 10:32:27 +08:00
refactor(server): async import without side effects
This commit is contained in:
parent
e07a7d291b
commit
fa9d0be651
@ -4,15 +4,22 @@ import sql_init from "./sql_init.js";
|
|||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { getResourceDir } from "./utils.js";
|
import { getResourceDir } from "./utils.js";
|
||||||
import hidden_subtree from "./hidden_subtree.js";
|
import hidden_subtree from "./hidden_subtree.js";
|
||||||
import { LOCALES, type Locale } from "@triliumnext/commons";
|
import { LOCALES, type Locale, type LOCALE_IDS } from "@triliumnext/commons";
|
||||||
import dayjs, { Dayjs } from "dayjs";
|
import dayjs, { Dayjs } from "dayjs";
|
||||||
|
|
||||||
const DAYJS_LOCALE_MAP: Record<string, string> = {
|
const DAYJS_LOADER: Record<LOCALE_IDS, () => Promise<typeof import("dayjs/locale/en.js")>> = {
|
||||||
cn: "zh-cn",
|
"ar": () => import("dayjs/locale/ar.js"),
|
||||||
tw: "zh-tw"
|
"cn": () => import("dayjs/locale/zh-cn.js"),
|
||||||
};
|
"de": () => import("dayjs/locale/de.js"),
|
||||||
|
"en": () => import("dayjs/locale/en.js"),
|
||||||
let dayjsLocale: string;
|
"es": () => import("dayjs/locale/es.js"),
|
||||||
|
"fa": () => import("dayjs/locale/fa.js"),
|
||||||
|
"fr": () => import("dayjs/locale/fr.js"),
|
||||||
|
"he": () => import("dayjs/locale/he.js"),
|
||||||
|
"ku": () => import("dayjs/locale/ku.js"),
|
||||||
|
"ro": () => import("dayjs/locale/ro.js"),
|
||||||
|
"tw": () => import("dayjs/locale/tw.js")
|
||||||
|
}
|
||||||
|
|
||||||
export async function initializeTranslations() {
|
export async function initializeTranslations() {
|
||||||
const resourceDir = getResourceDir();
|
const resourceDir = getResourceDir();
|
||||||
@ -30,12 +37,7 @@ export async function initializeTranslations() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Initialize dayjs locale.
|
// Initialize dayjs locale.
|
||||||
dayjsLocale = DAYJS_LOCALE_MAP[locale] ?? locale;
|
const dayjsLocale = await DAYJS_LOADER[locale]();
|
||||||
try {
|
|
||||||
await import(`dayjs/locale/${dayjsLocale}.js`);
|
|
||||||
} catch (err) {
|
|
||||||
console.warn(`Could not load locale ${dayjsLocale}`, err);
|
|
||||||
}
|
|
||||||
dayjs.locale(dayjsLocale);
|
dayjs.locale(dayjsLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,8 +50,8 @@ export function getLocales(): Locale[] {
|
|||||||
return LOCALES;
|
return LOCALES;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentLanguage() {
|
function getCurrentLanguage(): LOCALE_IDS {
|
||||||
let language;
|
let language: string;
|
||||||
if (sql_init.isDbInitialized()) {
|
if (sql_init.isDbInitialized()) {
|
||||||
language = options.getOptionOrNull("locale");
|
language = options.getOptionOrNull("locale");
|
||||||
}
|
}
|
||||||
@ -59,7 +61,7 @@ function getCurrentLanguage() {
|
|||||||
language = "en";
|
language = "en";
|
||||||
}
|
}
|
||||||
|
|
||||||
return language;
|
return language as LOCALE_IDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function changeLanguage(locale: string) {
|
export async function changeLanguage(locale: string) {
|
||||||
|
@ -9,7 +9,7 @@ export interface Locale {
|
|||||||
electronLocale?: string;
|
electronLocale?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LOCALES: Locale[] = [
|
const UNSORTED_LOCALES = [
|
||||||
{
|
{
|
||||||
id: "en",
|
id: "en",
|
||||||
name: "English",
|
name: "English",
|
||||||
@ -75,4 +75,9 @@ export const LOCALES: Locale[] = [
|
|||||||
rtl: true,
|
rtl: true,
|
||||||
contentOnly: true
|
contentOnly: true
|
||||||
}
|
}
|
||||||
].sort((a, b) => a.name.localeCompare(b.name));
|
] as const;
|
||||||
|
|
||||||
|
export const LOCALES: Locale[] = Array.from(UNSORTED_LOCALES)
|
||||||
|
.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
|
|
||||||
|
export type LOCALE_IDS = typeof UNSORTED_LOCALES[number]["id"];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user