2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-09-24 16:40:55 -06:00

Allow translations of button tooltips

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',
        },
    },
});
This commit is contained in:
Pablo Zmdl 2025-08-26 10:31:13 +02:00
parent 2996b67ec9
commit 0139ac814f

View File

@ -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);