function _ckeditor5_get_langcode_mapping

Same name and namespace in other branches
  1. 10 core/modules/ckeditor5/ckeditor5.module \_ckeditor5_get_langcode_mapping()
  2. 11.x core/modules/ckeditor5/ckeditor5.module \_ckeditor5_get_langcode_mapping()

Returns a list of language codes supported by CKEditor 5.

Parameters

$lang: The Drupal langcode to match.

Return value

array|mixed|string The associated CKEditor 5 langcode.

4 calls to _ckeditor5_get_langcode_mapping()
CKEditor5::getJSSettings in core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
Returns JavaScript settings to be attached.
CKEditor5::getLibraries in core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
Returns libraries to be attached.
ckeditor5_js_alter in core/modules/ckeditor5/ckeditor5.module
Implements hook_js_alter().
ckeditor5_library_info_alter in core/modules/ckeditor5/ckeditor5.module
Implements hook_library_info_alter().

File

core/modules/ckeditor5/ckeditor5.module, line 375

Code

function _ckeditor5_get_langcode_mapping($lang = FALSE) {
    // Cache the file system based language list calculation because this would
    // be expensive to calculate all the time. The cache is cleared on core
    // upgrades which is the only situation the CKEditor file listing should
    // change.
    $langcode_cache = \Drupal::cache()->get('ckeditor5.langcodes');
    if (!empty($langcode_cache)) {
        $langcodes = $langcode_cache->data;
    }
    if (empty($langcodes)) {
        $langcodes = [];
        // Collect languages included with CKEditor 5 based on file listing.
        $files = scandir('core/assets/vendor/ckeditor5/ckeditor5-dll/translations');
        foreach ($files as $file) {
            if (str_ends_with($file, '.js')) {
                $langcode = basename($file, '.js');
                $langcodes[$langcode] = $langcode;
            }
        }
        \Drupal::cache()->set('ckeditor5.langcodes', $langcodes);
    }
    // Get language mapping if available to map to Drupal language codes.
    // This is configurable in the user interface and not expensive to get, so
    // we don't include it in the cached language list.
    $language_mappings = \Drupal::moduleHandler()->moduleExists('language') ? language_get_browser_drupal_langcode_mappings() : [];
    foreach ($langcodes as $langcode) {
        // If this language code is available in a Drupal mapping, use that to
        // compute a possibility for matching from the Drupal langcode to the
        // CKEditor langcode.
        // For instance, CKEditor uses the langcode 'no' for Norwegian, Drupal
        // uses 'nb'. This would then remove the 'no' => 'no' mapping and
        // replace it with 'nb' => 'no'. Now Drupal knows which CKEditor
        // translation to load.
        if (isset($language_mappings[$langcode]) && !isset($langcodes[$language_mappings[$langcode]])) {
            $langcodes[$language_mappings[$langcode]] = $langcode;
            unset($langcodes[$langcode]);
        }
    }
    if ($lang) {
        return $langcodes[$lang] ?? 'en';
    }
    return $langcodes;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.