You are here

public function LanguageNegotiationUser::getLangcode in Drupal 9

Same name and namespace in other branches
  1. 8 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 28

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(FALSE);
    $languages = $this->languageManager
      ->getLanguages();
    if (!empty($preferred_langcode) && isset($languages[$preferred_langcode])) {
      $langcode = $preferred_langcode;
    }
  }

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