Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): call t directly instead of threading a translator

Elian Doran7c667f99a181
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.ts
index 541f387166..7ee55ac14d 100644
--- a/apps/client/src/menus/context_menu.ts
+++ b/apps/client/src/menus/context_menu.ts
@@ -2,7 +2,7 @@ import { KeyboardActionNames } from "@triliumnext/commons";
import { h, JSX, render } from "preact";
import keyboardActionService, { getActionSync } from "../services/keyboard_actions.js";
-import { formatShortcutLocalized } from "../services/keyboard_shortcut_display.js";
+import { formatShortcut } from "../services/keyboard_shortcut_display.js";
import note_tooltip from "../services/note_tooltip.js";
import utils from "../services/utils.js";
@@ -278,7 +278,7 @@ class ContextMenu {
if (shortcuts) {
const allShortcuts: string[] = [];
for (const effectiveShortcut of shortcuts) {
- allShortcuts.push(formatShortcutLocalized(effectiveShortcut)
+ allShortcuts.push(formatShortcut(effectiveShortcut)
.map(key => `<kbd>${key}</kbd>`)
.join("+"));
}
diff --git a/apps/client/src/services/command_registry.ts b/apps/client/src/services/command_registry.ts
index 85e6f989d2..2f7ac23507 100644
--- a/apps/client/src/services/command_registry.ts
+++ b/apps/client/src/services/command_registry.ts
@@ -3,7 +3,7 @@ import appContext, { type CommandNames } from "../components/app_context.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
import { t, translationsInitializedPromise } from "./i18n.js";
import keyboardActions from "./keyboard_actions.js";
-import { formatShortcutLocalized } from "./keyboard_shortcut_display.js";
+import { formatShortcut } from "./keyboard_shortcut_display.js";
import utils from "./utils.js";
export interface CommandDefinition {
@@ -164,7 +164,8 @@ export class CommandRegistry {
name,
description: action.description,
icon: action.iconClass,
- shortcut: primaryShortcut ? this.formatShortcut(primaryShortcut) : undefined,
+ // Render the primary shortcut in the command-palette style (spaced +).
+ shortcut: primaryShortcut ? formatShortcut(primaryShortcut).join(" + ") : undefined,
commandName: action.actionName as CommandNames,
source: "keyboard-action",
keyboardAction: action
@@ -174,11 +175,6 @@ export class CommandRegistry {
}
}
- private formatShortcut(shortcut: string): string {
- // Localize each key token and render the combination in the command-palette style (spaced +).
- return formatShortcutLocalized(shortcut).join(" + ");
- }
-
register(command: CommandDefinition) {
this.commands.set(command.id, command);
diff --git a/apps/client/src/services/keyboard_actions.ts b/apps/client/src/services/keyboard_actions.ts
index ce70a13e10..6d1d64fe08 100644
--- a/apps/client/src/services/keyboard_actions.ts
+++ b/apps/client/src/services/keyboard_actions.ts
@@ -1,6 +1,6 @@
import server from "./server.js";
import appContext from "../components/app_context.js";
-import { formatShortcutLocalized } from "./keyboard_shortcut_display.js";
+import { formatShortcut } from "./keyboard_shortcut_display.js";
import shortcutService, { ShortcutBinding } from "./shortcuts.js";
import type Component from "../components/component.js";
import type { ActionKeyboardShortcut } from "@triliumnext/commons";
@@ -132,5 +132,5 @@ export default {
/** Renders a list of stored shortcuts into a localized, comma-separated display string. */
function formatShortcutList(shortcuts: string[] | undefined) {
- return (shortcuts ?? []).map((shortcut) => formatShortcutLocalized(shortcut).join("+")).join(", ");
+ return (shortcuts ?? []).map((shortcut) => formatShortcut(shortcut).join("+")).join(", ");
}
diff --git a/apps/client/src/services/keyboard_shortcut_display.spec.ts b/apps/client/src/services/keyboard_shortcut_display.spec.ts
index 4dacad4f17..b59469ea51 100644
--- a/apps/client/src/services/keyboard_shortcut_display.spec.ts
+++ b/apps/client/src/services/keyboard_shortcut_display.spec.ts
@@ -1,16 +1,23 @@
import { describe, expect, it, vi } from "vitest";
+// Stub the labels the same way the sibling specs (command_registry, keyboard_actions) do: resolve
+// `keyboard_shortcut_keys.<id>` to a recognizable `L·<ID>` so translated output is unmistakable, and
+// anything else back to its key. A glyph/pass-through result (e.g. "↑", not "L·UP") then proves i18n
+// was not consulted for that token.
+vi.mock("./i18n.js", () => ({
+ t: (key: string) =>
+ key.startsWith("keyboard_shortcut_keys.")
+ ? `L·${key.slice("keyboard_shortcut_keys.".length).toUpperCase()}`
+ : key
+}));
+
import {
formatShortcut,
formatShortcutKey,
SHORTCUT_KEY_PREFIX,
- splitShortcutForDisplay,
- type ShortcutKeyTranslator
+ splitShortcutForDisplay
} from "./keyboard_shortcut_display.js";
-/** Fake i18n resolver: uppercases the id and prefixes `L·` so translated output is unmistakable. */
-const fakeTranslate: ShortcutKeyTranslator = (id) => `L·${id.toUpperCase()}`;
-
describe("keyboard_shortcut_display", () => {
describe("splitShortcutForDisplay", () => {
it("splits modifiers and the final key, preserving order and casing", () => {
@@ -46,63 +53,59 @@ describe("keyboard_shortcut_display", () => {
});
describe("formatShortcutKey", () => {
- it("resolves modifiers and their aliases through the translator to one id", () => {
- expect(formatShortcutKey("Ctrl", fakeTranslate)).toBe("L·CTRL");
- expect(formatShortcutKey("Control", fakeTranslate)).toBe("L·CTRL");
- expect(formatShortcutKey("CommandOrControl", fakeTranslate)).toBe("L·CTRL");
- expect(formatShortcutKey("Meta", fakeTranslate)).toBe("L·META");
- expect(formatShortcutKey("Cmd", fakeTranslate)).toBe("L·META");
- expect(formatShortcutKey("Command", fakeTranslate)).toBe("L·META");
+ it("resolves modifiers and their aliases through i18n to one id", () => {
+ expect(formatShortcutKey("Ctrl")).toBe("L·CTRL");
+ expect(formatShortcutKey("Control")).toBe("L·CTRL");
+ expect(formatShortcutKey("CommandOrControl")).toBe("L·CTRL");
+ expect(formatShortcutKey("Meta")).toBe("L·META");
+ expect(formatShortcutKey("Cmd")).toBe("L·META");
+ expect(formatShortcutKey("Command")).toBe("L·META");
});
it("resolves named-key aliases to one id and matches case-insensitively", () => {
- expect(formatShortcutKey("Enter", fakeTranslate)).toBe("L·ENTER");
- expect(formatShortcutKey("Return", fakeTranslate)).toBe("L·ENTER");
- expect(formatShortcutKey("Del", fakeTranslate)).toBe("L·DELETE");
- expect(formatShortcutKey("delete", fakeTranslate)).toBe("L·DELETE");
- expect(formatShortcutKey("ESC", fakeTranslate)).toBe("L·ESCAPE");
- expect(formatShortcutKey("PageDown", fakeTranslate)).toBe("L·PAGE_DOWN");
+ expect(formatShortcutKey("Enter")).toBe("L·ENTER");
+ expect(formatShortcutKey("Return")).toBe("L·ENTER");
+ expect(formatShortcutKey("Del")).toBe("L·DELETE");
+ expect(formatShortcutKey("delete")).toBe("L·DELETE");
+ expect(formatShortcutKey("ESC")).toBe("L·ESCAPE");
+ expect(formatShortcutKey("PageDown")).toBe("L·PAGE_DOWN");
});
- it("renders glyph keys (arrows, plus) as their universal symbol without consulting the translator", () => {
- const translate = vi.fn(fakeTranslate);
- expect(formatShortcutKey("ArrowUp", translate)).toBe("↑");
- expect(formatShortcutKey("up", translate)).toBe("↑");
- expect(formatShortcutKey("Down", translate)).toBe("↓");
- expect(formatShortcutKey("Left", translate)).toBe("←");
- expect(formatShortcutKey("Right", translate)).toBe("→");
- expect(formatShortcutKey("Plus", translate)).toBe("+");
- expect(formatShortcutKey("+", translate)).toBe("+");
- expect(translate).not.toHaveBeenCalled();
+ it("renders glyph keys (arrows, plus) as their universal symbol, not a translated label", () => {
+ expect(formatShortcutKey("ArrowUp")).toBe("↑");
+ expect(formatShortcutKey("up")).toBe("↑");
+ expect(formatShortcutKey("Down")).toBe("↓");
+ expect(formatShortcutKey("Left")).toBe("←");
+ expect(formatShortcutKey("Right")).toBe("→");
+ expect(formatShortcutKey("Plus")).toBe("+");
+ expect(formatShortcutKey("+")).toBe("+");
});
- it("passes non-table tokens through verbatim without consulting the translator", () => {
- const translate = vi.fn(fakeTranslate);
- expect(formatShortcutKey("J", translate)).toBe("J");
- expect(formatShortcutKey("5", translate)).toBe("5");
- expect(formatShortcutKey("F11", translate)).toBe("F11");
- expect(formatShortcutKey("[", translate)).toBe("[");
- expect(formatShortcutKey("=", translate)).toBe("=");
- expect(translate).not.toHaveBeenCalled();
+ it("passes non-table tokens through verbatim, not a translated label", () => {
+ expect(formatShortcutKey("J")).toBe("J");
+ expect(formatShortcutKey("5")).toBe("5");
+ expect(formatShortcutKey("F11")).toBe("F11");
+ expect(formatShortcutKey("[")).toBe("[");
+ expect(formatShortcutKey("=")).toBe("=");
});
});
describe("formatShortcut", () => {
it("resolves translatable tokens and leaves glyph/pass-through tokens intact", () => {
- expect(formatShortcut("Ctrl+Shift+J", fakeTranslate)).toEqual([ "L·CTRL", "L·SHIFT", "J" ]);
- expect(formatShortcut("Alt+Left", fakeTranslate)).toEqual([ "L·ALT", "←" ]);
- expect(formatShortcut("Meta+Plus", fakeTranslate)).toEqual([ "L·META", "+" ]);
- expect(formatShortcut("F5", fakeTranslate)).toEqual([ "F5" ]);
- expect(formatShortcut("Ctrl+Up", fakeTranslate)).toEqual([ "L·CTRL", "↑" ]);
+ expect(formatShortcut("Ctrl+Shift+J")).toEqual([ "L·CTRL", "L·SHIFT", "J" ]);
+ expect(formatShortcut("Alt+Left")).toEqual([ "L·ALT", "←" ]);
+ expect(formatShortcut("Meta+Plus")).toEqual([ "L·META", "+" ]);
+ expect(formatShortcut("F5")).toEqual([ "F5" ]);
+ expect(formatShortcut("Ctrl+Up")).toEqual([ "L·CTRL", "↑" ]);
});
it("strips global: and normalizes aliases end-to-end", () => {
- expect(formatShortcut("global:CommandOrControl+Alt+P", fakeTranslate)).toEqual([ "L·CTRL", "L·ALT", "P" ]);
- expect(formatShortcut("Cmd+ArrowRight", fakeTranslate)).toEqual([ "L·META", "→" ]);
+ expect(formatShortcut("global:CommandOrControl+Alt+P")).toEqual([ "L·CTRL", "L·ALT", "P" ]);
+ expect(formatShortcut("Cmd+ArrowRight")).toEqual([ "L·META", "→" ]);
});
it("returns [] for an empty shortcut", () => {
- expect(formatShortcut("", fakeTranslate)).toEqual([]);
+ expect(formatShortcut("")).toEqual([]);
});
});
diff --git a/apps/client/src/services/keyboard_shortcut_display.ts b/apps/client/src/services/keyboard_shortcut_display.ts
index 730154ba5b..14bf1a115a 100644
--- a/apps/client/src/services/keyboard_shortcut_display.ts
+++ b/apps/client/src/services/keyboard_shortcut_display.ts
@@ -1,5 +1,5 @@
/**
- * Translatable, display-only formatting of keyboard shortcut strings.
+ * Display-only formatting of keyboard shortcut strings.
*
* Shortcuts are *stored* in a canonical, language-independent format (e.g. `Ctrl+Shift+J`,
* `Alt+Left`, `Meta+Plus`). That stored form is load-bearing — it drives keystroke matching,
@@ -10,13 +10,11 @@
* The single rule is: split the shortcut on `+`, then classify each token:
* - a *glyph* token (arrow keys, the plus key) renders a universal symbol — identical in every
* language, so it is a constant here and never translated;
- * - a *translatable* token (modifier or named key) resolves its label entirely through the injected
- * translator, keyed by an id under {@link SHORTCUT_KEY_PREFIX}; the English source lives in the
- * translation files, not here;
+ * - a *translatable* token (modifier or named key) resolves its label through i18n, keyed by an id
+ * under {@link SHORTCUT_KEY_PREFIX}; the label text lives in the translation files, not here;
* - any other token — letter, digit, function key, punctuation — is emitted verbatim.
*
- * The translator is injected so the core is unit-testable with a stub; production callers use
- * {@link formatShortcutLocalized}, which binds the application's i18n runtime.
+ * Tests stub the labels by mocking `./i18n.js`.
*/
import { t } from "./i18n.js";
@@ -24,25 +22,8 @@ import { t } from "./i18n.js";
/** i18n key prefix under which the per-token labels live. */
export const SHORTCUT_KEY_PREFIX = "keyboard_shortcut_keys";
-/** Resolves a token id (e.g. `"ctrl"`) to its localized label via the translation files. */
-export type ShortcutKeyTranslator = (id: string) => string;
-
const GLOBAL_PREFIX = "global:";
-/**
- * Formats a stored shortcut into localized display tokens using the application's i18n runtime. This
- * is the binding every display site should use; {@link formatShortcut} accepts an injected translator
- * for tests.
- */
-export function formatShortcutLocalized(shortcut: string): string[] {
- return formatShortcut(shortcut, translateShortcutKey);
-}
-
-/** The i18n-backed token translator: resolves `${SHORTCUT_KEY_PREFIX}.<id>` from the translation files. */
-export function translateShortcutKey(id: string): string {
- return t(`${SHORTCUT_KEY_PREFIX}.${id}`);
-}
-
/**
* Formats a single stored shortcut string into an ordered list of display tokens. The array is the
* flexible primitive every display site can build on: wrap each token in its own `<kbd>`, join with
@@ -51,10 +32,9 @@ export function translateShortcutKey(id: string): string {
* A leading `global:` prefix (a storage detail) is stripped so it never leaks into the display.
*
* @param shortcut a stored shortcut, e.g. `"Ctrl+Shift+J"`, `"global:Meta+Plus"`, `"F5"`.
- * @param translate token-id → label resolver (see {@link translateShortcutKey}).
*/
-export function formatShortcut(shortcut: string, translate: ShortcutKeyTranslator): string[] {
- return splitShortcutForDisplay(shortcut).map((token) => formatShortcutKey(token, translate));
+export function formatShortcut(shortcut: string): string[] {
+ return splitShortcutForDisplay(shortcut).map((token) => formatShortcutKey(token));
}
/**
@@ -79,10 +59,10 @@ export function splitShortcutForDisplay(shortcut: string): string[] {
/**
* Renders one shortcut token (matched case-insensitively): a glyph token ({@link KEY_GLYPHS}) becomes
- * its universal symbol, a translatable token ({@link TRANSLATABLE_KEYS}) is resolved through
- * `translate`, and any other token — letter, digit, function key, punctuation — is returned unchanged.
+ * its universal symbol, a translatable token ({@link TRANSLATABLE_KEYS}) is resolved through i18n, and
+ * any other token — letter, digit, function key, punctuation — is returned unchanged.
*/
-export function formatShortcutKey(token: string, translate: ShortcutKeyTranslator): string {
+export function formatShortcutKey(token: string): string {
const lower = token.toLowerCase();
const glyph = KEY_GLYPHS[lower];
@@ -92,7 +72,7 @@ export function formatShortcutKey(token: string, translate: ShortcutKeyTranslato
const id = TRANSLATABLE_KEYS[lower];
if (id) {
- return translate(id);
+ return t(`${SHORTCUT_KEY_PREFIX}.${id}`);
}
return token;
diff --git a/apps/client/src/widgets/dialogs/help.tsx b/apps/client/src/widgets/dialogs/help.tsx
index 8e839e8afd..d0319b051e 100644
--- a/apps/client/src/widgets/dialogs/help.tsx
+++ b/apps/client/src/widgets/dialogs/help.tsx
@@ -5,7 +5,7 @@ import appContext, { CommandNames } from "../../components/app_context.js";
import RawHtml from "../react/RawHtml.jsx";
import { useEffect, useState } from "preact/hooks";
import keyboard_actions from "../../services/keyboard_actions.js";
-import { formatShortcutLocalized } from "../../services/keyboard_shortcut_display.js";
+import { formatShortcut } from "../../services/keyboard_shortcut_display.js";
import { useTriliumEvent } from "../react/hooks.jsx";
export default function HelpDialog() {
@@ -142,7 +142,7 @@ function FixedKeyboardShortcut({ keys, description }: { keys?: string[], descrip
<li>
{keys && keys.map((key, index) =>
<>
- <kbd key={index}>{formatShortcutLocalized(key).join("+")}</kbd>
+ <kbd key={index}>{formatShortcut(key).join("+")}</kbd>
{index < keys.length - 1 ? ", " : "" }
</>
)} - <RawHtml html={description} />
diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsx
index c482ff1b65..61472b85dc 100644
--- a/apps/client/src/widgets/react/ActionButton.tsx
+++ b/apps/client/src/widgets/react/ActionButton.tsx
@@ -4,7 +4,7 @@ import { useEffect, useMemo, useRef, useState } from "preact/hooks";
import { CommandNames } from "../../components/app_context";
import keyboard_actions from "../../services/keyboard_actions";
-import { formatShortcutLocalized } from "../../services/keyboard_shortcut_display";
+import { formatShortcut } from "../../services/keyboard_shortcut_display";
import { isMobile } from "../../services/utils";
import { useStaticTooltip } from "./hooks";
@@ -31,7 +31,7 @@ export default function ActionButton({ text, icon, className, triggerCommand, ti
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
const title = keyboardShortcut?.length
- ? `${text} (${keyboardShortcut.map((shortcut) => formatShortcutLocalized(shortcut).join("+")).join(", ")})`
+ ? `${text} (${keyboardShortcut.map((shortcut) => formatShortcut(shortcut).join("+")).join(", ")})`
: text;
const titleRef = useRef(title);
titleRef.current = title;
diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsx
index 579a05589f..7b2199ba88 100644
--- a/apps/client/src/widgets/react/Button.tsx
+++ b/apps/client/src/widgets/react/Button.tsx
@@ -2,7 +2,7 @@ import type { ComponentChildren, CSSProperties, RefObject } from "preact";
import { useMemo } from "preact/hooks";
import { CommandNames } from "../../components/app_context";
-import { formatShortcutLocalized } from "../../services/keyboard_shortcut_display";
+import { formatShortcut } from "../../services/keyboard_shortcut_display";
import { isDesktop, isMobile } from "../../services/utils";
import ActionButton from "./ActionButton";
import Icon from "./Icon";
@@ -58,7 +58,7 @@ function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, i
// Memoize keyboard shortcut rendering
const shortcutElements = useMemo(() => {
if (!keyboardShortcut || cachedIsMobile) return null;
- const keys = formatShortcutLocalized(keyboardShortcut);
+ const keys = formatShortcut(keyboardShortcut);
return keys.map((key, index) => (
<>
<kbd key={index}>{key}</kbd>
diff --git a/apps/client/src/widgets/react/KeyboardShortcut.tsx b/apps/client/src/widgets/react/KeyboardShortcut.tsx
index 035377ce4c..b1aa2226e2 100644
--- a/apps/client/src/widgets/react/KeyboardShortcut.tsx
+++ b/apps/client/src/widgets/react/KeyboardShortcut.tsx
@@ -1,7 +1,7 @@
import { ActionKeyboardShortcut, KeyboardActionNames } from "@triliumnext/commons";
import { useEffect, useState } from "preact/hooks";
import keyboard_actions from "../../services/keyboard_actions";
-import { formatShortcutLocalized } from "../../services/keyboard_shortcut_display";
+import { formatShortcut } from "../../services/keyboard_shortcut_display";
import { joinElements } from "./react_utils";
import utils from "../../services/utils";
@@ -25,7 +25,7 @@ export default function KeyboardShortcut({ actionName }: KeyboardShortcutProps)
return (!isMobile &&
<span className="keyboard-shortcut">
{joinElements(action.effectiveShortcuts?.map((shortcut) =>
- joinElements(formatShortcutLocalized(shortcut).map((key) => <kbd>{key}</kbd>), "+")
+ joinElements(formatShortcut(shortcut).map((key) => <kbd>{key}</kbd>), "+")
))}
</span>
);
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
index 5457f404c6..66a959ec47 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
@@ -9,7 +9,7 @@ import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n";
import options from "../../../services/options";
import server from "../../../services/server";
-import { formatShortcutLocalized } from "../../../services/keyboard_shortcut_display";
+import { formatShortcut } from "../../../services/keyboard_shortcut_display";
import { canonicalizeShortcut, KEYCODES_WITH_NO_MODIFIER } from "../../../services/shortcuts";
import toast from "../../../services/toast";
import { arrayEqual, isElectron, isMobile, reloadFrontendApp } from "../../../services/utils";
@@ -414,7 +414,7 @@ function revertShortcut(action: ActionKeyboardShortcut) {
function formatDefaultShortcuts(action: ActionKeyboardShortcut) {
return action.defaultShortcuts?.length
- ? action.defaultShortcuts.map((shortcut) => formatShortcutLocalized(shortcut).join("+")).join(", ")
+ ? action.defaultShortcuts.map((shortcut) => formatShortcut(shortcut).join("+")).join(", ")
: t("shortcuts.no_default_shortcut");
}
@@ -489,7 +489,7 @@ function ShortcutEditor({ keyboardShortcut: action, conflicts }: { keyboardShort
</TooltipButton>
)
: global && <TooltipIcon icon="bx bx-globe" className="shortcut-chip-global-indicator" tooltip={t("shortcuts.global_shortcut")} tooltipClass="tooltip-top" />}
- <kbd>{formatShortcutLocalized(shortcut).join("+")}</kbd>
+ <kbd>{formatShortcut(shortcut).join("+")}</kbd>
<TooltipButton
className="shortcut-chip-action shortcut-chip-remove"
title={t("shortcuts.remove_shortcut")}