12

In Drupal 7 the current language is a global variable we can access with

global $language;
$langcode = $language->language; 

I want to know how can we access the current language in a Twig file in Drupal 8 (in page--front.html.twig for example)?

5 Answers 5

29

I found my answer and post it here so maybe it helps others later.

First, in MYTHEME.theme:

In Drupal 8.2 language got added to page the variables in template_preprocess_page. But in my current version 8.1 it didn't exist so I added it with:

function MYTHEME_preprocess_page(&$vars) {

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

Then in my Twig file I use {{ language }}. Also I can get the language name with {{ language.getName() }}.

Update

After Drupal 8.2 a global variable language exists and now there's no more need to add it first.

{% if language.getId() == 'en' %}
  Do something
{% else %}
  Do something else
{% endif %}

Update Thanks @Lucas for his comment

on Drupal > 8.7.5

{% if language.getId() == 'en' %} should be {% if language == 'en' %} 
11
  • Does not work. Drupal 8.3.7 here and {{ dump(language) }} gives me null. Note that I am in a twig in a module and not a theme. Commented Sep 14, 2017 at 15:44
  • @GuillaumeBois I'm having the same issue, language variable is accessible inside the theme templates but not in my custom module template. Could you find anything?
    – madpoet
    Commented Oct 8, 2017 at 11:18
  • This simply does not work when trying to access the variable in the TWIG template. Works fine in the preprocess though.
    – John Cogan
    Commented Feb 12, 2018 at 17:03
  • Works in 8.5, just use {{ language.getId() }} in page.html.twig to print the language id Commented May 6, 2018 at 18:09
  • 1
    This doesn't depend on Drupal version 8.7.5, the confusing part is that this answers overwrites the language object set in core with a string value. If you want to pass the id as string to the template, then use a different variable name, so that existing code still works.
    – 4uk4
    Commented Dec 14, 2019 at 8:59
12

On Drupal version 8.2.3 the language variable is already available in Twig templates. A usage example is this:

{% if language.getId() == 'en' %}
  Do something
{% else %}
  Do something else
{% endif %}
2
  • Doesnt look like it. Commented Jan 24, 2019 at 16:46
  • From @Yusef's answer following is missing.. function MYTHEME_preprocess_page(&$vars) { $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $vars['language'] = $language; } Commented Jan 24, 2019 at 16:56
10

{{ language }} works but only in page context.

The example of this question(page--front.html.twig) happens to be a working one.

I tried in 8.7.6 with Xdebug. When called from somewhere other than page.html.twig it will returns a null instead of {Drupal\Core\Language\Language}

I think that's why some people report it working but others not. You might use it under node.html.twig or something. In this case you still need to provide that under preprocess() function of your theme.

1
  • Hi, welcome to drupal . have to seen function MYTHEME_preprocess_page(&$vars) { $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $vars['language'] = $language; } on my answer?
    – Yuseferi
    Commented Aug 26, 2019 at 7:41
6

In page.html.twig and Drupal 8.7.6, I had to use:

{% if language.id == 'en' %} or {{ language.id == 'en' ? 'active' : 'not-active' }}

Other suggestions did not work.

0

I was trying to display the language to the user in the user native language, so I was trying to display "Français" instead of "French" and this was the answer if it might help someone:

Inside a preprocess function:

    // Get the current content language in native form.
    $language_id = \Drupal::languageManager->getCurrentLanguage('language_content')->getId();
    $native_language = \Drupal::languageManager()->getNativeLanguages()[$language_id];
    $variables['current_language_name'] = $native_language->getName();
  

Inside a twig template:

{{ current_language_name }}

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