Skip to content

Build your personal knowledge base with Trilium Notes

refactor(dayjs): relocate locale loading in commons

Elian Doranebbdf0294afe
diff --git a/apps/server/src/services/i18n.spec.ts b/apps/server/src/services/i18n.spec.ts
index 00dfd113b2..c5dc00cadb 100644
--- a/apps/server/src/services/i18n.spec.ts
+++ b/apps/server/src/services/i18n.spec.ts
@@ -1,7 +1,6 @@
import { LOCALES } from "@triliumnext/commons";
import { readFileSync } from "fs";
import { join } from "path";
-import { DAYJS_LOADER } from "./i18n";
describe("i18n", () => {
it("translations are valid JSON", () => {
@@ -16,13 +15,4 @@ describe("i18n", () => {
.not.toThrow();
}
});
-
- it("all dayjs locales are valid", async () => {
- for (const locale of LOCALES) {
- const dayjsLoader = DAYJS_LOADER[locale.id];
- expect(dayjsLoader, `Locale ${locale.id} missing.`).toBeDefined();
-
- await dayjsLoader();
- }
- });
});
diff --git a/apps/server/src/services/i18n.ts b/apps/server/src/services/i18n.ts
index 82eb6f4088..5da578c88b 100644
--- a/apps/server/src/services/i18n.ts
+++ b/apps/server/src/services/i18n.ts
@@ -4,32 +4,9 @@ import sql_init from "./sql_init.js";
import { join } from "path";
import { getResourceDir } from "./utils.js";
import hidden_subtree from "./hidden_subtree.js";
-import { LOCALES, type Locale, type LOCALE_IDS } from "@triliumnext/commons";
+import { LOCALES, setDayjsLocale, type Locale, type LOCALE_IDS } from "@triliumnext/commons";
import dayjs, { Dayjs } from "dayjs";
-// When adding a new locale, prefer the version with hyphen instead of underscore.
-export const DAYJS_LOADER: Record<LOCALE_IDS, () => Promise<typeof import("dayjs/locale/en.js")>> = {
- "ar": () => import("dayjs/locale/ar.js"),
- "cn": () => import("dayjs/locale/zh-cn.js"),
- "de": () => import("dayjs/locale/de.js"),
- "en": () => import("dayjs/locale/en.js"),
- "en-GB": () => import("dayjs/locale/en-gb.js"),
- "en_rtl": () => import("dayjs/locale/en.js"),
- "es": () => import("dayjs/locale/es.js"),
- "fa": () => import("dayjs/locale/fa.js"),
- "fr": () => import("dayjs/locale/fr.js"),
- "it": () => import("dayjs/locale/it.js"),
- "he": () => import("dayjs/locale/he.js"),
- "ja": () => import("dayjs/locale/ja.js"),
- "ku": () => import("dayjs/locale/ku.js"),
- "pt_br": () => import("dayjs/locale/pt-br.js"),
- "pt": () => import("dayjs/locale/pt.js"),
- "ro": () => import("dayjs/locale/ro.js"),
- "ru": () => import("dayjs/locale/ru.js"),
- "tw": () => import("dayjs/locale/zh-tw.js"),
- "uk": () => import("dayjs/locale/uk.js"),
-}
-
export async function initializeTranslations() {
const resourceDir = getResourceDir();
const Backend = (await import("i18next-fs-backend/cjs")).default;
@@ -46,10 +23,7 @@ export async function initializeTranslations() {
});
// Initialize dayjs locale.
- const dayjsLocale = DAYJS_LOADER[locale];
- if (dayjsLocale) {
- dayjs.locale(await dayjsLocale());
- }
+ await setDayjsLocale(locale);
}
export function ordinal(date: Dayjs) {
diff --git a/packages/commons/src/lib/dayjs.spec.ts b/packages/commons/src/lib/dayjs.spec.ts
new file mode 100644
index 0000000000..43e3581a1e
--- /dev/null
+++ b/packages/commons/src/lib/dayjs.spec.ts
@@ -0,0 +1,13 @@
+import { LOCALES } from "./i18n.js";
+import { DAYJS_LOADER } from "./dayjs.js";
+
+describe("dayjs", () => {
+ it("all dayjs locales are valid", async () => {
+ for (const locale of LOCALES) {
+ const dayjsLoader = DAYJS_LOADER[locale.id];
+ expect(dayjsLoader, `Locale ${locale.id} missing.`).toBeDefined();
+
+ await dayjsLoader();
+ }
+ });
+});
diff --git a/packages/commons/src/lib/dayjs.ts b/packages/commons/src/lib/dayjs.ts
index a75e0d1ea1..3b59e120a6 100644
--- a/packages/commons/src/lib/dayjs.ts
+++ b/packages/commons/src/lib/dayjs.ts
@@ -8,6 +8,7 @@ import isSameOrAfter from "dayjs/plugin/isSameOrAfter.js";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore.js";
import quarterOfYear from "dayjs/plugin/quarterOfYear.js";
import utc from "dayjs/plugin/utc.js";
+import { LOCALE_IDS } from "./i18n.js";
dayjs.extend(advancedFormat);
dayjs.extend(isBetween);
@@ -18,7 +19,39 @@ dayjs.extend(quarterOfYear);
dayjs.extend(utc);
//#endregion
+//#region Locales
+export const DAYJS_LOADER: Record<LOCALE_IDS, () => Promise<typeof import("dayjs/locale/en.js")>> = {
+ "ar": () => import("dayjs/locale/ar.js"),
+ "cn": () => import("dayjs/locale/zh-cn.js"),
+ "de": () => import("dayjs/locale/de.js"),
+ "en": () => import("dayjs/locale/en.js"),
+ "en-GB": () => import("dayjs/locale/en-gb.js"),
+ "en_rtl": () => import("dayjs/locale/en.js"),
+ "es": () => import("dayjs/locale/es.js"),
+ "fa": () => import("dayjs/locale/fa.js"),
+ "fr": () => import("dayjs/locale/fr.js"),
+ "it": () => import("dayjs/locale/it.js"),
+ "he": () => import("dayjs/locale/he.js"),
+ "ja": () => import("dayjs/locale/ja.js"),
+ "ku": () => import("dayjs/locale/ku.js"),
+ "pt_br": () => import("dayjs/locale/pt-br.js"),
+ "pt": () => import("dayjs/locale/pt.js"),
+ "ro": () => import("dayjs/locale/ro.js"),
+ "ru": () => import("dayjs/locale/ru.js"),
+ "tw": () => import("dayjs/locale/zh-tw.js"),
+ "uk": () => import("dayjs/locale/uk.js"),
+}
+
+async function setDayjsLocale(locale: LOCALE_IDS) {
+ const dayjsLocale = DAYJS_LOADER[locale];
+ if (dayjsLocale) {
+ dayjs.locale(await dayjsLocale());
+ }
+}
+//#endregion
+
export {
dayjs,
- Dayjs
+ Dayjs,
+ setDayjsLocale
};
diff --git a/packages/commons/src/lib/i18n.ts b/packages/commons/src/lib/i18n.ts
index eed5aec1bf..9003ae786e 100644
--- a/packages/commons/src/lib/i18n.ts
+++ b/packages/commons/src/lib/i18n.ts
@@ -11,6 +11,7 @@ export interface Locale {
electronLocale?: "en" | "de" | "es" | "fr" | "zh_CN" | "zh_TW" | "ro" | "af" | "am" | "ar" | "bg" | "bn" | "ca" | "cs" | "da" | "el" | "en_GB" | "es_419" | "et" | "fa" | "fi" | "fil" | "gu" | "he" | "hi" | "hr" | "hu" | "id" | "it" | "ja" | "kn" | "ko" | "lt" | "lv" | "ml" | "mr" | "ms" | "nb" | "nl" | "pl" | "pt_BR" | "pt_PT" | "ru" | "sk" | "sl" | "sr" | "sv" | "sw" | "ta" | "te" | "th" | "tr" | "uk" | "ur" | "vi";
}
+// When adding a new locale, prefer the version with hyphen instead of underscore.
const UNSORTED_LOCALES = [
{ id: "cn", name: "简体中文", electronLocale: "zh_CN" },
{ id: "de", name: "Deutsch", electronLocale: "de" },