Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): render keyboard shortcuts through the translatable formatter

Elian Doran4ebf8db9dc1f
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.ts
index 130b4cfd52..541f387166 100644
--- a/apps/client/src/menus/context_menu.ts
+++ b/apps/client/src/menus/context_menu.ts
@@ -2,6 +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 note_tooltip from "../services/note_tooltip.js";
import utils from "../services/utils.js";
@@ -277,7 +278,7 @@ class ContextMenu {
if (shortcuts) {
const allShortcuts: string[] = [];
for (const effectiveShortcut of shortcuts) {
- allShortcuts.push(effectiveShortcut.split("+")
+ allShortcuts.push(formatShortcutLocalized(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 5ad87458b2..85e6f989d2 100644
--- a/apps/client/src/services/command_registry.ts
+++ b/apps/client/src/services/command_registry.ts
@@ -3,6 +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 utils from "./utils.js";
export interface CommandDefinition {
@@ -174,10 +175,8 @@ export class CommandRegistry {
}
private formatShortcut(shortcut: string): string {
- // Convert electron accelerator format to display format
- return shortcut
- .replace(/CommandOrControl/g, 'Ctrl')
- .replace(/\+/g, ' + ');
+ // Localize each key token and render the combination in the command-palette style (spaced +).
+ return formatShortcutLocalized(shortcut).join(" + ");
}
register(command: CommandDefinition) {
diff --git a/apps/client/src/services/keyboard_actions.spec.ts b/apps/client/src/services/keyboard_actions.spec.ts
index 881cf5289c..4cabde174f 100644
--- a/apps/client/src/services/keyboard_actions.spec.ts
+++ b/apps/client/src/services/keyboard_actions.spec.ts
@@ -233,7 +233,7 @@ describe("keyboard_actions", () => {
<kbd data-command="cmdMissing">not set</kbd>
<span data-trigger-command="cmdTrigger" title="My label"></span>
<span data-trigger-command="cmdTriggerNoTitle"></span>
- <span data-trigger-command="cmdTriggerAlready" title="X (ctrl+x)"></span>
+ <span data-trigger-command="cmdTriggerAlready" title="X (Ctrl+x)"></span>
<span data-trigger-command=""></span>
<span data-trigger-command="cmdMissing"></span>
</div>
@@ -244,18 +244,19 @@ describe("keyboard_actions", () => {
// jQuery .each runs synchronously but the async getAction inside resolves later
await new Promise((r) => setTimeout(r, 0));
- expect($container.find('kbd[data-command="cmdKbd"]').text()).toBe("ctrl+a, ctrl+b");
+ // Shortcuts are localized for display: the modifier token normalizes to its canonical label.
+ expect($container.find('kbd[data-command="cmdKbd"]').text()).toBe("Ctrl+a, Ctrl+b");
// empty shortcuts but text already "not set" -> condition false -> text untouched
expect($container.find('kbd[data-command="cmdEmpty"]').text()).toBe("not set");
// missing action -> getAction silent returns undefined -> skipped
expect($container.find('kbd[data-command="cmdMissing"]').text()).toBe("not set");
// title with content gets the shortcut appended in parentheses
- expect($container.find('[data-trigger-command="cmdTrigger"]').attr("title")).toBe("My label (ctrl+t)");
+ expect($container.find('[data-trigger-command="cmdTrigger"]').attr("title")).toBe("My label (Ctrl+t)");
// no title -> becomes just the shortcuts
- expect($container.find('[data-trigger-command="cmdTriggerNoTitle"]').attr("title")).toBe("ctrl+n");
+ expect($container.find('[data-trigger-command="cmdTriggerNoTitle"]').attr("title")).toBe("Ctrl+n");
// title already includes the shortcut -> early return, unchanged
- expect($container.find('[data-trigger-command="cmdTriggerAlready"]').attr("title")).toBe("X (ctrl+x)");
+ expect($container.find('[data-trigger-command="cmdTriggerAlready"]').attr("title")).toBe("X (Ctrl+x)");
});
it("updateDisplayedShortcuts sets text when shortcuts empty but element text differs from 'not set'", async () => {
diff --git a/apps/client/src/services/keyboard_actions.ts b/apps/client/src/services/keyboard_actions.ts
index e9d9625a11..ce70a13e10 100644
--- a/apps/client/src/services/keyboard_actions.ts
+++ b/apps/client/src/services/keyboard_actions.ts
@@ -1,5 +1,6 @@
import server from "./server.js";
import appContext from "../components/app_context.js";
+import { formatShortcutLocalized } from "./keyboard_shortcut_display.js";
import shortcutService, { ShortcutBinding } from "./shortcuts.js";
import type Component from "../components/component.js";
import type { ActionKeyboardShortcut } from "@triliumnext/commons";
@@ -89,7 +90,7 @@ function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {
const action = await getAction(actionName, true);
if (action) {
- const keyboardActions = (action.effectiveShortcuts ?? []).join(", ");
+ const keyboardActions = formatShortcutList(action.effectiveShortcuts);
if (keyboardActions || $(el).text() !== "not set") {
$(el).text(keyboardActions);
@@ -108,7 +109,7 @@ function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {
if (action) {
const title = $(el).attr("title");
- const shortcuts = (action.effectiveShortcuts ?? []).join(", ");
+ const shortcuts = formatShortcutList(action.effectiveShortcuts);
if (title?.includes(shortcuts)) {
return;
@@ -128,3 +129,8 @@ export default {
getActions,
getActionsForScope
};
+
+/** 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(", ");
+}
diff --git a/apps/client/src/services/keyboard_shortcut_display.ts b/apps/client/src/services/keyboard_shortcut_display.ts
index 461ea2f811..656a5eb7f2 100644
--- a/apps/client/src/services/keyboard_shortcut_display.ts
+++ b/apps/client/src/services/keyboard_shortcut_display.ts
@@ -17,6 +17,8 @@
* that resolves `${SHORTCUT_KEY_PREFIX}.<id>`.
*/
+import { t } from "./i18n.js";
+
/** i18n key prefix under which the per-token labels live once this mapper is wired to translations. */
export const SHORTCUT_KEY_PREFIX = "keyboard_shortcut_keys";
@@ -25,6 +27,26 @@ export type ShortcutKeyTranslator = (id: string) => string | null | undefined;
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} remains available for callers
+ * that need to inject their own translator (e.g. tests) or the built-in English defaults.
+ */
+export function formatShortcutLocalized(shortcut: string): string[] {
+ return formatShortcut(shortcut, translateShortcutKey);
+}
+
+/**
+ * The default i18n-backed token translator: resolves `${SHORTCUT_KEY_PREFIX}.<id>`. Returns
+ * `undefined` when the key is missing (i18next echoes the key back), so {@link formatShortcutKey}
+ * falls back to the built-in English label.
+ */
+export function translateShortcutKey(id: string): string | undefined {
+ const key = `${SHORTCUT_KEY_PREFIX}.${id}`;
+ const value = t(key);
+ return value === key ? undefined : value;
+}
+
/**
* 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
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index c96ab30dba..d2941cbc78 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1935,6 +1935,28 @@
"conflicts_badge_other": "{{count}} conflicts",
"conflicts_badge_tooltip": "Some shortcuts are assigned to more than one action. Click to show only the conflicting shortcuts."
},
+ "keyboard_shortcut_keys": {
+ "ctrl": "Ctrl",
+ "alt": "Alt",
+ "shift": "Shift",
+ "meta": "Meta",
+ "enter": "Enter",
+ "escape": "Esc",
+ "delete": "Delete",
+ "backspace": "Backspace",
+ "space": "Space",
+ "tab": "Tab",
+ "home": "Home",
+ "end": "End",
+ "page_up": "Page Up",
+ "page_down": "Page Down",
+ "up": "Up",
+ "down": "Down",
+ "left": "Left",
+ "right": "Right",
+ "plus": "+",
+ "insert": "Insert"
+ },
"spellcheck": {
"title": "Spell Check",
"description": "These options apply only for desktop builds, browsers will use their own native spell check.",
diff --git a/apps/client/src/widgets/dialogs/help.tsx b/apps/client/src/widgets/dialogs/help.tsx
index a775cbadfc..8e839e8afd 100644
--- a/apps/client/src/widgets/dialogs/help.tsx
+++ b/apps/client/src/widgets/dialogs/help.tsx
@@ -5,6 +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 { useTriliumEvent } from "../react/hooks.jsx";
export default function HelpDialog() {
@@ -141,7 +142,7 @@ function FixedKeyboardShortcut({ keys, description }: { keys?: string[], descrip
<li>
{keys && keys.map((key, index) =>
<>
- <kbd key={index}>{key}</kbd>
+ <kbd key={index}>{formatShortcutLocalized(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 b6882a5342..c482ff1b65 100644
--- a/apps/client/src/widgets/react/ActionButton.tsx
+++ b/apps/client/src/widgets/react/ActionButton.tsx
@@ -4,6 +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 { isMobile } from "../../services/utils";
import { useStaticTooltip } from "./hooks";
@@ -29,7 +30,9 @@ export default function ActionButton({ text, icon, className, triggerCommand, ti
const buttonRef = useRef<HTMLButtonElement>(null);
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
- const title = keyboardShortcut?.length ? `${text} (${keyboardShortcut.join(",")})` : text;
+ const title = keyboardShortcut?.length
+ ? `${text} (${keyboardShortcut.map((shortcut) => formatShortcutLocalized(shortcut).join("+")).join(", ")})`
+ : text;
const titleRef = useRef(title);
titleRef.current = title;
const hasTitle = !!title && title.length > 0;
diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsx
index 07b70c2133..579a05589f 100644
--- a/apps/client/src/widgets/react/Button.tsx
+++ b/apps/client/src/widgets/react/Button.tsx
@@ -2,6 +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 { isDesktop, isMobile } from "../../services/utils";
import ActionButton from "./ActionButton";
import Icon from "./Icon";
@@ -57,11 +58,11 @@ function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, i
// Memoize keyboard shortcut rendering
const shortcutElements = useMemo(() => {
if (!keyboardShortcut || cachedIsMobile) return null;
- const splitShortcut = keyboardShortcut.split("+");
- return splitShortcut.map((key, index) => (
+ const keys = formatShortcutLocalized(keyboardShortcut);
+ return keys.map((key, index) => (
<>
- <kbd key={index}>{key.toUpperCase()}</kbd>
- {index < splitShortcut.length - 1 ? "+" : ""}
+ <kbd key={index}>{key}</kbd>
+ {index < keys.length - 1 ? "+" : ""}
</>
));
}, [keyboardShortcut]);
diff --git a/apps/client/src/widgets/react/KeyboardShortcut.tsx b/apps/client/src/widgets/react/KeyboardShortcut.tsx
index 0a76b80939..035377ce4c 100644
--- a/apps/client/src/widgets/react/KeyboardShortcut.tsx
+++ b/apps/client/src/widgets/react/KeyboardShortcut.tsx
@@ -1,6 +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 { joinElements } from "./react_utils";
import utils from "../../services/utils";
@@ -23,12 +24,9 @@ export default function KeyboardShortcut({ actionName }: KeyboardShortcutProps)
return (!isMobile &&
<span className="keyboard-shortcut">
- {joinElements(action.effectiveShortcuts?.map((shortcut) => {
- const keys = shortcut.split("+");
- return joinElements(
- keys.map((key, i) => <kbd>{key}</kbd>)
- , "+");
- }))}
+ {joinElements(action.effectiveShortcuts?.map((shortcut) =>
+ joinElements(formatShortcutLocalized(shortcut).map((key) => <kbd>{key}</kbd>), "+")
+ ))}
</span>
);
}
\ No newline at end of file
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
index 3e5e24bc98..5457f404c6 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
@@ -9,6 +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 { canonicalizeShortcut, KEYCODES_WITH_NO_MODIFIER } from "../../../services/shortcuts";
import toast from "../../../services/toast";
import { arrayEqual, isElectron, isMobile, reloadFrontendApp } from "../../../services/utils";
@@ -413,7 +414,7 @@ function revertShortcut(action: ActionKeyboardShortcut) {
function formatDefaultShortcuts(action: ActionKeyboardShortcut) {
return action.defaultShortcuts?.length
- ? action.defaultShortcuts.join(", ")
+ ? action.defaultShortcuts.map((shortcut) => formatShortcutLocalized(shortcut).join("+")).join(", ")
: t("shortcuts.no_default_shortcut");
}
@@ -488,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>{stripGlobalPrefix(shortcut)}</kbd>
+ <kbd>{formatShortcutLocalized(shortcut).join("+")}</kbd>
<TooltipButton
className="shortcut-chip-action shortcut-chip-remove"
title={t("shortcuts.remove_shortcut")}