6

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)?

16

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' %} 
  • 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. – Guillaume Bois Sep 14 '17 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 Oct 8 '17 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 Feb 12 '18 at 17:03
  • Works in 8.5, just use {{ language.getId() }} in page.html.twig to print the language id – Hans Rossel May 6 '18 at 18:09
  • {% if language.getId() == 'en' %} should be {% if language == 'en' %} (Drupal 8.7.5) – Lukas Aug 20 '19 at 7:43
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 %}
  • Doesnt look like it. – Anil Maharjan Jan 24 '19 at 16:46
  • From @Yusef's answer following is missing.. function MYTHEME_preprocess_page(&$vars) { $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $vars['language'] = $language; } – Anil Maharjan Jan 24 '19 at 16:56
5

{{ 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.

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

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.

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.