kanopi / kelp

0 stars 0 forks source link

Language helpers #7

Open asipple1 opened 1 year ago

asipple1 commented 1 year ago

We need better/easier ways to detect what the current lang is.

Some code from Mises I've been using in hook_preprocess_node().

  $variables['#cache']['contexts'][] = 'languages:language_interface';

  // TODO: Would like to to get all language variables in a helper function so it can be reused in different hooks and Entities
  // Create a list of all supported languages on Mises.
  $langcodes = \Drupal::languageManager()->getLanguages();
  $langcodesList = array_keys($langcodes);
  $variables['language']['supported_langs'] = $langcodesList;

  // Default language.
  $variables['language']['active'] = 'en';
  if (\Drupal::languageManager()->getCurrentLanguage()->getId()) {
    $variables['language']['active'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
  }

  $translations = $node->getTranslationLanguages();
  // Check to see if node has more than one translation.
  $hasTranslations = count($translations) > 1;
  $variables['language']['has_translations'] = FALSE;
  if ($hasTranslations) {
    $variables['language']['has_translations'] = TRUE;
  }