From 5545026999bc082e3568efda3337a97ab3a250d9 Mon Sep 17 00:00:00 2001 From: indigoxela Date: Thu, 11 Dec 2025 11:40:12 +0100 Subject: [PATCH 1/2] Issue #188: Improved editor UI langcode handling --- js/tinymce-integration.js | 6 ++-- tinymce.module | 62 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/js/tinymce-integration.js b/js/tinymce-integration.js index 24f26b2..f6de65a 100644 --- a/js/tinymce-integration.js +++ b/js/tinymce-integration.js @@ -54,8 +54,8 @@ } } - // Content language defaults to interface language. - let contentLang = options.language; + // Content language defaults to current page language. + let contentLang = format.editorSettings.contentLanguageDefault; // If this element's form has a language select list, toggle content lang // based on that value. if (element.form.querySelector('#edit-langcode') !== null) { @@ -66,7 +66,7 @@ languageToggle.addEventListener('change', function (ev) { let langcode = ev.target.value; if (langcode === 'und') { - langcode = options.language; + langcode = format.editorSettings.contentLanguageDefault; } let event = new CustomEvent('contentLangSwitch', { detail: langcode }); window.dispatchEvent(event); diff --git a/tinymce.module b/tinymce.module index 6653efb..ba3016f 100644 --- a/tinymce.module +++ b/tinymce.module @@ -247,6 +247,11 @@ function _tinymce_js_settings($format, array $existing_settings) { $options['tiny_options']['paste_data_images'] = FALSE; } + // Set content language independently from editor UI language, as the editor + // has only a limited number of translations. + global $language; + $options['contentLanguageDefault'] = $language->langcode; + backdrop_alter('tinymce_options', $options, $format); return $options; } @@ -370,11 +375,10 @@ function _tinymce_get_unregister_formats($taglist) { * @return array */ function _tinymce_merge_default_settings(array $settings) { - global $language; $tiny_defaults = array( 'branding' => FALSE, 'promotion' => FALSE, - 'language' => $language->langcode, + 'language' => _tinymce_interface_language_option(), 'browser_spellcheck' => TRUE, 'entity_encoding' => 'raw', 'license_key' => 'gpl', @@ -534,3 +538,57 @@ function tinymce_tinymce_external_plugins($format) { return $plugins; } + +/** + * + */ +function _tinymce_interface_language_option() { + $langcode = &backdrop_static(__FUNCTION__); + + if (!isset($langcode)) { + global $language; + $langcode = $language->langcode; + + if ($langcode == 'en') { + // Return early, as "en" is the default editor UI language. + return $langcode; + } + + // Note: this mapping needs to get checked with every language pack update. + // @see standard_language_list() + $mapping = array( + 'bg' => 'bg_BG', + 'bn' => 'bn_BD', + 'en-gb' => 'en', + 'fr' => 'fr_FR', + 'he' => 'he_IL', + 'hu' => 'hu_HU', + 'is' => 'is_IS', + 'ka' => 'ka_GE', + 'ko' => 'ko_KR', + 'nb' => 'nb_NO', + 'pt' => 'pt_PT', + 'pt-br' => 'pt_BR', + 'pt-pt' => 'pt_PT', + 'sl' => 'sl_SI', + 'sv' => 'sv_SE', + 'ta-lk' => 'ta', + 'th' => 'th_TH', + 'zh-hans' => 'zh_CN', + 'zh-hant' => 'zh_TW', + ); + if (array_key_exists($langcode, $mapping)) { + $langcode = $mapping[$langcode]; + } + else { + // Otherwise check if such a file exists, fall back to English if not. + $path = backdrop_get_path('module', 'tinymce'); + $file_exists = file_exists("$path/libraries/tinymce/js/tinymce/langs/$langcode.js"); + if (!$file_exists) { + $langcode = 'en'; + } + } + } + + return $langcode; +} From f4489290488c3c2abf96861a52a050de19990f7f Mon Sep 17 00:00:00 2001 From: indigoxela Date: Thu, 11 Dec 2025 11:44:26 +0100 Subject: [PATCH 2/2] Add missing doc block info --- tinymce.module | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tinymce.module b/tinymce.module index ba3016f..f25935a 100644 --- a/tinymce.module +++ b/tinymce.module @@ -540,7 +540,10 @@ function tinymce_tinymce_external_plugins($format) { } /** + * Helper function to determine the editor's UI language. * + * @return string + * Language code to use for editor option. */ function _tinymce_interface_language_option() { $langcode = &backdrop_static(__FUNCTION__);