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(ckeditor5): localize the include note box sizes
Elian Doranf80e3a0fa1b2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 92d305ad60..87e53d3c95 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3573,6 +3573,7 @@"align-text-to-the-left": "Align text to the left","align-text-to-the-right": "Align text to the right","anchor": "Anchor",+ "box-size": "Box size","bulleted-list": "Bulleted list","card": "Card","caution": "Caution",@@ -3599,11 +3600,14 @@"embed": "Embed","equation-preview": "Equation preview","error-rendering-equation": "Error rendering equation",+ "expandable": "Expandable","footnote": "Footnote","footnote-widget": "footnote widget",+ "full": "Full","import-a-markdown-file-into-this-note": "Import a markdown file into this note","important": "Important","include-note": "Include note",+ "include-note-widget": "include note widget","increase-the-indentation-of-the-current-block": "Increase the indentation of the current block.","indent": "Indent","inline": "Inline",@@ -3638,6 +3642,7 @@"markdown-import-from-clipboard": "Markdown import from clipboard","math-editor-unavailable": "Math editor unavailable","math-equation": "Math equation",+ "medium": "Medium","mermaid-diagram": "Mermaid diagram","mermaid-diagram-0": "Mermaid diagram: %0","mermaid-toolbar": "Mermaid toolbar",diff --git a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.tsindex 62eefa40df..3511e94d4e 100644--- a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts+++ b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts@@ -71,9 +71,8 @@ describe("IncludeNoteBoxSizeDropdown", () => {const command = editor.commands.get(BOX_SIZE_COMMAND_NAME) as { value: string | null };expect(command.value).toBe("small");- // The button label should reflect the BOX_SIZE label for "small"- const expectedLabel = BOX_SIZES.find((s) => s.value === "small")?.label ?? "small";- expect(dropdownView.buttonView.label).toBe(expectedLabel);+ // No dictionary is configured, so the label renders its English message id.+ expect(dropdownView.buttonView.label).toBe("Small");});it("button label falls back to raw value for an unknown size", () => {@@ -251,8 +250,7 @@ describe("IncludeNoteBoxSizeDropdown", () => {expect(command.value).toBe("medium");// The "medium" index in BOX_SIZES- const mediumIndex = BOX_SIZES.findIndex((s) => s.value === "medium");- expect(mediumIndex).toBeGreaterThanOrEqual(0);+ expect(BOX_SIZES.indexOf("medium")).toBeGreaterThanOrEqual(0);});it("_getBoxSizeListItemDefinitions returns one item per BOX_SIZES entry", () => {diff --git a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.ts b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.tsindex c89ee52c11..8d68a99b98 100644--- a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.ts+++ b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.ts@@ -1,5 +1,5 @@import { Plugin, type ListDropdownButtonDefinition, Collection, ViewModel, createDropdown, addListToDropdown, DropdownButtonView, type Command } from "ckeditor5";-import IncludeNote, { BOX_SIZE_COMMAND_NAME, BOX_SIZES, type BoxSizeValue } from "./includenote.js";+import IncludeNote, { BOX_SIZE_COMMAND_NAME, BOX_SIZES, type BoxSizeValue, getBoxSizeLabel } from "./includenote.js";/*** Toolbar item which displays the list of box sizes for include notes in a dropdown.@@ -19,16 +19,16 @@ export default class IncludeNoteBoxSizeDropdown extends Plugin {componentFactory.add("includeNoteBoxSizeDropdown", _locale => {const dropdownView = createDropdown(editor.locale, DropdownButtonView);+ const boxSizeLabel = editor.t("Box size");dropdownView.buttonView.set({withText: true,tooltip: true,- label: "Box size"+ label: boxSizeLabel});dropdownView.bind("isEnabled").to(command, "isEnabled");dropdownView.buttonView.bind("label").to(command, "value", (value) => {- if (!value) return "Box size";- const sizeDef = BOX_SIZES.find(s => s.value === value);- return sizeDef?.label ?? value;+ if (!value) return boxSizeLabel;+ return getBoxSizeLabel(editor.t, value);});dropdownView.on("execute", evt => {const source = evt.source as any;@@ -47,19 +47,19 @@ export default class IncludeNoteBoxSizeDropdown extends Plugin {const command = editor.commands.get(BOX_SIZE_COMMAND_NAME) as Command & { value: BoxSizeValue | null };const itemDefinitions = new Collection<ListDropdownButtonDefinition>();- for (const sizeDef of BOX_SIZES) {+ for (const size of BOX_SIZES) {const definition: ListDropdownButtonDefinition = {type: "button",model: new ViewModel({- _boxSizeValue: sizeDef.value,- label: sizeDef.label,+ _boxSizeValue: size,+ label: getBoxSizeLabel(editor.t, size),role: "menuitemradio",withText: true})};definition.model.bind("isOn").to(command, "value", value => {- return value === sizeDef.value;+ return value === size;});itemDefinitions.add(definition);diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.tsindex 2f98968732..7d1adf9d1f 100644--- a/packages/ckeditor5/src/plugins/includenote.spec.ts+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts@@ -225,7 +225,7 @@ describe("IncludeNote", () => {expect(command.value).toBe("medium");// Execute through every defined box size to cover the model write path.- for (const { value } of BOX_SIZES) {+ for (const value of BOX_SIZES) {editor.execute(BOX_SIZE_COMMAND_NAME, { value });expect(command.value).toBe(value);expect(findIncludeNote(editor)?.getAttribute("boxSize")).toBe(value);diff --git a/packages/ckeditor5/src/plugins/includenote.ts b/packages/ckeditor5/src/plugins/includenote.tsindex dd354288a7..845be08394 100644--- a/packages/ckeditor5/src/plugins/includenote.ts+++ b/packages/ckeditor5/src/plugins/includenote.ts@@ -4,14 +4,34 @@ import noteIcon from '../icons/note.svg?raw';export const COMMAND_NAME = 'insertIncludeNote';export const BOX_SIZE_COMMAND_NAME = 'includeNoteBoxSize';-export const BOX_SIZES = [- { value: 'small', label: 'Small' },- { value: 'medium', label: 'Medium' },- { value: 'full', label: 'Full' },- { value: 'expandable', label: 'Expandable' }-] as const;+export const BOX_SIZES = [ 'small', 'medium', 'full', 'expandable' ] as const;-export type BoxSizeValue = typeof BOX_SIZES[number]['value'];+export type BoxSizeValue = typeof BOX_SIZES[number];++/**+ * The user-facing name of a box size, as shown by the widget toolbar's dropdown.+ *+ * A switch rather than a table of labels, so that each one is written as a literal argument of a+ * `t()` call: that is how the messages this package owns are discovered (see `messages.ts`) and a+ * label tucked away in a table would be invisible to translators.+ *+ * @param t the editor's translation function.+ * @param size the box size; an unrecognized one has no label and is returned as-is.+ */+export function getBoxSizeLabel(t: (message: string) => string, size: BoxSizeValue): string {+ switch (size) {+ case 'small':+ return t('Small');+ case 'medium':+ return t('Medium');+ case 'full':+ return t('Full');+ case 'expandable':+ return t('Expandable');+ default:+ return size;+ }+}export default class IncludeNote extends Plugin {static get requires() {@@ -143,7 +163,9 @@ class IncludeNoteEditing extends Plugin {// hasSelectionHandle gives the block widget CKEditor's own drag grip so it moves// atomically, instead of the browser's native drag tearing the embedded note apart.- return toWidget( section, viewWriter, { label: 'include note widget', hasSelectionHandle: true } );+ // The label is announced by screen readers; lowercase to match the "image widget" /+ // "table widget" labels CKEditor gives its own widgets.+ return toWidget( section, viewWriter, { label: editor.t('include note widget'), hasSelectionHandle: true } );}} );