You are here

class LanguageNegotiationBrowser in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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

Expanded class hierarchy of LanguageNegotiationBrowser

1 file declares its use of LanguageNegotiationBrowser
LanguageUILanguageNegotiationTest.php in core/modules/language/src/Tests/LanguageUILanguageNegotiationTest.php
Contains \Drupal\language\Tests\LanguageUILanguageNegotiationTest.

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php, line 25
Contains \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser.

Namespace

Drupal\language\Plugin\LanguageNegotiation
View source
class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase {

  /**
   * The language negotiation method id.
   */
  const METHOD_ID = 'language-browser';

  /**
   * {@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.
      \Drupal::service('page_cache_kill_switch')
        ->trigger();
    }
    return $langcode;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageNegotiationBrowser::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
LanguageNegotiationBrowser::METHOD_ID constant The language negotiation method id.
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager