mirror of
https://github.com/zadam/trilium.git
synced 2025-12-03 22:14:24 +01:00
refactor(dayjs): relocate locale loading in commons
This commit is contained in:
parent
5df539f0a4
commit
ebbdf0294a
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -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) {
|
||||
|
||||
13
packages/commons/src/lib/dayjs.spec.ts
Normal file
13
packages/commons/src/lib/dayjs.spec.ts
Normal file
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
@ -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
|
||||
};
|
||||
|
||||
@ -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" },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user