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): restore stored language on server/desktop startup
Elian Doranbdc631c8a3d1
diff --git a/apps/server/src/app.ts b/apps/server/src/app.tsindex b2d96fabe5..47bd71395e 100644--- a/apps/server/src/app.ts+++ b/apps/server/src/app.ts@@ -121,9 +121,19 @@ export default async function buildApp() {custom.register(app);error_handlers.register(app);- const { sync, consistency_checks, scheduler } = await import("@triliumnext/core");+ const { sync, consistency_checks, scheduler, sql_init, becca_loader, i18n } = await import("@triliumnext/core");sync.startSyncTimer();+ // Server-side i18next always boots on "en" (initTranslations runs before initSql in initializeCore),+ // so re-sync it with the document's stored locale before the scheduler's dbReady.then(checkHiddenSubtree)+ // rebuilds the built-in titles — otherwise they are (re-)generated in English on every start. The read+ // goes through becca, so wait for it; guard on isDbInitialized so we don't await beccaLoaded (which never+ // resolves pre-setup) on a fresh install. Desktop reaches this via the same www.js → buildApp path.+ if (sql_init.isDbInitialized()) {+ await becca_loader.beccaLoaded;+ await i18n.reconcileLanguageAfterDbInit();+ }+consistency_checks.startConsistencyChecks();scheduler.startScheduler();diff --git a/packages/trilium-core/src/services/i18n.spec.ts b/packages/trilium-core/src/services/i18n.spec.tsindex eecaed2be2..bd22737431 100644--- a/packages/trilium-core/src/services/i18n.spec.ts+++ b/packages/trilium-core/src/services/i18n.spec.ts@@ -1,9 +1,10 @@import { dayjs, LOCALES } from "@triliumnext/commons";+import i18next from "i18next";import { afterEach, describe, expect, it, vi } from "vitest";import * as cls from "./context.js";import hidden_subtree from "./hidden_subtree.js";-import { changeLanguage, getCurrentLocale, initTranslations, ordinal } from "./i18n.js";+import { changeLanguage, getCurrentLocale, initTranslations, ordinal, reconcileLanguageAfterDbInit } from "./i18n.js";import options from "./options.js";import sql_init from "./sql_init.js";@@ -42,6 +43,57 @@ describe("i18n service (core)", () => {expect(getCurrentLocale().id).toBe("en");});+ describe("reconcileLanguageAfterDbInit", () => {+ it("restores i18next to the stored locale once the DB is initialized", async () => {+ const prevOpt = options.getOption("locale");+ const prevLang = i18next.language;+ try {+ // 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);+ }+ });++ it("does not switch the language when the DB is not initialized", async () => {+ vi.spyOn(sql_init, "isDbInitialized").mockReturnValue(false);+ const changeSpy = vi.spyOn(i18next, "changeLanguage");++ await reconcileLanguageAfterDbInit();++ expect(changeSpy).not.toHaveBeenCalled();+ });++ it("does not switch the language when the stored locale already matches the active one", async () => {+ const prevOpt = options.getOption("locale");+ const prevLang = i18next.language;+ try {+ await i18next.changeLanguage("en");+ cls.init(() => options.setOption("locale", "en"));+ 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 () => {const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});const previous = options.getOption("locale");diff --git a/packages/trilium-core/src/services/i18n.ts b/packages/trilium-core/src/services/i18n.tsindex 1cc7e8f7cd..d943c10c38 100644--- a/packages/trilium-core/src/services/i18n.ts+++ b/packages/trilium-core/src/services/i18n.ts@@ -37,6 +37,33 @@ export async function changeLanguage(locale: string) {hidden_subtree.checkHiddenSubtree(true, { restoreNames: true });}+/**+ * Re-syncs the active i18next (and dayjs) language with the document's stored `locale` option.+ *+ * `initTranslations` runs before `initSql` inside `initializeCore` (options_init needs translations), so+ * on the server/desktop boot path i18next is always initialized to the fallback "en" regardless of the+ * stored locale — it cannot read the option before the DB is open. Call this once the DB is initialized+ * and becca is loaded to bring i18next in line with the stored locale, so server-generated content (e.g.+ * the hidden-subtree titles rebuilt by the scheduler) is produced in the right language.+ *+ * This is language-only: it does not rebuild the hidden subtree, so user-renamed system notes are left+ * untouched (unlike {@link changeLanguage}). It is a no-op when the DB is uninitialized or the stored+ * locale already matches the active language.+ */+export async function reconcileLanguageAfterDbInit() {+ if (!sql_init.isDbInitialized()) {+ return;+ }++ const locale = options.getOptionOrNull("locale");+ if (!locale || locale === i18next.language) {+ return;+ }++ await i18next.changeLanguage(locale);+ await setDayjsLocale(locale as LOCALE_IDS);+}+export function getCurrentLocale() {if (!sql_init.isDbInitialized()) {// If DB is not initialized, we cannot get the locale from options, so we return English as a default.