From bb3ee2e9af27cc580f0ab433eb368b251172489d Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Thu, 10 Feb 2022 10:01:36 +0200
Subject: [PATCH] Issue #3250191 by nod_, Wim Leers, lauriii, rkoller, Joachim
 Namyslo: Translation of toolbar button tooltips not working when text part
 language plugin is enabled

---
 core/modules/ckeditor5/js/ckeditor5.es6.js    | 19 ++++++--------
 core/modules/ckeditor5/js/ckeditor5.js        | 14 +++++-----
 .../src/FunctionalJavascript/LanguageTest.php | 26 +++++++++++++------
 3 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/core/modules/ckeditor5/js/ckeditor5.es6.js b/core/modules/ckeditor5/js/ckeditor5.es6.js
index e41d911c1f01..e6916bc443e4 100644
--- a/core/modules/ckeditor5/js/ckeditor5.es6.js
+++ b/core/modules/ckeditor5/js/ckeditor5.es6.js
@@ -367,25 +367,22 @@
      */
     attach(element, format) {
       const { editorClassic } = CKEditor5;
-      const {
-        toolbar,
-        plugins,
-        config: pluginConfig,
-        language,
-      } = format.editorSettings;
+      const { toolbar, plugins, config, language } = format.editorSettings;
       const extraPlugins = selectPlugins(plugins);
-
-      const config = {
+      const pluginConfig = processConfig(config);
+      const editorConfig = {
         extraPlugins,
         toolbar,
-        language,
-        ...processConfig(pluginConfig),
+        ...pluginConfig,
+        // Language settings have a conflict between the editor localization
+        // settings and the "language" plugin.
+        language: { ...pluginConfig.language, ...language },
       };
       // Set the id immediately so that it is available when onChange is called.
       const id = setElementId(element);
       const { ClassicEditor } = editorClassic;
 
-      ClassicEditor.create(element, config)
+      ClassicEditor.create(element, editorConfig)
         .then((editor) => {
           // Save a reference to the initialized instance.
           Drupal.CKEditor5Instances.set(id, editor);
diff --git a/core/modules/ckeditor5/js/ckeditor5.js b/core/modules/ckeditor5/js/ckeditor5.js
index ef697a63951d..ed479c324d5b 100644
--- a/core/modules/ckeditor5/js/ckeditor5.js
+++ b/core/modules/ckeditor5/js/ckeditor5.js
@@ -195,19 +195,21 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
       var _format$editorSetting = format.editorSettings,
           toolbar = _format$editorSetting.toolbar,
           plugins = _format$editorSetting.plugins,
-          pluginConfig = _format$editorSetting.config,
+          config = _format$editorSetting.config,
           language = _format$editorSetting.language;
       var extraPlugins = selectPlugins(plugins);
+      var pluginConfig = processConfig(config);
 
-      var config = _objectSpread({
+      var editorConfig = _objectSpread(_objectSpread({
         extraPlugins: extraPlugins,
-        toolbar: toolbar,
-        language: language
-      }, processConfig(pluginConfig));
+        toolbar: toolbar
+      }, pluginConfig), {}, {
+        language: _objectSpread(_objectSpread({}, pluginConfig.language), language)
+      });
 
       var id = setElementId(element);
       var ClassicEditor = editorClassic.ClassicEditor;
-      ClassicEditor.create(element, config).then(function (editor) {
+      ClassicEditor.create(element, editorConfig).then(function (editor) {
         Drupal.CKEditor5Instances.set(id, editor);
 
         if (element.hasAttribute('required')) {
diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/LanguageTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/LanguageTest.php
index 88d3d0fee00b..40730f740779 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/LanguageTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/LanguageTest.php
@@ -29,18 +29,20 @@ class LanguageTest extends CKEditor5TestBase {
    *
    * @param string $langcode
    *   The language code.
-   * @param string $blockquote_translation
-   *   The expected translation for blockquote toolbar button.
+   * @param string $toolbar_item_name
+   *   The CKEditor 5 plugin to enable.
+   * @param string $toolbar_item_translation
+   *   The expected translation for CKEditor 5 plugin toolbar button.
    *
    * @dataProvider provider
    */
-  public function test(string $langcode, string $blockquote_translation): void {
+  public function test(string $langcode, string $toolbar_item_name, string $toolbar_item_translation): void {
     $page = $this->getSession()->getPage();
     $assert_session = $this->assertSession();
 
     $this->createNewTextFormat($page, $assert_session);
-    $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-blockQuote'));
-    $this->triggerKeyUp('.ckeditor5-toolbar-item-blockQuote', 'ArrowDown');
+    $this->assertNotEmpty($assert_session->waitForElement('css', ".ckeditor5-toolbar-item-$toolbar_item_name"));
+    $this->triggerKeyUp(".ckeditor5-toolbar-item-$toolbar_item_name", 'ArrowDown');
     $assert_session->assertWaitOnAjaxRequest();
     $this->saveNewTextFormat($page, $assert_session);
 
@@ -50,7 +52,7 @@ public function test(string $langcode, string $blockquote_translation): void {
     $this->drupalGet('node/add');
     $this->assertNotEmpty($assert_session->waitForElement('css', '.ck-editor'));
     // Ensure that blockquote button is translated.
-    $assert_session->elementExists('xpath', "//span[text()='$blockquote_translation']");
+    $assert_session->elementExists('xpath', "//span[text()='$toolbar_item_translation']");
   }
 
   /**
@@ -62,11 +64,19 @@ public function provider(): array {
     return [
       'Language code both in Drupal and CKEditor' => [
         'langcode' => 'th',
-        'blockquote_translation' => 'คำพูดบล็อก',
+        'toolbar_item_name' => 'blockQuote',
+        'toolbar_item_translation' => 'คำพูดบล็อก',
       ],
       'Language code transformed from browser mappings' => [
         'langcode' => 'zh-hans',
-        'blockquote_translation' => '块引用',
+        'toolbar_item_name' => 'blockQuote',
+        'toolbar_item_translation' => '块引用',
+      ],
+      'Language configuration conflict' => [
+        'langcode' => 'fr',
+        'toolbar_item_name' => 'textPartLanguage',
+        // cSpell:disable-next-line
+        'toolbar_item_translation' => 'Choisir la langue',
       ],
     ];
   }
-- 
GitLab