InvalidArgumentException: Invalid translation language (fr) specified 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(&amp;$variables) {   

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




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

    &lt;li class="tab"&gt;{{ tab_label.entity.field_tab_title.entity.translation('fr').label }}&lt;/li&gt;

{% else %}

    &lt;li class="tab"&gt;{{ tab_label.entity.field_tab_title.entity.label }}&lt;/li&gt;

{% endif %}




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

&lt;li class="tab"&gt;{{ tab_label.entity.field_tab_title.entity.translation('fr').label }}&lt;/li&gt;
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) %}
  &lt;li class="tab"&gt;{{ tab_label.entity.translation(lang_code).field_tab_title.entity.label }}&lt;/li&gt;
{% else %}
  &lt;li class="tab"&gt;{{ tab_label.entity.field_tab_title.entity.label }}&lt;/li&gt;
{% endif %}