Scenario: I have the original string: "Perro", which has its translations: EN => "Dog", FR => "Chien". Mission: In my custom module, I have the string "Chien" and I need to get its English translation, "Dog".

1 Answer 1


This will work for a node that has a field_animal with translation (in Drupal 8). First we found the french translation then we compare with the string, second we fetch the english translation.

$myString = 'Chien';
$translation = \Drupal::service('entity.repository')->getTranslationFromContext($node, 'fr');
$animalFr = $translation->get('field_animal')->value;
if ($myString == $animalFr){
  $translation = \Drupal::service('entity.repository')->getTranslationFromContext($node, 'en');
  $animalEn = $translation->get('field_animal')->value;
  • This answer is valid if the string is the only content of an entity field. It doesn't work when the field contains Chien fidèle, for example, or when the string is not contained in an entity field. The question doesn't say where in the custom module the string is stored. (This is not the answer's fault.)
    – apaderno
    May 9, 2020 at 10:29

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.