Skip to content

Build your personal knowledge base with Trilium Notes

refactor(i18n): fold the content-language helpers in with the locale ones

Elian Doranf253854e5d01
diff --git a/apps/client/src/services/content_language.spec.ts b/apps/client/src/services/content_language.spec.ts
deleted file mode 100644
index 7e22d5cbc9..0000000000
--- a/apps/client/src/services/content_language.spec.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { beforeEach, describe, expect, it, vi } from "vitest";
-
-import { isContentRightToLeft, resolveContentLanguage } from "./content_language.js";
-
-const optionsState = vi.hoisted(() => ({ map: {} as Record<string, string | undefined> }));
-
-vi.mock("./options.js", () => ({
- default: {
- get: (name: string) => optionsState.map[name]
- }
-}));
-
-beforeEach(() => {
- optionsState.map = {};
-});
-
-describe("resolveContentLanguage", () => {
- it("prefers the note's own language over the configured default", () => {
- optionsState.map.defaultContentLanguage = "fr";
- optionsState.map.locale = "ru";
-
- expect(resolveContentLanguage("de")).toBe("de");
- });
-
- it("falls back to the default content language, then to the application's language", () => {
- optionsState.map.defaultContentLanguage = "fr";
- optionsState.map.locale = "ru";
-
- expect(resolveContentLanguage(null)).toBe("fr");
- expect(resolveContentLanguage(undefined)).toBe("fr");
- expect(resolveContentLanguage("")).toBe("fr");
-
- // An empty default is the "auto" entry, meaning follow the application's language rather
- // than meaning no language at all.
- optionsState.map.defaultContentLanguage = "";
- expect(resolveContentLanguage(null)).toBe("ru");
- });
-
- it("returns null only when nothing at all is configured", () => {
- expect(resolveContentLanguage(null)).toBeNull();
- });
-});
-
-describe("isContentRightToLeft", () => {
- it("follows the note's own language", () => {
- expect(isContentRightToLeft("he")).toBe(true);
- expect(isContentRightToLeft("de")).toBe(false);
- });
-
- it("applies the default to a note that has no language of its own", () => {
- optionsState.map.defaultContentLanguage = "ar";
- expect(isContentRightToLeft(null)).toBe(true);
-
- // ...and a note that does have one is not dragged along by it.
- expect(isContentRightToLeft("de")).toBe(false);
- });
-
- it("treats an unknown or missing language as left-to-right", () => {
- expect(isContentRightToLeft(null)).toBe(false);
- expect(isContentRightToLeft("not-a-locale")).toBe(false);
- });
-});
diff --git a/apps/client/src/services/content_language.ts b/apps/client/src/services/content_language.ts
deleted file mode 100644
index 9277bcf84c..0000000000
--- a/apps/client/src/services/content_language.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import { getLocaleById } from "./i18n.js";
-import options from "./options.js";
-
-/**
- * The language a note is written in.
- *
- * A note carries its own language as a `#language` label, set from the Basic Properties ribbon, but
- * the label is opt-in and the picker that sets it stays empty until content languages are enabled —
- * so in practice almost no note has one. The `defaultContentLanguage` option answers for all of
- * them, and an empty value there means "follow the application's language" rather than "none".
- *
- * Callers should route the raw label through this rather than reading it directly, so that what the
- * setting promises to affect — text direction, typographic quotes — actually follows it everywhere.
- */
-export function resolveContentLanguage(noteLanguage: string | null | undefined): string | null {
- return noteLanguage || options.get("defaultContentLanguage") || options.get("locale") || null;
-}
-
-/** Whether a note's resolved language is written right-to-left. */
-export function isContentRightToLeft(noteLanguage: string | null | undefined): boolean {
- return getLocaleById(resolveContentLanguage(noteLanguage))?.rtl ?? false;
-}
diff --git a/apps/client/src/utils/formatters.spec.ts b/apps/client/src/utils/formatters.spec.ts
index 4f9501cec4..77b7ee0f5f 100644
--- a/apps/client/src/utils/formatters.spec.ts
+++ b/apps/client/src/utils/formatters.spec.ts
@@ -3,14 +3,20 @@ import { describe, expect, it, vi } from "vitest";
// i18next is not initialised here, so have t() echo the key and the interpolated count. That lets the
// tests assert exactly which unit was chosen and what count was computed — the real logic — while
// leaving the plural rendering itself to i18next.
-vi.mock("../services/i18n", () => ({
- t: (key: string, opts?: { count?: number }) => `${key}|${opts?.count}`
-}));
+// `getLocaleById` is the real lookup rather than a stub — it is a find over the shared catalog, so
+// mocking it would only risk disagreeing with the locales the assertions name.
+vi.mock("../services/i18n", async () => {
+ const { LOCALES } = await import("@triliumnext/commons");
+ return {
+ t: (key: string, opts?: { count?: number }) => `${key}|${opts?.count}`,
+ getLocaleById: (id: string | null | undefined) => LOCALES.find((l) => l.id === id) ?? null
+ };
+});
import { LOCALES } from "@triliumnext/commons";
import options from "../services/options";
-import { formatDateNumeric, formatDateTime, formatDuration, getMeasurementSystem, normalizeLocale } from "./formatters";
+import { formatDateNumeric, formatDateTime, formatDuration, getMeasurementSystem, isContentRightToLeft, normalizeLocale, resolveContentLanguage } from "./formatters";
describe("formatters", () => {
it("tolerates incorrect locale", () => {
@@ -256,3 +262,47 @@ describe("formatters", () => {
});
});
});
+
+describe("resolveContentLanguage", () => {
+ it("prefers the note's own language over the configured default", () => {
+ options.set("defaultContentLanguage", "fr");
+ options.set("locale", "ru");
+
+ expect(resolveContentLanguage("de")).toBe("de");
+ });
+
+ it("falls back to the default content language, then to the application's language", () => {
+ options.set("defaultContentLanguage", "fr");
+ options.set("locale", "ru");
+
+ expect(resolveContentLanguage(null)).toBe("fr");
+ expect(resolveContentLanguage(undefined)).toBe("fr");
+ expect(resolveContentLanguage("")).toBe("fr");
+
+ // An empty default is the "auto" entry, meaning follow the application's language rather
+ // than meaning no language at all.
+ options.set("defaultContentLanguage", "");
+ expect(resolveContentLanguage(null)).toBe("ru");
+ });
+});
+
+describe("isContentRightToLeft", () => {
+ it("follows the note's own language", () => {
+ expect(isContentRightToLeft("he")).toBe(true);
+ expect(isContentRightToLeft("de")).toBe(false);
+ });
+
+ it("applies the default to a note that has no language of its own", () => {
+ options.set("defaultContentLanguage", "ar");
+ expect(isContentRightToLeft(null)).toBe(true);
+
+ // ...and a note that does have one is not dragged along by it.
+ expect(isContentRightToLeft("de")).toBe(false);
+ });
+
+ it("treats an unrecognized language as left-to-right", () => {
+ options.set("defaultContentLanguage", "not-a-locale");
+ expect(isContentRightToLeft(null)).toBe(false);
+ expect(isContentRightToLeft("also-not-a-locale")).toBe(false);
+ });
+});
diff --git a/apps/client/src/utils/formatters.ts b/apps/client/src/utils/formatters.ts
index ad70d1a899..d4d7d2c7a1 100644
--- a/apps/client/src/utils/formatters.ts
+++ b/apps/client/src/utils/formatters.ts
@@ -1,4 +1,4 @@
-import { t } from "../services/i18n";
+import { getLocaleById, t } from "../services/i18n";
import options from "../services/options";
type DateTimeStyle = "full" | "long" | "medium" | "short" | "none" | undefined;
@@ -172,6 +172,26 @@ export function normalizeLocale(locale: string) {
}
}
+/**
+ * The language a note is written in.
+ *
+ * A note carries its own language as a `#language` label, set from the Basic Properties ribbon, but
+ * the label is opt-in and the picker that sets it stays empty until content languages are enabled —
+ * so in practice almost no note has one. The `defaultContentLanguage` option answers for all of
+ * them, and an empty value there means "follow the application's language" rather than "none".
+ *
+ * Callers should route the raw label through this rather than reading it directly, so that what the
+ * setting promises to affect — text direction, typographic quotes — actually follows it everywhere.
+ */
+export function resolveContentLanguage(noteLanguage: string | null | undefined): string | null {
+ return noteLanguage || options.get("defaultContentLanguage") || options.get("locale") || null;
+}
+
+/** Whether a note's resolved language is written right-to-left. */
+export function isContentRightToLeft(noteLanguage: string | null | undefined): boolean {
+ return getLocaleById(resolveContentLanguage(noteLanguage))?.rtl ?? false;
+}
+
/**
* Whether distances should be shown in miles or in kilometres, for consumers such as the geo map's
* scale bar.
diff --git a/apps/client/src/widgets/note_wrapper.ts b/apps/client/src/widgets/note_wrapper.ts
index 1ae9679985..918404801d 100644
--- a/apps/client/src/widgets/note_wrapper.ts
+++ b/apps/client/src/widgets/note_wrapper.ts
@@ -2,8 +2,8 @@ import type { EventData } from "../components/app_context.js";
import type NoteContext from "../components/note_context.js";
import type FNote from "../entities/fnote.js";
import attributeService from "../services/attributes.js";
-import { isContentRightToLeft } from "../services/content_language.js";
import utils from "../services/utils.js";
+import { isContentRightToLeft } from "../utils/formatters.js";
import type BasicWidget from "./basic_widget.js";
import FlexContainer from "./containers/flex_container.js";
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 73292a0452..5d5e07b758 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -10,12 +10,12 @@ import { useEffect, useLayoutEffect, useMemo, useRef as usePreactRef } from "pre
import appContext from "../../../components/app_context";
import FNote from "../../../entities/fnote";
-import { isContentRightToLeft } from "../../../services/content_language";
import { applyInlineMermaid, rewriteMermaidDiagramsInContainer } from "../../../services/content_renderer_text";
import { applyLinkEmbeds } from "../../../services/link_embed";
import { renderMathInElement } from "../../../services/math";
import { trackPendingRender } from "../../../services/pending_renders";
import { formatCodeBlocks } from "../../../services/syntax_highlight";
+import { isContentRightToLeft } from "../../../utils/formatters";
import { useNoteBlob, useNoteLabel, useSyncedRef, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";
import { RawHtmlBlock } from "../../react/RawHtml";
import { TypeWidgetProps } from "../type_widget";
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 465cf76bac..b067b4e842 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -4,7 +4,6 @@ import { ALLOWED_PROTOCOLS, DISPLAYABLE_LOCALE_IDS, formatShortcut, IMAGE_UPLOAD
import i18next from "i18next";
import { copyTextWithToast } from "../../../services/clipboard_ext.js";
-import { resolveContentLanguage } from "../../../services/content_language.js";
import { t } from "../../../services/i18n.js";
import imageService from "../../../services/image.js";
import { getMermaidConfig } from "../../../services/mermaid.js";
@@ -14,6 +13,7 @@ import options from "../../../services/options.js";
import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";
import { getTaskStateDefinitions, openCustomTaskStateConfig } from "../../../services/task_states.js";
import { isMac } from "../../../services/utils.js";
+import { resolveContentLanguage } from "../../../utils/formatters.js";
import SAMPLE_DIAGRAMS from "../mermaid/sample_diagrams.js";
import { buildQuoteTransformations, resolveQuoteStyle } from "./quotes.js";
import { buildToolbarConfig } from "./toolbar.js";