Mercurial > hg > isophonics-drupal-site
annotate core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 4c8ae668cc8c |
children | af1871eacc83 |
rev | line source |
---|---|
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 // Internal page cache with multiple languages and browser negotiation |
Chris@0 | 39 // could lead to wrong cached sites. Therefore disabling the internal |
Chris@0 | 40 // page cache. |
Chris@0 | 41 // @todo Solve more elegantly in https://www.drupal.org/node/2430335. |
Chris@0 | 42 \Drupal::service('page_cache_kill_switch')->trigger(); |
Chris@0 | 43 } |
Chris@0 | 44 |
Chris@0 | 45 return $langcode; |
Chris@0 | 46 } |
Chris@0 | 47 |
Chris@0 | 48 } |