Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LanguageNegotiationUser.php

function LanguageNegotiationUser::getLangcode

Overrides LanguageNegotiationMethodInterface::getLangcode

File

core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php, line 29

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;
}
RSS feed
Powered by Drupal