Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

How to get the current language in Drupal 8

By editor , 6 November, 2015
Question

How to get the currently selected language (current page or user language) programmatically in Drupal8 ?

Drupal 8

//To get the lanuage code:
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

//To get the language name:
$language =  \Drupal::languageManager()->getCurrentLanguage()->getName();

 

Drupal 7

global $language;
$language=$language->language;

Tags

  • Drupal 8
  • Code
  • Translation
  • User
  • Add new comment

Comments7

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Alok (not verified)

10 years 4 months ago

To get all enabled language…

To get all enabled language in drupal 8 :

 \Drupal::languageManager()->getLanguages();

 

This may be helpful to any one.

  • Reply

editor

10 years 4 months ago

In reply to To get all enabled language… by Alok (not verified)

Yes

Yes, Thank you :)

  • Reply

Navneet Singh (not verified)

9 years 10 months ago

Replacement of global $language

Drupal 7:

global $language;
// return lang code;
return $language->language;

Drupal 8:

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

  • Reply

Abhinaw (not verified)

8 years 9 months ago

Thanks its work for me :)

Thanks its work for me :)

  • Reply

Anonymous (not verified)

8 years 8 months ago

Thank you! I couldn't figure…

Thank you! I couldn't figure out how to call this.

  • Reply

james (not verified)

8 years 4 months ago

thanks

thank you worked like a charm

  • Reply

William (not verified)

4 years 7 months ago

Current language in current language

And how do I get the current language in the current language, not "French" but "FranƧais"?

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer