Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): name the code block button "Code block"

Elian Doran6663041c0719
diff --git a/packages/ckeditor5/src/i18n.spec.ts b/packages/ckeditor5/src/i18n.spec.ts
index 5afd7ef92f..d55a8fb5d4 100644
--- a/packages/ckeditor5/src/i18n.spec.ts
+++ b/packages/ckeditor5/src/i18n.spec.ts
@@ -122,6 +122,14 @@ describe("getCkLocale", () => {
expect(dictionary).not.toHaveProperty("Admonition");
});
+ it("renames a string to CKEditor's own translation of the replacement", async () => {
+ const { translations } = await getCkLocale("de", { englishMessages: ENGLISH_MESSAGES, translate: (key) => key });
+
+ if (!Array.isArray(translations)) throw new Error("expected an array of translations");
+ const dictionary = (translations[2] as Record<string, { dictionary: Record<string, string> }>).de.dictionary;
+ expect(dictionary["Insert code block"]).toBe("Codeblock");
+ });
+
// End-to-end over a real editor: `AdmonitionUI` localizes with plain `editor.t("Admonition")`,
// knowing nothing about translation keys or the host, and the only thing that makes it Romanian
// is the dictionary configured here.
diff --git a/packages/ckeditor5/src/i18n.ts b/packages/ckeditor5/src/i18n.ts
index aa80e1e918..98a3c9f742 100644
--- a/packages/ckeditor5/src/i18n.ts
+++ b/packages/ckeditor5/src/i18n.ts
@@ -117,6 +117,7 @@ export default async function getCkLocale(
): Promise<Pick<EditorConfig, "language" | "translations">> {
const mapping = LOCALE_MAPPINGS[locale];
const translations: Translations[] = [];
+ let upstreamDictionary: Record<string, string | readonly string[]> | undefined;
// `en`, the `en_rtl` pseudo-locale and `ga` have no CKEditor translation to load; they still get
// our dictionary, since it also carries any rewording of CKEditor's built-in English strings.
@@ -133,6 +134,7 @@ export default async function getCkLocale(
// text editor, and no test.
const [ catalog ] = Object.values((await mapping.coreTranslation()).default);
translations.push({ [mapping.languageCode]: catalog });
+ upstreamDictionary = catalog.dictionary;
}
if (messages) {
@@ -140,7 +142,9 @@ export default async function getCkLocale(
// dictionary still carries the renames of CKEditor's own strings, which apply regardless.
// Keyed by the language CKEditor will resolve messages under, which is the editor's default
// (`en`) whenever we pass no `language` below.
- const dictionary = buildMessageDictionary(messages.englishMessages, messages.translate);
+ const dictionary = buildMessageDictionary(
+ messages.englishMessages, messages.translate, upstreamDictionary
+ );
translations.push({ [mapping?.languageCode ?? "en"]: { dictionary } });
}
diff --git a/packages/ckeditor5/src/messages.spec.ts b/packages/ckeditor5/src/messages.spec.ts
index 169ad545ed..cee50495c5 100644
--- a/packages/ckeditor5/src/messages.spec.ts
+++ b/packages/ckeditor5/src/messages.spec.ts
@@ -45,6 +45,16 @@ describe("buildMessageDictionary", () => {
expect(dictionary.Bookmark).toBe("Anchor");
expect(dictionary["Edit bookmark"]).toBe("Edit anchor");
+ expect(dictionary["Insert code block"]).toBe("Code block");
+ });
+
+ // A replacement CKEditor translates itself, such as "Code block", needs no entry of ours.
+ it("renames to CKEditor's own translation of the replacement when the locale has none of ours", () => {
+ const upstream = { "Code block": "Codeblock", "Bookmark": "Lesezeichen" };
+ const dictionary = buildMessageDictionary({}, (key) => key, upstream);
+
+ expect(dictionary["Insert code block"]).toBe("Codeblock");
+ expect(dictionary.Bookmark).toBe("Anchor");
});
// i18next echoes the key back when an entry is missing. Putting that in the dictionary would
diff --git a/packages/ckeditor5/src/messages.ts b/packages/ckeditor5/src/messages.ts
index 8ad6c645ec..ec341fe735 100644
--- a/packages/ckeditor5/src/messages.ts
+++ b/packages/ckeditor5/src/messages.ts
@@ -43,13 +43,16 @@ export function slugify(message: string): string {
/**
* CKEditor's own strings that Trilium renames, mapping the upstream message id to the English
* wording we show instead. Trilium calls CKEditor's bookmarks "anchors", so every string the
- * upstream `Bookmark` plugin renders is relabelled here.
+ * upstream `Bookmark` plugin renders is relabelled here, and the code block button is named after
+ * the block, as the block quote button is.
*
* This is the one case where a message id and its English text differ, which is why the pairs are
* declared rather than discovered: the dictionary has to be keyed by the *upstream* id for CKEditor
* to find it, while the text shown comes from our catalog entry for the replacement. Both halves of
* a pair therefore live here, and the replacement needs its own English entry like any other
- * message — which is what makes the rename translatable per locale instead of English-only.
+ * message — which is what makes the rename translatable per locale instead of English-only. A
+ * replacement CKEditor translates itself (`Code block`) needs no entry: CKEditor's translation of it
+ * stands in.
*
* A plugin Trilium owns never belongs here: rename its message id at the call site instead.
*/
@@ -66,7 +69,8 @@ export const MESSAGE_OVERRIDES: Record<string, string> = {
"Bookmark toolbar": "Anchor toolbar",
"bookmark widget": "anchor widget",
"No bookmarks available.": "No anchors available.",
- "Scroll to bookmark": "Scroll to anchor"
+ "Scroll to bookmark": "Scroll to anchor",
+ "Insert code block": "Code block"
};
/**
@@ -76,10 +80,13 @@ export const MESSAGE_OVERRIDES: Record<string, string> = {
* key to the English text — which is the message id plugins pass to the
* editor's translation function.
* @param translate resolves a full i18next key to the localized string.
+ * @param upstreamDictionary CKEditor's own dictionary for the locale, which translates a
+ * replacement our catalog has no entry for.
*/
export function buildMessageDictionary(
englishMessages: Record<string, string>,
- translate: (key: string) => string
+ translate: (key: string) => string,
+ upstreamDictionary: Record<string, string | readonly string[]> = {}
): Record<string, string> {
const dictionary: Record<string, string> = {};
@@ -97,7 +104,9 @@ export function buildMessageDictionary(
// Unlike a translation, a rename applies even when the locale has no entry: the English
// replacement is itself the point, so it stands in until a translator supplies the rest.
for (const [ upstreamMessage, replacement ] of Object.entries(MESSAGE_OVERRIDES)) {
- dictionary[upstreamMessage] = dictionary[replacement] ?? replacement;
+ const upstream = upstreamDictionary[replacement];
+ dictionary[upstreamMessage] = dictionary[replacement]
+ ?? (typeof upstream === "string" ? upstream : replacement);
}
return dictionary;