Drupal 8: Get The Current Language

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

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

Short, cool and informative. Thank you!

Permalink

Small remark,

To get the currently selected language for content :

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

 

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();

 

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

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.