diff --git a/src/features/plugins/commandTitle.test.ts b/src/features/plugins/commandTitle.test.ts new file mode 100644 index 0000000000..ac30a300bb --- /dev/null +++ b/src/features/plugins/commandTitle.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { usePluginCatalogs } from "./pluginText"; +import { commandTitle, usePlugins } from "./store"; + +describe("commandTitle", () => { + afterEach(() => { + usePlugins.setState({ ui: [] }); + usePluginCatalogs.setState({ catalogs: {} }); + }); + + it("uses the plugin's translation of the command title", () => { + usePlugins.setState({ + ui: [{ pluginId: "a", point: "commands", key: "show", decl: { id: "show", title: "Note: Show" } }], + }); + usePluginCatalogs.setState({ catalogs: { a: { "Note: Show": "筆記:顯示" }, b: { "Note: Show": "x" } } }); + expect(commandTitle("a", "show")).toBe("筆記:顯示"); + }); + + it("keeps the declared title without a translation and the id without a title", () => { + usePlugins.setState({ + ui: [{ pluginId: "a", point: "commands", key: "show", decl: { id: "show", title: "Note: Show" } }], + }); + expect(commandTitle("a", "show")).toBe("Note: Show"); + expect(commandTitle("a", "missing")).toBe("missing"); + }); +}); diff --git a/src/features/plugins/store.ts b/src/features/plugins/store.ts index cd468ca4ca..9b3024039a 100644 --- a/src/features/plugins/store.ts +++ b/src/features/plugins/store.ts @@ -16,6 +16,7 @@ import { notifications } from "@/workbench/contributions"; import { pluginsBackend, type PluginsBackend } from "./backend"; import { editorContext } from "./editorContext"; import { iconThemeKey, selectedIconTheme, selectIconTheme } from "./iconThemes"; +import { translatorOf, usePluginCatalogs } from "./pluginText"; export const SETTINGS_PAGE_ID = "plugins"; /** `plugins#permissions:`: the Plugins page selects that plugin with its permissions in view. */ @@ -447,13 +448,14 @@ export async function selectPluginTheme(pluginId: string | null, themeId: string } } -/** Title of a plugin command from its UI contribution. */ +/** Title of a plugin command from its UI contribution, in the plugin's translation for the UI language. */ export function commandTitle(pluginId: string, commandId: string): string { const item = usePlugins .getState() .ui.find((c) => c.point === "commands" && c.pluginId === pluginId && c.key === commandId); const title = (item?.decl as { title?: unknown } | undefined)?.title; - return typeof title === "string" ? title : commandId; + if (typeof title !== "string") return commandId; + return translatorOf(usePluginCatalogs.getState().catalogs, pluginId)(title); } /** Runs a plugin command from an action; failures become notifications. */