LanguageNegotiationBrowser.php in Drupal 10
File
core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php
View source
<?php
namespace Drupal\language\Plugin\LanguageNegotiation;
use Drupal\Component\Utility\UserAgent;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\language\LanguageNegotiationMethodBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {
const METHOD_ID = 'language-browser';
protected $pageCacheKillSwitch;
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;
}
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);
}
$this->pageCacheKillSwitch
->trigger();
return $langcode;
}
}