function LanguageNegotiationBrowser::getLangcode

Same name and namespace in other branches
  1. 9 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::getLangcode()
  2. 8.9.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::getLangcode()
  3. 11.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::getLangcode()

Performs language negotiation.

@todo Determine whether string|false or string|null should be the normalized result across all implementations and update the

Parameters

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

Return value

string|null|false A valid language code if the negotiation was successful and either NULL or FALSE otherwise.

and its comment accordingly.

Overrides LanguageNegotiationMethodInterface::getLangcode

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php, line 49

Class

LanguageNegotiationBrowser
Class for identifying language from the browser Accept-language HTTP header.

Namespace

Drupal\language\Plugin\LanguageNegotiation

Code

public function getLangcode(?Request $request = NULL) {
  $langcode = NULL;
  if ($this->languageManager && $request && $request->server
    ->get('HTTP_ACCEPT_LANGUAGE')) {
    $http_accept_language = $request->server
      ->get('HTTP_ACCEPT_LANGUAGE');
    $langcodes = array_keys($this->languageManager
      ->getLanguages());
    $mappings = $this->config
      ->get('language.mappings')
      ->get('map');
    $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
  }
  // Internal page cache with multiple languages and browser negotiation
  // could lead to wrong cached sites. Therefore disabling the internal page
  // cache.
  // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
  $this->pageCacheKillSwitch
    ->trigger();
  return $langcode;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.