By Yusef Mohamadi


2016-06-16 07:26:34 8 Comments

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

3 comments

@Vagelis Prokopiou 2016-11-27 13:57:03

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

@Anil Maharjan 2019-01-24 16:46:43

Doesnt look like it.

@Anil Maharjan 2019-01-24 16:56:08

From @Yusef's answer following is missing.. function MYTHEME_preprocess_page(&$vars) { $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $vars['language'] = $language; }

@Yusef Mohamadi 2016-06-16 08:12:54

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

@Guillaume Bois 2017-09-14 15:44:49

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.

@madpoet 2017-10-08 11:18:31

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

@John Cogan 2018-02-12 17:03:34

This simply does not work when trying to access the variable in the TWIG template. Works fine in the preprocess though.

@Hans Rossel 2018-05-06 18:09:22

Works in 8.5, just use {{ language.getId() }} in page.html.twig to print the language id

@Nandhana 2016-06-16 07:31:42

<?php if($language->language == 'regional') { $lang = "tamil"; } else { $lang = "english"; } ?>

@Yusef Mohamadi 2016-06-16 07:33:07

in drupal 8 twig file !!!

@ARUN 2016-06-16 08:03:27

@zhilevan, did u try to print $language variable? Is that empty?

@Yusef Mohamadi 2016-06-16 08:09:00

@ARUN I ask in drupal 8, in drupal 8 variables with $ not exists in twig file.

@ARUN 2016-06-16 08:14:29

@zhilevan, but template_preprocess_page function already passing language in to the template.

@Yusef Mohamadi 2016-06-16 08:22:53

@ARUN it added in 8.2 but I am using 8.1 :D

@ARUN 2016-06-16 08:24:52

@zhilevan, anyway that solve your issue. :)

@Yusef Mohamadi 2016-06-16 08:27:24

@ARUN tnx for your attention ,drupal 8.2 not released yet and it is under development, I add it manually in hook_preprocess_page

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to get the language code when a language name is given?

  • 2018-12-12 10:45:02
  • ktrev
  • 152 View
  • 3 Score
  • 3 Answer
  • Tags:   8 i18n-l10n

1 Answered Questions

[SOLVED] Display block content in current language

1 Answered Questions

How do I get the current language in custom user edit Twig file?

  • 2018-08-03 09:10:12
  • salu
  • 395 View
  • 0 Score
  • 1 Answer
  • Tags:   8 users

2 Answered Questions

[SOLVED] Language name of translated node

  • 2018-04-06 16:25:39
  • Bruno
  • 238 View
  • 2 Score
  • 2 Answer
  • Tags:   8 theming

2 Answered Questions

[SOLVED] Get current language of Drupal 8 in javascript?

  • 2017-11-21 15:30:33
  • Bassem Mohamed
  • 2303 View
  • 3 Score
  • 2 Answer
  • Tags:   8 javascript

1 Answered Questions

[SOLVED] t() function - get specific language

  • 2016-12-19 17:54:00
  • mogio
  • 121 View
  • 2 Score
  • 1 Answer
  • Tags:   8 i18n-l10n

1 Answered Questions

[SOLVED] Get current language in a template file

  • 2016-11-04 18:25:24
  • Stefan Korn
  • 793 View
  • 1 Score
  • 1 Answer
  • Tags:   theming 8 i18n-l10n

1 Answered Questions

Accessing translation and language URL from hook_preprocess_menu()

1 Answered Questions

[SOLVED] Page template suggestion per language?

  • 2011-11-06 17:07:09
  • Ivailo Milenkov
  • 1381 View
  • 2 Score
  • 1 Answer
  • Tags:   theming i18n-l10n

1 Answered Questions

[SOLVED] How to tell if the current language is written RTL or LTR

  • 2013-05-13 20:35:50
  • Nabil Kadimi
  • 1113 View
  • 1 Score
  • 1 Answer
  • Tags:   theming i18n-l10n

Sponsored Content