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
feat(client): localize the text editor toolbar group labels
Elian Doran65c9c0577e57
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d673ca7a0c..320ae8dcbc 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3691,6 +3691,11 @@"task-state-dropped-message_other": "{{count}} custom task state definitions are invalid and were ignored.","task-state-dropped-heading": "Ignored definitions:","task-state-dropped-configure": "Configure task states",+ "toolbar-groups": {+ "text-formatting": "Text formatting",+ "insert": "Insert",+ "alignment": "Alignment"+ },"validation-errors": {"undefined-name": "undefined identifier","invalid-name": "invalid identifier",diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts b/apps/client/src/widgets/type_widgets/text/toolbar.spec.tsindex 5eb1418128..35fa0996c7 100644--- a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts@@ -11,6 +11,10 @@ vi.mock("../../../services/options.js", () => ({default: { get: (name: string) => optionsState.values[name] }}));+// The group labels are translated when the toolbar is built (CKEditor renders a nested dropdown's+// `label` verbatim), so echo the key back to make the lookup visible to the assertions below.+vi.mock("../../../services/i18n.js", () => ({ t: (key: string) => key }));+function setDevice(device: "mobile" | "desktop") {(window as unknown as { glob?: { device?: string } }).glob = { device };}@@ -56,6 +60,31 @@ describe("buildClassicToolbar", () => {});});+describe("group labels", () => {+ // Each nested group carries a translated label; a hardcoded English one would reach the user+ // untranslated, since CKEditor passes it straight to the dropdown button.+ function groupLabels(items: unknown[]): string[] {+ return items+ .filter((item): item is { label: string } => typeof item === "object" && item !== null && "label" in item)+ .map((item) => item.label);+ }++ it("translates every group label, on both the classic and the floating toolbar", () => {+ expect(groupLabels(buildClassicToolbar(false).toolbar.items)).toStrictEqual([+ "text-editor.toolbar-groups.text-formatting",+ "text-editor.toolbar-groups.insert",+ "text-editor.toolbar-groups.alignment"+ ]);++ const floating = buildFloatingToolbar();+ expect(groupLabels(floating.toolbar.items)).toStrictEqual([ "text-editor.toolbar-groups.text-formatting" ]);+ expect(groupLabels(floating.blockToolbar)).toStrictEqual([+ "text-editor.toolbar-groups.insert",+ "text-editor.toolbar-groups.alignment"+ ]);+ });+});+describe("buildMobileToolbar", () => {it("flattens nested toolbar groups into a single flat item list", () => {const mobile = buildMobileToolbar();diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.ts b/apps/client/src/widgets/type_widgets/text/toolbar.tsindex 54c0245acb..5212d24327 100644--- a/apps/client/src/widgets/type_widgets/text/toolbar.ts+++ b/apps/client/src/widgets/type_widgets/text/toolbar.ts@@ -1,12 +1,8 @@import utils from "../../../services/utils.js";import options from "../../../services/options.js";+import { t } from "../../../services/i18n.js";import { IconAlignCenter } from "@ckeditor/ckeditor5-icons";-const TEXT_FORMATTING_GROUP = {- label: "Text formatting",- icon: "text"-};-export function buildToolbarConfig(isClassicToolbar: boolean) {if (utils.isMobile()) {return buildMobileToolbar();@@ -52,7 +48,7 @@ export function buildClassicToolbar(multilineToolbar: boolean) {"bold","italic",{- ...TEXT_FORMATTING_GROUP,+ ...buildTextFormattingGroup(),items: ["underline", "strikethrough", "|", "superscript", "subscript", "|", "kbd"]},"formatPainter",@@ -75,8 +71,7 @@ export function buildClassicToolbar(multilineToolbar: boolean) {"|","footnote",{- label: "Insert",- icon: "plus",+ ...buildInsertGroup(),items: ["link", "linkEmbed", "bookmark", "internallink", "includeNote", "|", "collapsible", "math", "mermaid", "horizontalLine", "pageBreak", "|", "dateTime", "specialCharacters", "emoji"]},"|",@@ -105,7 +100,7 @@ export function buildFloatingToolbar() {"italic","underline",{- ...TEXT_FORMATTING_GROUP,+ ...buildTextFormattingGroup(),items: [ "strikethrough", "|", "superscript", "subscript", "|", "kbd" ]},"formatPainter",@@ -138,8 +133,7 @@ export function buildFloatingToolbar() {"insertTable","footnote",{- label: "Insert",- icon: "plus",+ ...buildInsertGroup(),items: ["link", "linkEmbed", "bookmark", "internallink", "includeNote", "|", "collapsible", "math", "mermaid", "horizontalLine", "pageBreak", "dateTime"]},"|",@@ -155,9 +149,27 @@ export function buildFloatingToolbar() {};}+// The labels below are resolved here rather than by the editor: CKEditor takes a nested toolbar+// dropdown's `label` verbatim (see `ToolbarView#_createNestedToolbarDropdown`), so it never reaches+// a translation function of its own. They render as the dropdown's tooltip and accessible name.++function buildTextFormattingGroup() {+ return {+ label: t("text-editor.toolbar-groups.text-formatting"),+ icon: "text"+ };+}++function buildInsertGroup() {+ return {+ label: t("text-editor.toolbar-groups.insert"),+ icon: "plus"+ };+}+function buildAlignmentToolbar() {return {- label: "Alignment",+ label: t("text-editor.toolbar-groups.alignment"),icon: IconAlignCenter,items: ["alignment:left", "alignment:center", "alignment:right", "|", "alignment:justify"]};