class LanguageNegotiationBrowser
Same name in other branches
- 8.9.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser
- 10 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser
- 11.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser
Class for identifying language from the browser Accept-language HTTP header.
Plugin annotation
@LanguageNegotiation(
id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID,
weight = -2,
name = @Translation("Browser"),
description = @Translation("Language from the browser's language settings."),
config_route_name = "language.negotiation_browser"
)
Hierarchy
- class \Drupal\language\LanguageNegotiationMethodBase implements \Drupal\language\LanguageNegotiationMethodInterface
- class \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser extends \Drupal\language\LanguageNegotiationMethodBase implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface
Expanded class hierarchy of LanguageNegotiationBrowser
1 file declares its use of LanguageNegotiationBrowser
- LanguageUILanguageNegotiationTest.php in core/
modules/ language/ tests/ src/ Functional/ LanguageUILanguageNegotiationTest.php
File
-
core/
modules/ language/ src/ Plugin/ LanguageNegotiation/ LanguageNegotiationBrowser.php, line 22
Namespace
Drupal\language\Plugin\LanguageNegotiationView source
class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {
/**
* The language negotiation method id.
*/
const METHOD_ID = 'language-browser';
/**
* The page cache disabling policy.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $pageCacheKillSwitch;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static();
$instance->pageCacheKillSwitch = $container->get('page_cache_kill_switch');
return $instance;
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.