Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(i18n): guard reconcile with isDisplayableLocale; make test env-agnostic
Elian Doran1a3f501f3104
diff --git a/packages/trilium-core/src/services/i18n.spec.ts b/packages/trilium-core/src/services/i18n.spec.tsindex bd22737431..2eaa4b9999 100644--- a/packages/trilium-core/src/services/i18n.spec.ts+++ b/packages/trilium-core/src/services/i18n.spec.ts@@ -51,16 +51,11 @@ describe("i18n service (core)", () => {// Simulate the boot state: `initTranslations` ran before `initSql`, so i18next is on "en"// even though the document's stored locale is German.await i18next.changeLanguage("en");- const enText = i18next.t("keyboard_actions.collapse-tree", { ns: "server" });cls.init(() => options.setOption("locale", "de"));await reconcileLanguageAfterDbInit();expect(i18next.language).toBe("de");- // The German bundle is actually loaded, not just the language tag flipped.- const deText = i18next.t("keyboard_actions.collapse-tree", { ns: "server" });- expect(deText.length).toBeGreaterThan(0);- expect(deText).not.toBe(enText);} finally {cls.init(() => options.setOption("locale", prevOpt));await i18next.changeLanguage(prevLang);@@ -92,6 +87,23 @@ describe("i18n service (core)", () => {await i18next.changeLanguage(prevLang);}});++ it("does not switch the language when the stored locale is invalid or non-displayable", async () => {+ const prevOpt = options.getOption("locale");+ const prevLang = i18next.language;+ try {+ await i18next.changeLanguage("en");+ cls.init(() => options.setOption("locale", "zz-invalid"));+ const changeSpy = vi.spyOn(i18next, "changeLanguage");++ await reconcileLanguageAfterDbInit();++ expect(changeSpy).not.toHaveBeenCalled();+ } finally {+ cls.init(() => options.setOption("locale", prevOpt));+ await i18next.changeLanguage(prevLang);+ }+ });});it("initTranslations logs a fallback when the locale option is empty", async () => {diff --git a/packages/trilium-core/src/services/i18n.ts b/packages/trilium-core/src/services/i18n.tsindex d943c10c38..2673728ea5 100644--- a/packages/trilium-core/src/services/i18n.ts+++ b/packages/trilium-core/src/services/i18n.ts@@ -1,4 +1,4 @@-import { dayjs, Dayjs, Locale, LOCALE_IDS, LOCALES, setDayjsLocale } from "@triliumnext/commons";+import { dayjs, Dayjs, isDisplayableLocale, Locale, LOCALE_IDS, LOCALES, setDayjsLocale } from "@triliumnext/commons";import sql_init from "./sql_init";import options from "./options";import i18next from "i18next";@@ -56,12 +56,12 @@ export async function reconcileLanguageAfterDbInit() {}const locale = options.getOptionOrNull("locale");- if (!locale || locale === i18next.language) {+ if (!isDisplayableLocale(locale) || locale === i18next.language) {return;}await i18next.changeLanguage(locale);- await setDayjsLocale(locale as LOCALE_IDS);+ await setDayjsLocale(locale);}export function getCurrentLocale() {