You are here

public function LanguageNegotiationUser::getLangcode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser::getLangcode()

Performs language negotiation.

Parameters

\Symfony\Component\HttpFoundation\Request $request: (optional) The current request. Defaults to NULL if it has not been initialized yet.

Return value

string A valid language code or FALSE if the negotiation was unsuccessful.

Overrides LanguageNegotiationMethodInterface::getLangcode

File

core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php, line 33
Contains \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser.

Class

LanguageNegotiationUser
Class for identifying language from the user preferences.

Namespace

Drupal\user\Plugin\LanguageNegotiation

Code

public function getLangcode(Request $request = NULL) {
  $langcode = NULL;

  // User preference (only for authenticated users).
  if ($this->languageManager && $this->currentUser
    ->isAuthenticated()) {
    $preferred_langcode = $this->currentUser
      ->getPreferredLangcode();
    $default_langcode = $this->languageManager
      ->getDefaultLanguage()
      ->getId();
    $languages = $this->languageManager
      ->getLanguages();
    if (!empty($preferred_langcode) && $preferred_langcode != $default_langcode && isset($languages[$preferred_langcode])) {
      $langcode = $preferred_langcode;
    }
  }

  // No language preference from the user.
  return $langcode;
}