CKEditor 5 should explicitly set negotiated content language, not just UI language

Created on 30 September 2022, about 2 years ago
Updated 21 August 2024, 3 months ago

Problem/Motivation

While testing a contributed module's CKEditor 5 support, @anairamzap remarked:

The only thing missing to behave exactly as is CK4 is not related with this plugin/module, but with the contentsLangDirection https://docs-old.ckeditor.com/ckeditor_api/symbols/CKEDITOR.config.html#... that we were using to set rtl as the default direction. Just commenting this here in case it helps someone else for the upgrade: https://ckeditor.com/docs/ckeditor5/latest/installation/getting-started/...
UPDATE: to set the default language/direction in CK5 you can use the following hook:

/**
 * Implements hook_editor_js_settings_alter.
 */
function module_name_editor_js_settings_alter(array &$settings) {
  foreach ($settings['editor']['formats'] as $name => $value) {
    $settings['editor']['formats'][$name]['editorSettings']['language'] = [
      'ui' => 'en',
      'content' => 'ar'
    ];
  }
}

That uses the language config for CK5 API and sets a different lang for the UI and the editor content for all editor formats.

The odd thing is that Drupal core never configured CKEditor 4's contentsLangDirection. So AFAICT this can't be a regression. Unless there's some undocumented behavior in CKEditor 4 that automatically applied this; I suspect that it's really just the CKEditor 5 BiDi plugin does not set the content language direction correctly? (That was a contributed module/plugin in CKEditor 4 too: https://www.drupal.org/project/ckeditor_bidi ā†’ ā€” so not a core bug then.)

But, Drupal core natively supports distinguishing UI language (\Drupal\Core\Language\LanguageInterface::TYPE_INTERFACE) and content language (\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT). This cleanly maps onto CKEditor 5's capabilities.

Today, we already make sure the CKEditor 5 UI language matches Drupal's UI language, in \Drupal\ckeditor5\Plugin\Editor\CKEditor5::getJSSettings():

    if ($this->moduleHandler->moduleExists('locale')) {
      $language_interface = $this->languageManager->getCurrentLanguage();
      $settings['language']['ui'] = _ckeditor5_get_langcode_mapping($language_interface->getId());
    }

Steps to reproduce

TBD

Proposed resolution

Add something like

    if ($this->moduleHandler->moduleExists('content_translation')) {
      $content_language = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
      $settings['language']['content'] = _ckeditor5_get_langcode_mapping($content_language->getId());
    }

šŸ¤”šŸ¤“ But what about multiple content languages on a single page? Does CKEditor 5 support specifying a different content language per instance? Does the editor.module API allow for that? That seems like it'd need a follow-up.

Remaining tasks

  1. Failing test with negotiated content language not matching negotiated UI language
  2. Fix
  3. Discuss multiple distinct content languages in use in a single response, regardless of negotiation. Consider opening up follow-up?

User interface changes

TBD

API changes

None.

Data model changes

None.

Release notes snippet

TBD

šŸ“Œ Task
Status

Active

Version

11.0 šŸ”„

Component
CKEditor 5Ā  ā†’

Last updated about 15 hours ago

Created by

šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

  • JavaScript

    Affects the content, performance, or handling of Javascript.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024