3

Fetching node translation

$node = Node::load($nid);
$node->getTitle(); // returns title in en(default language).
$translated_node = \Drupal::service('entity.repository')->getTranslationFromContext($node, 'es');
$translated_node->getTitle(); // Returns translated title in es (spanish).

If we want to fetch the bundle's translation. The above approach doesn't work.

Kindly advise?

$article = $node->bundle();
$article->label(); // returns "Article".
$translated = \Drupal::service('entity.repository')->getTranslationFromContext($article, 'es');
$translated->label(); // Also returns "Article". 

Translation for bundles provided from /admin/structure/types/manage/article/translate/es/edit

3

You need to set the config override language to the desired language:

  $language = $language_manager->getLanguage('es');
  $original_language = $language_manager->getConfigOverrideLanguage();
  $language_manager->setConfigOverrideLanguage($language);

  // Load the node type here.       

  $language_manager->setConfigOverrideLanguage($original_language);

Taken from user_mail()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.