Como obter o idioma atual no Drupal 8 | Drupal 8

Como obter o idioma atual no Drupal 8

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

Como obter o idioma actualmente seleccionado (página atual ou usuário de idioma) por meio de programação em Drupal8 ?

o Drupal 8

//Para obter o lanuage código:
$idioma = \Drupal::languageManager()->getCurrentLanguage()->getId();

//Para obter o nome do idioma:
$idioma =  \Drupal::languageManager()->getCurrentLanguage()->getName();

 

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

KaedSaugos (not verified)

Fri, 01/24/2020 - 21:27

I tried this, and I am getting lang-english for all languages even after clearing all cache.

function mytheme_preprocess_html(&$variables) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getName();
$variables['language'] = $language;
}

Add new comment

Plain text

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