You are here

public function CKEditor::getLangcodes in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/ckeditor/src/Plugin/Editor/CKEditor.php \Drupal\ckeditor\Plugin\Editor\CKEditor::getLangcodes()
  2. 9 core/modules/ckeditor/src/Plugin/Editor/CKEditor.php \Drupal\ckeditor\Plugin\Editor\CKEditor::getLangcodes()

Returns a list of language codes supported by CKEditor.

Return value

array An associative array keyed by language codes.

File

core/modules/ckeditor/src/Plugin/Editor/CKEditor.php, line 372

Class

CKEditor
Defines a CKEditor-based text editor for Drupal.

Namespace

Drupal\ckeditor\Plugin\Editor

Code

public function getLangcodes() {

  // 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('ckeditor.langcodes');
  if (!empty($langcode_cache)) {
    $langcodes = $langcode_cache->data;
  }
  if (empty($langcodes)) {
    $langcodes = [];

    // Collect languages included with CKEditor based on file listing.
    $files = scandir('core/assets/vendor/ckeditor/lang');
    foreach ($files as $file) {
      if ($file[0] !== '.' && preg_match('/\\.js$/', $file)) {
        $langcode = basename($file, '.js');
        $langcodes[$langcode] = $langcode;
      }
    }
    \Drupal::cache()
      ->set('ckeditor.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 = $this->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]);
    }
  }
  return $langcodes;
}