Get user current language in Drupal 8

While writing custom code in a Drupal multilingual project you should always remember to use the user's current language to show content. And this can be achieved with the methods provided by LanguageManager class.

Here are the quick code snippets of how we can get the current user language code, name, and enabled languages in Drupal 8.

Get current language code:

Returns user current language code on the site.

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

Get current language name:

Returns user current language name on the site.

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

Get all languages in Drupal site:

Provides a list of languages set up on the site.

$site_langs = \Drupal::languageManager()->getLanguages();