comparison core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\language\Plugin\LanguageNegotiation;
4
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\language\LanguageNegotiationMethodBase;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10 * Class for identifying language from the browser Accept-language HTTP header.
11 *
12 * @LanguageNegotiation(
13 * id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID,
14 * weight = -2,
15 * name = @Translation("Browser"),
16 * description = @Translation("Language from the browser's language settings."),
17 * config_route_name = "language.negotiation_browser"
18 * )
19 */
20 class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase {
21
22 /**
23 * The language negotiation method id.
24 */
25 const METHOD_ID = 'language-browser';
26
27 /**
28 * {@inheritdoc}
29 */
30 public function getLangcode(Request $request = NULL) {
31 $langcode = NULL;
32
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
40 // page cache.
41 // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
42 \Drupal::service('page_cache_kill_switch')->trigger();
43 }
44
45 return $langcode;
46 }
47
48 }