You are here

public function UserSession::getPreferredLangcode in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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 Returned language code depends upon following:

  • The user preferred language code is returned if set in the account.
  • If the user has no preferred language and $fallback_to_default is TRUE then the site default language code is returned.
  • If the user has no preferred language and $fallback_to_default is FALSE then empty string is returned.

Overrides AccountInterface::getPreferredLangcode

File

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

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