Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\Plugin\LanguageNegotiation;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\UserAgent;
|
Chris@0
|
6 use Drupal\language\LanguageNegotiationMethodBase;
|
Chris@0
|
7 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Class for identifying language from the browser Accept-language HTTP header.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @LanguageNegotiation(
|
Chris@0
|
13 * id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID,
|
Chris@0
|
14 * weight = -2,
|
Chris@0
|
15 * name = @Translation("Browser"),
|
Chris@0
|
16 * description = @Translation("Language from the browser's language settings."),
|
Chris@0
|
17 * config_route_name = "language.negotiation_browser"
|
Chris@0
|
18 * )
|
Chris@0
|
19 */
|
Chris@0
|
20 class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The language negotiation method id.
|
Chris@0
|
24 */
|
Chris@0
|
25 const METHOD_ID = 'language-browser';
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * {@inheritdoc}
|
Chris@0
|
29 */
|
Chris@0
|
30 public function getLangcode(Request $request = NULL) {
|
Chris@0
|
31 $langcode = NULL;
|
Chris@0
|
32
|
Chris@0
|
33 if ($this->languageManager && $request && $request->server->get('HTTP_ACCEPT_LANGUAGE')) {
|
Chris@0
|
34 $http_accept_language = $request->server->get('HTTP_ACCEPT_LANGUAGE');
|
Chris@0
|
35 $langcodes = array_keys($this->languageManager->getLanguages());
|
Chris@0
|
36 $mappings = $this->config->get('language.mappings')->get('map');
|
Chris@0
|
37 $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@18
|
40 // Internal page cache with multiple languages and browser negotiation
|
Chris@18
|
41 // could lead to wrong cached sites. Therefore disabling the internal page
|
Chris@18
|
42 // cache.
|
Chris@18
|
43 // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
|
Chris@18
|
44 \Drupal::service('page_cache_kill_switch')->trigger();
|
Chris@18
|
45
|
Chris@0
|
46 return $langcode;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 }
|