1

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

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;
}
|improve this answer|||||
  • Many thanks for your response. That's exactly what I'm looking for but I forgot to mention that I need the Drupal 7 implementation. I will try to find the same solution but in D7. – devcatalarubio Jan 24 '18 at 10:59

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.