I'm currently trying to translate my twig template to the current language and at this moment I'm getting the following error

InvalidArgumentException: Invalid translation language (fr) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 872 of /Applications/XAMPP/xamppfiles/htdocs/iom/core/lib/Drupal/Core/Entity/ContentEntityBase.php).

Here is a snippet of code that I'm currently working on

// Page-level pre-processing
function iom_preprocess_page(&$variables) {   

    $language = \Drupal::languageManager()->getCurrentLanguage();
    $variables['lang_code'] = $language->getId();

{% if (language.getId() == 'fr')  %}

    <li class="tab">{{ tab_label.entity.field_tab_title.entity.translation('fr').label }}</li>

{% else %}

    <li class="tab">{{ tab_label.entity.field_tab_title.entity.label }}</li>

{% endif %}

Somehow this part right here is what seems to be causing the error even though the language is currently correct

<li class="tab">{{ tab_label.entity.field_tab_title.entity.translation('fr').label }}</li>
  • 1
    should check on lang_code not language.getId() something like {% if tab_label.entity.hastranslation(lang_code) %} {{ tab_label.entity.translation(lang_code).field_tab_title.entity.label }}
    – berramou
    Commented Dec 19, 2018 at 9:35
  • 2
    Why you need to do this, normally the correct language is selected and rendered by the system if it's exist a translation... Commented Dec 19, 2018 at 12:28

1 Answer 1


Should check on lang_code the variable you already passed from preprocess, not language.getId() try something like that:

{% if lang_code == 'fr' and tab_label.entity.hastranslation(lang_code) %}
  <li class="tab">{{ tab_label.entity.translation(lang_code).field_tab_title.entity.label }}</li>
{% else %}
  <li class="tab">{{ tab_label.entity.field_tab_title.entity.label }}</li>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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