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
test(setup): cover setup-locale language switch; extract applySetupLanguage
Elian Doran1a9dfedac902
diff --git a/packages/trilium-core/src/services/sql_init.spec.ts b/packages/trilium-core/src/services/sql_init.spec.tsindex f41ed5d681..41b4b7edda 100644--- a/packages/trilium-core/src/services/sql_init.spec.ts+++ b/packages/trilium-core/src/services/sql_init.spec.ts@@ -1,9 +1,10 @@-import { beforeAll, describe, expect, it } from "vitest";+import i18next from "i18next";+import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";import { getContext } from "./context.js";import eventService from "./events.js";import { getSql } from "./sql/index.js";-import sqlInit from "./sql_init.js";+import sqlInit, { applySetupLanguage } from "./sql_init.js";describe("sql_init (real DB)", () => {beforeAll(() => {@@ -72,6 +73,31 @@ describe("sql_init (real DB)", () => {});});+ describe("applySetupLanguage", () => {+ afterEach(() => {+ vi.restoreAllMocks();+ });++ it("switches i18next to the locale chosen during setup", async () => {+ const changeLanguage = vi.spyOn(i18next, "changeLanguage").mockResolvedValue((() => "") as never);++ await applySetupLanguage("de");++ // Persisting the `locale` option is not enough — the running i18next instance must be switched+ // before the hidden subtree is built, otherwise the built-in titles are generated in English.+ expect(changeLanguage).toHaveBeenCalledWith("de");+ });++ it("leaves the language untouched for an undefined or non-displayable locale", async () => {+ const changeLanguage = vi.spyOn(i18next, "changeLanguage").mockResolvedValue((() => "") as never);++ await applySetupLanguage(undefined);+ await applySetupLanguage("zz-not-a-locale");++ expect(changeLanguage).not.toHaveBeenCalled();+ });+ });+describe("initDbConnection", () => {it("creates the param_list and user_data tables and resolves dbReady", async () => {await getContext().init(() => sqlInit.initDbConnection());diff --git a/packages/trilium-core/src/services/sql_init.ts b/packages/trilium-core/src/services/sql_init.tsindex 35e1620caf..c0fd7f3526 100644--- a/packages/trilium-core/src/services/sql_init.ts+++ b/packages/trilium-core/src/services/sql_init.ts@@ -173,16 +173,10 @@ async function createInitialDatabase(skipDemoDb?: boolean, locale?: string) {});// Persisting the `locale` option above only records the choice in the DB; it does not switch the- // active i18next language. Because `initTranslations` runs before `initSql` inside `initializeCore`- // (options_init needs translations), i18next is still on the boot default "en" at this point. Switch- // it to the chosen locale now, before `checkHiddenSubtree` builds the built-in titles with t() —+ // active i18next language. Switch it now, before `checkHiddenSubtree` builds the built-in titles,// otherwise every system note (Options, Launch Bar, templates, Help) is created in English regardless- // of the language selected during setup. No forced name restore is needed: the notes are being created- // here for the first time, so there is nothing to clobber.- if (isDisplayableLocale(locale)) {- await i18next.changeLanguage(locale);- await setDayjsLocale(locale);- }+ // of the language selected during setup.+ await applySetupLanguage(locale);// Check hidden subtree.// This ensures the existence of system templates, for the demo content.@@ -237,6 +231,24 @@ async function createInitialDatabase(skipDemoDb?: boolean, locale?: string) {log.info("Database initialization completed, emitted DB_INITIALIZED event");}+/**+ * Applies the display language chosen during initial setup to the running i18next (and dayjs) instance.+ *+ * `createInitialDatabase` persists the choice as the `locale` option, but that is only a DB write: because+ * `initTranslations` runs before `initSql` inside `initializeCore` (options_init needs translations),+ * i18next is still on the boot default "en" at setup time. Switching here, before the hidden subtree is+ * built, ensures the built-in note titles are generated in the selected language. Undefined or+ * non-displayable locales are ignored so the default is kept.+ */+export async function applySetupLanguage(locale: string | undefined) {+ if (!isDisplayableLocale(locale)) {+ return;+ }++ await i18next.changeLanguage(locale);+ await setDayjsLocale(locale);+}+async function createDatabaseForSync(options: OptionRow[], syncServerHost = "", syncProxy = "", syncMaxBlobContentSize = 0) {const log = getLog();const sql = getSql();