Drupal - How do I get the translated title of a node?

You should load the translated entity, and get the title from that.

$translated_entity = $entity->getTranslation('en');
$translated_title = $translated_entity->getTitle();

You can get the current entity language using $entity->get('langcode')->value;, and as @4k4 says, you should check that a translation exists using $entity->hasTranslation($langcode);.


The current language of the entity is not so important, because you can get the same language again. More important is to check if the entity has the translation so that you don't get an error:

 $title_en = $entity->hasTranslation('en') ? $entity->getTranslation('en')->getTitle() : '';
 $title_fr = $entity->hasTranslation('fr') ? $entity->getTranslation('fr')->getTitle() : '';

Tags:

8

I18N L10N