Cum pentru a obține curent limba în Drupal 8 | Drupal 8

Cum pentru a obține curent limba în Drupal 8

Translation

The original text is written in English and This translation is Powered by Yandex.Translate. Please always refer to the english version.
Thank you for your comprehension.

Submitted by editor on Fri, 11/06/2015 - 10:57
Question

Cum să obțineți în prezent limba selectată (pagina curentă sau în limba utilizatorului) programatic în Drupal8 ?

Drupal 8

//Pentru a obține părerea cod:
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

//Pentru a obține numele limba:
$language =  \Drupal::languageManager()->getCurrentLanguage()->getName();

 

Drupal 7

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

Comments

Navneet Singh (not verified)

Sun, 01/15/2017 - 10:18

Drupal 7:

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

Drupal 8:

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

Anonymous (not verified)

Thu, 10/19/2017 - 22:07

For Drupal 8 - getCurrentLanguage() returns the default language:

public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
  return $this->getDefaultLanguage();
}

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Language%21LanguageManager.php/function/LanguageManager%3A%3AgetCurrentLanguage/8.4.x

If Customize Content language detection to differ from Interface text language detection settings is off in Home > Administration > Configuration > Regional and language > Languages, you need to specify a the language detection method I think:

\Drupal::languageManager()->getCurrentLanguage(Drupal\Core\Language\LanguageInterface::TYPE_URL)->getId()

or

\Drupal::languageManager()->getCurrentLanguage(Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId()

Add new comment