Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): restore the --lang switch from the configured locale

Elian Doran1cffaf8153d5
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 715f5086e9..c70754d86d 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -1,5 +1,6 @@
import { join as pathJoin } from "node:path";
+import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
type Handler = (...args: unknown[]) => unknown;
@@ -142,14 +143,10 @@ vi.mock("@triliumnext/core", async (importOriginal) => {
return Promise.resolve();
}),
options: {
- // NB: smoothScrollEnabled is deliberately NOT served here — it is read before
- // core init straight from the shared provider (see the sql_provider mock), so
- // getOptionOrNull() returns null for it at that point, as in production.
- getOptionOrNull: (key: string) => {
- if (key === "locale") return h.locale;
- if (key === "formattingLocale") return h.formattingLocale;
- return null;
- },
+ // NB: smoothScrollEnabled / locale / formattingLocale are deliberately NOT served
+ // here — they are read before core init straight from the shared provider (see the
+ // sql_provider mock), so getOptionOrNull() returns null for them, as in production.
+ getOptionOrNull: () => null,
getOptionBool: (key: string) => (key === "disableTray" ? h.disableTray : false)
},
sql_init: { isDbInitialized: () => h.isDbInitialized, dbReady: Promise.resolve() },
@@ -177,17 +174,21 @@ vi.mock("@triliumnext/server/src/crypto_provider.js", () => ({ default: class {}
vi.mock("@triliumnext/server/src/in_app_help_provider.js", () => ({ default: class {} }));
vi.mock("@triliumnext/server/src/log_provider.js", () => ({ default: class {} }));
// readDbOption() reads the pre-`ready` switch options from this shared provider via
-// prepare().pluck().get(name). Back it with h.smoothScroll, and let h.dbUninitialized
+// prepare().pluck().get(name). Back it with the h.* option values, and let h.dbUninitialized
// simulate a first run where the schema (and so the options table) does not exist yet.
vi.mock("@triliumnext/server/src/sql_provider.js", () => ({
default: class {
loadFromFile = vi.fn();
prepare() {
if (h.dbUninitialized) throw new Error("no such table: options");
+ const values: Record<string, string | null> = {
+ smoothScrollEnabled: h.smoothScroll,
+ locale: h.locale,
+ formattingLocale: h.formattingLocale
+ };
return {
pluck: () => ({
- get: (name: string) =>
- (name === "smoothScrollEnabled" && h.smoothScroll !== null ? h.smoothScroll : undefined)
+ get: (name: string) => values[name] ?? undefined
})
};
}
@@ -405,6 +406,19 @@ describe("main() bootstrap", () => {
expect(switches).not.toContain("disable-smooth-scrolling");
});
+ // Regression for #10559 (sibling of the smooth-scroll bug): the --lang switch is set
+ // before core init from the same provider, so it must reflect the configured locale
+ // instead of always falling back to "en".
+ it("sets the --lang switch to the configured locale read from the DB", async () => {
+ setPlatform("darwin");
+ h.locale = "de";
+ h.formattingLocale = null;
+ h.locales = [{ id: "de", rtl: false }];
+ const { main } = await importMain();
+ await main();
+ expect(h.appendSwitch).toHaveBeenCalledWith("lang", "de");
+ });
+
it("exits when it is not the primary instance", async () => {
h.isPrimaryInstance = false;
const { main } = await importMain();
@@ -691,12 +705,14 @@ describe("getUserData()", () => {
});
describe("getElectronLocale()", () => {
+ // The locale options are read from the shared provider (core options are not wired up
+ // before `ready`), so each test drives them through a mocked provider instance.
it("returns the formatting locale when its corresponding UI locale is not RTL", async () => {
h.locale = "en";
h.formattingLocale = "de";
h.locales = [{ id: "en", rtl: false }];
const { getElectronLocale } = await importMain();
- expect(getElectronLocale()).toBe("de");
+ expect(getElectronLocale(new BetterSqlite3Provider())).toBe("de");
});
it("falls back to the UI locale when the corresponding locale is RTL", async () => {
@@ -704,7 +720,7 @@ describe("getElectronLocale()", () => {
h.formattingLocale = "de";
h.locales = [{ id: "ar", rtl: true }];
const { getElectronLocale } = await importMain();
- expect(getElectronLocale()).toBe("ar");
+ expect(getElectronLocale(new BetterSqlite3Provider())).toBe("ar");
});
it("returns the UI locale when there is no formatting locale", async () => {
@@ -712,7 +728,7 @@ describe("getElectronLocale()", () => {
h.formattingLocale = null;
h.locales = [{ id: "fr", rtl: false }];
const { getElectronLocale } = await importMain();
- expect(getElectronLocale()).toBe("fr");
+ expect(getElectronLocale(new BetterSqlite3Provider())).toBe("fr");
});
it("defaults to 'en' when neither locale is set", async () => {
@@ -720,6 +736,6 @@ describe("getElectronLocale()", () => {
h.formattingLocale = null;
h.locales = [];
const { getElectronLocale } = await importMain();
- expect(getElectronLocale()).toBe("en");
+ expect(getElectronLocale(new BetterSqlite3Provider())).toBe("en");
});
});
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 64813a7666..d1b9ec8857 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -82,7 +82,6 @@ export async function main() {
// needed for excalidraw export https://github.com/zadam/trilium/issues/4271
app.commandLine.appendSwitch("enable-experimental-web-platform-features");
- app.commandLine.appendSwitch("lang", getElectronLocale());
// In dev mode, disable Chromium's HTTP cache so stale assets cached from a
// previous production run (which served `max-age: 1y` headers) don't shadow
@@ -181,11 +180,12 @@ export async function main() {
dbProvider.loadFromFile(dataDirs.DOCUMENT_PATH, config.General.readOnly);
markStartupMetric("database-opened");
- // Disable smooth scrolling if the option is off. This Chromium switch must be applied
- // before `ready`; the prologue above is await-free, so `ready` cannot have fired yet.
- // The value is read straight from the provider we just opened (and reuse for the whole
- // app below) because the core options service isn't wired up until initializeCore() runs
- // — reading via options.getOptionOrNull() here would always return null (#10559).
+ // These Chromium switches must be applied before `ready`; the prologue above is
+ // await-free, so `ready` cannot have fired yet. Their option values are read straight
+ // from the provider we just opened (and reuse for the whole app below) because the core
+ // options service isn't wired up until initializeCore() runs — reading via
+ // options.getOptionOrNull() here would always return null (#10559).
+ app.commandLine.appendSwitch("lang", getElectronLocale(dbProvider));
if (readDbOption(dbProvider, "smoothScrollEnabled") === "false") {
app.commandLine.appendSwitch("disable-smooth-scrolling");
}
@@ -339,9 +339,9 @@ async function onReady() {
await windowService.registerGlobalShortcuts();
}
-export function getElectronLocale() {
- const uiLocale = options.getOptionOrNull("locale");
- const formattingLocale = options.getOptionOrNull("formattingLocale");
+export function getElectronLocale(provider: BetterSqlite3Provider) {
+ const uiLocale = readDbOption(provider, "locale");
+ const formattingLocale = readDbOption(provider, "formattingLocale");
const correspondingLocale = LOCALES.find(l => l.id === uiLocale);
// For RTL, we have to force the UI locale to align the window buttons properly.