function UserSession::getPreferredLangcode

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getPreferredLangcode()
  2. 10 core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getPreferredLangcode()
  3. 8.9.x core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getPreferredLangcode()

File

core/lib/Drupal/Core/Session/UserSession.php, line 132

Class

UserSession
An implementation of the user account interface for the global user.

Namespace

Drupal\Core\Session

Code

public function getPreferredLangcode($fallback_to_default = TRUE) {
  $language_list = \Drupal::languageManager()->getLanguages();
  if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
    return $language_list[$this->preferred_langcode]
      ->getId();
  }
  else {
    return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()
      ->getId() : '';
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.