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


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

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

  // Load the node type here.       


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.