You are here

public function I18n::getTranslation in Bamboo Twig 8.3

Same name and namespace in other branches
  1. 8.5 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n::getTranslation()
  2. 8.4 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n::getTranslation()

Gets a translation of the entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to get the translation.

string|null $langcode: (optional) The language code of the translation to get. NULL (default) means to use the user interface language for the page.

Return value

\Drupal\Core\Entity\EntityInterface|null An entity object when translations exists or NULL.

File

bamboo_twig_i18n/src/TwigExtension/I18n.php, line 102

Class

I18n
Provides some 'Internationalization' Twig Extensions.

Namespace

Drupal\bamboo_twig_i18n\TwigExtension

Code

public function getTranslation(EntityInterface $entity, $langcode = NULL) {

  /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
  $language_manager = $this
    ->getLanguageManager();
  if (!$langcode) {
    $langcode = $this
      ->getCurrentLanguage();
  }

  // Return the translation of this entity or NULL when not translated.
  return $entity
    ->hasTranslation($langcode) ? $entity
    ->getTranslation($langcode) : NULL;
}