3 namespace Drupal\language\Plugin\LanguageNegotiation;
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\language\LanguageNegotiationMethodBase;
7 use Symfony\Component\HttpFoundation\Request;
10 * Class for identifying language from the browser Accept-language HTTP header.
12 * @LanguageNegotiation(
13 * id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID,
15 * name = @Translation("Browser"),
16 * description = @Translation("Language from the browser's language settings."),
17 * config_route_name = "language.negotiation_browser"
20 class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase {
23 * The language negotiation method id.
25 const METHOD_ID = 'language-browser';
30 public function getLangcode(Request $request = NULL) {
33 if ($this->languageManager && $request && $request->server->get('HTTP_ACCEPT_LANGUAGE')) {
34 $http_accept_language = $request->server->get('HTTP_ACCEPT_LANGUAGE');
35 $langcodes = array_keys($this->languageManager->getLanguages());
36 $mappings = $this->config->get('language.mappings')->get('map');
37 $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
38 // Internal page cache with multiple languages and browser negotiation
39 // could lead to wrong cached sites. Therefore disabling the internal
41 // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
42 \Drupal::service('page_cache_kill_switch')->trigger();