Drupal 8: Get The Current Language

26th April 2016 - 1 minute read time

To get the currently selected language on a Drupal 8 site you need to run the following.

$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

This will return the 2 character ISO language code for the language. So for English this would be "en".

To get the language name instead of the language code use this.

$language =  \Drupal::languageManager()->getCurrentLanguage()->getName();

Comments

Permalink
It,s really helpful. Thank you so much.

Sarah Jay (Tue, 08/30/2016 - 20:46)

Permalink

Short, cool and informative. Thank you!

Josip Rajkovic (Wed, 10/17/2018 - 15:04)

Permalink

Small remark,

To get the currently selected language for content :

$language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();

 

tty11 (Thu, 02/07/2019 - 09:17)

Permalink

Thank you Philip. I did not have the interface translation enabled so your snipped was always returning the default language. The snipped posted by "tty11" worked. Thanks tty11 :), however I will make a small change to it. Drupal couldn't find "LanguageInterface" in my theme's preprocess function, I had to provide namespace too as follows

$language = \Drupal::languageManager()->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId();

 

Ejay Rus (Fri, 12/11/2020 - 09:57)

Permalink

Thanks Ejay. I wrote this post during my first few weeks of Drupal 8 development. I've since learned a lot about how the language manager works so there are probably a few contextual issues with my original snippet :)

Add new comment

The content of this field is kept private and will not be shown publicly.