You are here

function UserSession::getPreferredLangcode in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getPreferredLangcode()

Returns the preferred language code of the account.

Parameters

bool $fallback_to_default: (optional) Whether the return value will fall back to the site default language if the user has no language preference.

Return value

string The language code that is preferred by the account. If the preferred language is not set or is a language not configured anymore on the site, the site default is returned or an empty string is returned (if $fallback_to_default is FALSE).

Overrides AccountInterface::getPreferredLangcode

File

core/lib/Drupal/Core/Session/UserSession.php, line 136
Contains \Drupal\Core\Session\UserSession.

Class

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

Namespace

Drupal\Core\Session

Code

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() : '';
  }
}