I have been trying to figure out how I can print or display the translated values based on the current language of the page.

I have tried using some of the following options {% trans %} and {% endtrans %} does not work

{# {% trans  %}
<li class="tab">{{ tab_label.entity.field_tab_title_selection.value }}</li>
{% endtrans %} #}

2nd Option using preprocess, the problem with this method in the event I have multiple language setup I have to create multiple if's statement. Lets say I have 10 languages then I have to create 10 conditions

Using Pre-process

function iom_preprocess_page(&$variables) {

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


{# {% 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 %} #}

1 Answer 1


First the {{ trans }} and {{ var|t }} methods only do substitutions from the UI language strings (e.g. specific strings (e.g. "More") that have translations defined (in the UI or code). So, this will not work for 'random' text entered into a field.

That said, it looks like tab_label is an entity reference field. So, tab_label.entity will always get the default language entity.

I would say do the theme_preprocess_xxx to add the current language to what you are rendering (e.g. if it's a block or a node use those, not just page. Preprocess variables may or may not trickle down. I'd only pass on the langcode just to save a bit of memory. So the preprocess function would look like:

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

Next in the twig file, you can use this code to get the translations:

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

Using the set will help keep your HTML easy to validate in the template.

  • I'm getting an error "Call to a member function getId() " $vars['lang_code'] = $language->getId();
    – clestcruz
    Commented Jun 27, 2018 at 2:04
  • 1
    Hmm, didn't see the ->getId() in the initial $language setting. Modifying the example code.
    – CG Monroe
    Commented Jun 27, 2018 at 15:38
  • Already tried that by the way, it still returns the english translation. I tried dumping the lang code . {{ dump(lang_code) }} variable and it returned a NULL
    – clestcruz
    Commented Jun 28, 2018 at 0:34
  • 1
    Where are you trying to do this? E.g. during the node display or a block? Also, checking the basic assumptions just in case, I assume that you put the code above in your theme's .theme file, changed theme to the theme name and replaced XXX with the type of display you're doing, e.g. block or node. Finally, did you clear cache to make sure drupal knows about the new hook. That said, you should check if the preprocess function if firing. E.g. enable dev kint and do ksm($language) in it.
    – CG Monroe
    Commented Jun 29, 2018 at 13:43
  • 1
    D'oh.. the function definition uses $variables and the code sets $vars. Changing answer to use corrected code.
    – CG Monroe
    Commented Jun 29, 2018 at 16:01

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.