From 0139ac814f3f79a89a2163522a20f09b4e178c36 Mon Sep 17 00:00:00 2001 From: Pablo Zmdl Date: Tue, 26 Aug 2025 10:31:13 +0200 Subject: [PATCH] Allow translations of button tooltips MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a non-breaking change that allows to pass in `translations` and a wanted `language` as parameters to `EasyMDE()` in order to translate the toolbar buttons' titles that show up as tooltips. The code uses a simple `replace()` call, so the translation keys must exactly match the default english strings (including letter case, white spaces, etc.) to have the translations be applied. Example: const easymde = new EasyMDE({ language: 'de', translations: { de: { 'Create Link': 'Link einfügen', }, }, }); --- src/js/easymde.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/js/easymde.js b/src/js/easymde.js index a4c3059..777534f 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -209,7 +209,7 @@ function createToolbarButton(options, enableActions, enableTooltips, shortcuts, } if (options.title && enableTooltips) { - el.title = createTooltip(options.title, options.action, shortcuts); + el.title = createTooltip(options.title, options.action, shortcuts, parent.options.translations, parent.options.language); if (isMac) { el.title = el.title.replace('Ctrl', '⌘'); @@ -289,9 +289,16 @@ function createSep() { return el; } -function createTooltip(title, action, shortcuts) { +var translate = function (key, translations, language) { + if (translations && language && translations[language] && translations[language][key]) { + return translations[language][key]; + } + return key; +}; + +function createTooltip(title, action, shortcuts, translations, language) { var actionName; - var tooltip = title; + var tooltip = translate(title, translations, language); if (action) { actionName = getBindingName(action);