diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 30a63e5..5a46c8c 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1,6 +1,6 @@ server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); + $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); $form['langcode'] = array( '#type' => 'select', '#title' => t('Choose language'), diff --git a/core/lib/Drupal/Component/Utility/Browser.php b/core/lib/Drupal/Component/Utility/UserAgent.php similarity index 71% rename from core/lib/Drupal/Component/Utility/Browser.php rename to core/lib/Drupal/Component/Utility/UserAgent.php index 7977b39..361b057 100644 --- a/core/lib/Drupal/Component/Utility/Browser.php +++ b/core/lib/Drupal/Component/Utility/UserAgent.php @@ -2,58 +2,58 @@ /** * @file - * Contains \Drupal\Component\Utility\Browser. + * Contains \Drupal\Component\Utility\UserAgent. */ namespace Drupal\Component\Utility; /** - * Provides Browser-related utility functions. + * Provides user agent related utility functions. */ -class Browser { +class UserAgent { /** - * Identifies browser language from the Accept-language HTTP header. + * Identifies user agent language from the Accept-language HTTP header. * * The algorithm works as follows: - * - map browser language codes to available language codes. - * - order all browser language codes by qvalue from high to low. - * - add generic browser language codes if they aren't already specified + * - map user agent language codes to available language codes. + * - order all user agent language codes by qvalue from high to low. + * - add generic user agent language codes if they aren't already specified * but with a slightly lower qvalue. * - find the most specific available language code with the highest qvalue. * - if 2 or more languages are having the same qvalue, respect the order of * them inside the $languages array. * - * We perform browser accept-language parsing only if page cache is disabled, - * otherwise we would cache a user-specific preference. + * We perform user agent accept-language parsing only if page cache is + * disabled, otherwise we would cache a user-specific preference. * * @param string $http_accept_language * The value of the "Accept-Language" HTTP header. * @param array $langcodes * An array of available language codes to pick from. * @param array $mappings - * (optional) Custom mappings to support browsers that are sending non + * (optional) Custom mappings to support user agents that are sending non * standard language codes. No mapping is assumed by default. * * @return string * The selected language code or FALSE if no valid language can be * identified. */ - public static function getLangcode($http_accept_language, $langcodes, $mappings = array()) { + public static function getBestMatchingLangcode($http_accept_language, $langcodes, $mappings = array()) { // The Accept-Language header contains information about the language - // preferences configured in the user's browser / operating system. RFC 2616 - // (section 14.4) defines the Accept-Language header as follows: + // preferences configured in the user's user agent / operating system. + // RFC 2616 (section 14.4) defines the Accept-Language header as follows: // Accept-Language = "Accept-Language" ":" // 1#( language-range [ ";" "q" "=" qvalue ] ) // language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" - $browser_langcodes = array(); + $ua_langcodes = array(); if (preg_match_all('@(?<=[, ]|^)([a-zA-Z-]+|\*)(?:;q=([0-9.]+))?(?:$|\s*,\s*)@', trim($http_accept_language), $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { if ($mappings) { $langcode = strtolower($match[1]); - foreach ($mappings as $browser_langcode => $standard_langcode) { - if ($langcode == $browser_langcode) { + foreach ($mappings as $ua_langcode => $standard_langcode) { + if ($langcode == $ua_langcode) { $match[1] = $standard_langcode; } } @@ -67,9 +67,9 @@ public static function getLangcode($http_accept_language, $langcodes, $mappings // Take the highest qvalue for this langcode. Although the request // supposedly contains unique langcodes, our mapping possibly resolves // to the same langcode for different qvalues. Keep the highest. - $browser_langcodes[$langcode] = max( + $ua_langcodes[$langcode] = max( (int) ($qvalue * 1000), - (isset($browser_langcodes[$langcode]) ? $browser_langcodes[$langcode] : 0) + (isset($ua_langcodes[$langcode]) ? $ua_langcodes[$langcode] : 0) ); } } @@ -82,8 +82,8 @@ public static function getLangcode($http_accept_language, $langcodes, $mappings // possible. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 and // http://blogs.msdn.com/b/ie/archive/2006/10/17/accept-language-header-for-internet-explorer-7.aspx - asort($browser_langcodes); - foreach ($browser_langcodes as $langcode => $qvalue) { + asort($ua_langcodes); + foreach ($ua_langcodes as $langcode => $qvalue) { // For Chinese languages the generic tag is either zh-hans or zh-hant, so // we need to handle this separately, we can not split $langcode on the // first occurrence of '-' otherwise we get a non-existing language zh. @@ -96,11 +96,11 @@ public static function getLangcode($http_accept_language, $langcodes, $mappings else { $generic_tag = strtok($langcode, '-'); } - if (!empty($generic_tag) && !isset($browser_langcodes[$generic_tag])) { + if (!empty($generic_tag) && !isset($ua_langcodes[$generic_tag])) { // Add the generic langcode, but make sure it has a lower qvalue as the // more specific one, so the more specific one gets selected if it's - // defined by both the browser and us. - $browser_langcodes[$generic_tag] = $qvalue - 0.1; + // defined by both the user agent and us. + $ua_langcodes[$generic_tag] = $qvalue - 0.1; } } @@ -115,14 +115,15 @@ public static function getLangcode($http_accept_language, $langcodes, $mappings // If nothing matches below, the default qvalue is the one of the wildcard // language, if set, or is 0 (which will never match). - $qvalue = isset($browser_langcodes['*']) ? $browser_langcodes['*'] : 0; + $qvalue = isset($ua_langcodes['*']) ? $ua_langcodes['*'] : 0; - // Find the longest possible prefix of the browser-supplied language ('the - // language-range') that matches this site language ('the language tag'). + // Find the longest possible prefix of the user agent supplied language + // ('the language-range') that matches this site language ('the language + // tag'). $prefix = $langcode; do { - if (isset($browser_langcodes[$prefix])) { - $qvalue = $browser_langcodes[$prefix]; + if (isset($ua_langcodes[$prefix])) { + $qvalue = $ua_langcodes[$prefix]; break; } } diff --git a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php index 3f4d09a..49c7ea4 100644 --- a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php +++ b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php @@ -172,14 +172,6 @@ public function getDefinedLanguageTypesInfo() { } /** - * {@inheritdoc} - */ - function disableLanguageTypes(array $types) { - $this->languageTypes['configurable'] = array_diff($this->getLanguageTypes(), $types); - $this->saveLanguageTypesConfiguration($this->languageTypes); - } - - /** * Stores language types configuration. */ public function saveLanguageTypesConfiguration(array $config) { @@ -254,7 +246,8 @@ public function getNegotiator() { */ public function setNegotiator(LanguageNegotiatorInterface $negotiator) { $this->negotiator = $negotiator; - $this->reset(); + $this->initialized = FALSE; + $this->negotiatedLanguages = array(); } /** diff --git a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManagerInterface.php b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManagerInterface.php index a0c8206..c9a13d6 100644 --- a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManagerInterface.php +++ b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManagerInterface.php @@ -64,14 +64,6 @@ public function getDefinedLanguageTypes(); public function getDefinedLanguageTypesInfo(); /** - * Disables the given language types. - * - * @param array $types - * An array of language type names. - */ - function disableLanguageTypes(array $types); - - /** * Stores language types configuration. * * @param array diff --git a/core/modules/language/lib/Drupal/language/EventSubscriber/LanguageRequestSubscriber.php b/core/modules/language/lib/Drupal/language/EventSubscriber/LanguageRequestSubscriber.php index 2976620..c9551f4 100644 --- a/core/modules/language/lib/Drupal/language/EventSubscriber/LanguageRequestSubscriber.php +++ b/core/modules/language/lib/Drupal/language/EventSubscriber/LanguageRequestSubscriber.php @@ -78,12 +78,13 @@ public function __construct(ConfigurableLanguageManagerInterface $language_manag public function onKernelRequestLanguage(GetResponseEvent $event) { if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); - $this->negotiator->setContext($this->currentUser, $request); - $this->languageManager->setNegotiator($this->negotiator); + $this->negotiator->setCurrentUser($this->currentUser); + $this->negotiator->setRequest($request); if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { + $this->languageManager->setNegotiator($this->negotiator); + $this->languageManager->setRequest($request); $this->languageManager->initConfigOverrides(); } - $this->languageManager->setRequest($request); // After the language manager has initialized, set the default langcode // for the string translations. $langcode = $this->languageManager->getCurrentLanguage()->id; diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php index 6e0e266..c5b2a66 100644 --- a/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php @@ -269,10 +269,10 @@ protected function configureFormTable(array &$form, $type) { $table_form['description'][$method_id] = array('#markup' => Xss::filterAdmin($method['description'])); $config_op = array(); - if (isset($method['config'])) { + if (isset($method['config_path'])) { $config_op['configure'] = array( 'title' => $this->t('Configure'), - 'href' => $method['config'], + 'href' => $method['config_path'], ); // If there is at least one operation enabled show the operation // column. diff --git a/core/modules/language/lib/Drupal/language/HttpKernel/PathProcessorLanguage.php b/core/modules/language/lib/Drupal/language/HttpKernel/PathProcessorLanguage.php index 0a9137a..71a5cfe 100644 --- a/core/modules/language/lib/Drupal/language/HttpKernel/PathProcessorLanguage.php +++ b/core/modules/language/lib/Drupal/language/HttpKernel/PathProcessorLanguage.php @@ -51,13 +51,6 @@ class PathProcessorLanguage implements InboundPathProcessorInterface, OutboundPa protected $negotiator; /** - * The current active user. - * - * @return \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** * Local cache for language path processors. * * @var array @@ -90,7 +83,7 @@ public function __construct(ConfigFactory $config, Settings $settings, Configura $this->mixedModeSessions = $settings->get('mixed_mode_sessions', FALSE); $this->languageManager = $language_manager; $this->negotiator = $negotiator; - $this->currentUser = $current_user; + $this->negotiator->setCurrentUser($current_user); } /** @@ -117,7 +110,7 @@ public function processOutbound($path, &$options = array(), Request $request = N $this->multilingual = $this->languageManager->isMultilingual(); } if ($this->multilingual) { - $this->negotiator->setContext($this->currentUser, $request); + $this->negotiator->setRequest($request); $scope = 'outbound'; if (!isset($this->processors[$scope])) { $this->initProcessors($scope); diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodBase.php b/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodBase.php index 2f2b5cf..5dc497b 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodBase.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodBase.php @@ -8,6 +8,7 @@ namespace Drupal\language; use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; /** @@ -57,4 +58,12 @@ public function setCurrentUser(AccountInterface $current_user) { $this->currentUser = $current_user; } + /** + * {@inheritdoc} + */ + public function persist(Language $language) { + // Remember the method ID used to detect the language. + $language->method_id = static::METHOD_ID; + } + } diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodInterface.php b/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodInterface.php index cd19bb1..eb23977 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodInterface.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiationMethodInterface.php @@ -8,6 +8,7 @@ namespace Drupal\language; use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; use Symfony\Component\HttpFoundation\Request; @@ -53,4 +54,11 @@ public function setCurrentUser(AccountInterface $current_user); */ public function getLangcode(Request $request = NULL); + /** + * Notifies the plugin that the language code it returned has been accepted. + * + * @param string $langcode + */ + public function persist(Language $language); + } diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiator.php b/core/modules/language/lib/Drupal/language/LanguageNegotiator.php index 855c8aa..f2a74f6 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiator.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiator.php @@ -116,8 +116,15 @@ public function reset() { /** * {@inheritdoc} */ - public function setContext(AccountInterface $current_user, Request $request) { + public function setCurrentUser(AccountInterface $current_user) { $this->currentUser = $current_user; + $this->reset(); + } + + /** + * {@inheritdoc} + */ + public function setRequest(Request $request) { $this->request = $request; $this->reset(); } @@ -126,7 +133,7 @@ public function setContext(AccountInterface $current_user, Request $request) { * {@inheritdoc} */ public function initializeType($type) { - $language = FALSE; + $language = NULL; if ($this->currentUser && $this->request) { // Execute the language negotiation methods in the order they were set up @@ -140,11 +147,10 @@ public function initializeType($type) { // the language negotiation method cache from being unintentionally // altered. The same methods might be used with different language types // based on configuration. - $language = !empty($this->negotiatedLanguages[$method_id]) ? clone($this->negotiatedLanguages[$method_id]) : FALSE; + $language = !empty($this->negotiatedLanguages[$method_id]) ? clone($this->negotiatedLanguages[$method_id]) : NULL; if ($language) { - // Remember the method ID used to detect the language. - $language->method_id = $method_id; + $this->getNegotiationMethodInstance($method_id)->persist($language); break; } } @@ -178,11 +184,11 @@ protected function getConfiguration($type) { * The string identifier of the language negotiation method to use to detect * language. * - * @return \Drupal\Core\Language\Language|FALSE + * @return \Drupal\Core\Language\Language|NULL * Negotiated language object for given type and method, FALSE otherwise. */ protected function negotiateLanguage($type, $method_id) { - $langcode = FALSE; + $langcode = NULL; $method = $this->negotiatorManager->getDefinition($method_id); if (!isset($method['types']) || in_array($type, $method['types'])) { @@ -203,7 +209,7 @@ protected function negotiateLanguage($type, $method_id) { } $languages = $this->languageManager->getLanguages(); - return isset($languages[$langcode]) ? $languages[$langcode] : FALSE; + return isset($languages[$langcode]) ? $languages[$langcode] : NULL; } /** diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php b/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php index f16ed54..0654fde 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php @@ -116,14 +116,20 @@ public function reset(); /** - * Sets the contextual data and resets all language types. + * Sets the current active user and resets all language types. * * @param \Drupal\Core\Session\AccountInterface $current_user * The current active user. + */ + public function setCurrentUser(AccountInterface $current_user); + + /** + * Sets the active request and resets all language types. + * * @param \Symfony\Component\HttpFoundation\Request $request * The HttpRequest object representing the current request. */ - public function setContext(AccountInterface $current_user, Request $request); + public function setRequest(Request $request); /** * Initializes the specified language type. diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php index 85d49c5..0ce2c86 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php @@ -7,7 +7,7 @@ namespace Drupal\language\Plugin\LanguageNegotiation; -use Drupal\Component\Utility\Browser; +use Drupal\Component\Utility\UserAgent; use Drupal\language\LanguageNegotiationMethodBase; use Symfony\Component\HttpFoundation\Request; @@ -20,7 +20,7 @@ * cache = 0, * name = @Translation("Browser"), * description = @Translation("Language from the browser's language settings."), - * config = "admin/config/regional/language/detection/browser" + * config_path = "admin/config/regional/language/detection/browser" * ) */ class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase { @@ -34,13 +34,13 @@ class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $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(); - $langcode = Browser::getLangcode($http_accept_language, $langcodes, $mappings); + $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings); } return $langcode; diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSelected.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSelected.php index fba2a33..bdbedc5 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSelected.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSelected.php @@ -18,7 +18,7 @@ * weight = 12, * name = @Translation("Selected language"), * description = @Translation("Language based on a selected language."), - * config = "admin/config/regional/language/detection/selected" + * config_path = "admin/config/regional/language/detection/selected" * ) */ class LanguageNegotiationSelected extends LanguageNegotiationMethodBase { @@ -32,14 +32,11 @@ class LanguageNegotiationSelected extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $langcode = NULL; if ($this->languageManager) { $languages = $this->languageManager->getLanguages(); $langcode = $this->config->get('language.negotiation')->get('selected_langcode'); - if (!isset($languages[$langcode])) { - $langcode = $this->languageManager->getDefaultLanguage()->id; - } } return $langcode; diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSession.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSession.php index dd7e5b4..e56278d 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSession.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationSession.php @@ -7,6 +7,7 @@ namespace Drupal\language\Plugin\LanguageNegotiation; +use Drupal\Core\Language\Language; use Drupal\Core\PathProcessor\OutboundPathProcessorInterface; use Drupal\language\LanguageNegotiationMethodBase; use Drupal\language\LanguageSwitcherInterface; @@ -20,7 +21,7 @@ * weight = -6, * name = @Translation("Session"), * description = @Translation("Language from a request/session parameter."), - * config = "admin/config/regional/language/detection/session" + * config_path = "admin/config/regional/language/detection/session" * ) */ class LanguageNegotiationSession extends LanguageNegotiationMethodBase implements OutboundPathProcessorInterface, LanguageSwitcherInterface { @@ -57,23 +58,29 @@ class LanguageNegotiationSession extends LanguageNegotiationMethodBase implement public function getLangcode(Request $request = NULL) { $config = $this->config->get('language.negotiation')->get('session'); $param = $config['parameter']; - $langcode = $request && $request->query->get($param) ? $request->query->get($param) : FALSE; + $langcode = $request && $request->query->get($param) ? $request->query->get($param) : NULL; + if (!$langcode && isset($_SESSION[$param])) { + $langcode = $_SESSION[$param]; + } + return $langcode; + } + + /** + * {@inheritdoc} + */ + public function persist(Language $language) { + parent::persist($language); // We need to update the session parameter with the request value only if we // have an authenticated user. + $langcode = $language->id; if ($langcode && $this->languageManager) { $languages = $this->languageManager->getLanguages(); if ($this->currentUser->isAuthenticated() && isset($languages[$langcode])) { - $_SESSION[$param] = $langcode; + $config = $this->config->get('language.negotiation')->get('session'); + $_SESSION[$config['parameter']] = $langcode; } } - - // Session parameter. - if (isset($_SESSION[$param])) { - $langcode = $_SESSION[$param]; - } - - return $langcode; } /** @@ -136,7 +143,7 @@ function getLanguageSwitchLinks(Request $request, $type, $path) { $links[$langcode]['query'][$param] = $langcode; } else { - $links[$langcode]['attributes']['class'][] = ' session-active'; + $links[$langcode]['attributes']['class'][] = 'session-active'; } } diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUI.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUI.php index c3e5aeb..3b64974 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUI.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUI.php @@ -32,7 +32,7 @@ class LanguageNegotiationUI extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - return $this->languageManager ? $this->languageManager->getCurrentLanguage()->id : FALSE; + return $this->languageManager ? $this->languageManager->getCurrentLanguage()->id : NULL; } } diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php index 7cb8bc7..a72ca44 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php @@ -23,7 +23,7 @@ * weight = -8, * name = @Translation("URL"), * description = @Translation("Language from the URL (Path prefix or domain)."), - * config = "admin/config/regional/language/detection/url" + * config_path = "admin/config/regional/language/detection/url" * ) */ class LanguageNegotiationUrl extends LanguageNegotiationMethodBase implements InboundPathProcessorInterface, OutboundPathProcessorInterface, LanguageSwitcherInterface { @@ -47,7 +47,7 @@ class LanguageNegotiationUrl extends LanguageNegotiationMethodBase implements In * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $langcode = NULL; if ($request && $this->languageManager) { $languages = $this->languageManager->getLanguages(); @@ -68,7 +68,7 @@ public function getLangcode(Request $request = NULL) { } } - if ($negotiated_language !== FALSE && $negotiated_language instanceof \Drupal\Core\Language\Language) { + if ($negotiated_language) { $langcode = $negotiated_language->id; } break; @@ -133,7 +133,7 @@ public function processOutbound($path, &$options = array(), Request $request = N $options['language'] = $language_url; } // We allow only enabled languages here. - elseif (is_object($options['language']) && !isset($languages[$options['language']->id])) { + elseif (!is_object($options['language']) || !isset($languages[$options['language']->id])) { return $path; } $config = $this->config->get('language.negotiation')->get('url'); diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php index 379cf79..2310153 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php +++ b/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php @@ -51,7 +51,7 @@ class LanguageNegotiationUrlFallback extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $langcode = NULL; if ($this->languageManager) { $default = $this->languageManager->getDefaultLanguage(); diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php index f660eb8..5571668 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php @@ -7,7 +7,7 @@ namespace Drupal\language\Tests; -use Drupal\Component\Utility\Browser; +use Drupal\Component\Utility\UserAgent; use Drupal\Core\Language\Language; use Drupal\simpletest\WebTestBase; use Symfony\Component\HttpFoundation\Request; @@ -157,7 +157,7 @@ function testLanguageFromBrowser() { $mappings = $this->container->get('config.factory')->get('language.mappings')->get(); foreach ($test_cases as $accept_language => $expected_result) { - $result = Browser::getLangcode($accept_language, array_keys($languages), $mappings); + $result = UserAgent::getBestMatchingLangcode($accept_language, array_keys($languages), $mappings); $this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none'))); } } diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php index 9e3cdab..e8b3b26 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php @@ -10,11 +10,12 @@ use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; -use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUser; -use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; +use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser; +use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; use Drupal\simpletest\WebTestBase; use Drupal\Core\Language\Language; use Symfony\Component\HttpFoundation\Request; +use Drupal\language\LanguageNegotiatorInterface; /** * Test UI language negotiation @@ -53,7 +54,7 @@ class LanguageUILanguageNegotiationTest extends WebTestBase { * * @var array */ - public static $modules = array('locale', 'language_test', 'block'); + public static $modules = array('locale', 'language_test', 'block', 'user'); public static function getInfo() { return array( @@ -165,7 +166,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); @@ -177,7 +178,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); @@ -189,7 +190,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.', ), @@ -225,7 +226,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => $http_header_blah, 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language', ), @@ -250,7 +251,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => array(), 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default', ); @@ -265,7 +266,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => array(), 'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default', ); @@ -293,7 +294,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default', ); @@ -307,7 +308,7 @@ function testUILanguageNegotiation() { 'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, + 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default', ); diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 7c62df0..e67520b 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -12,8 +12,8 @@ use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\language\ConfigurableLanguageManagerInterface; -use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; -use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUser; +use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; +use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser; use Symfony\Component\DependencyInjection\ContainerInterface; /** diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUser.php b/core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUser.php similarity index 88% rename from core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUser.php rename to core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUser.php index 2e267e3..f2c99fe 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUser.php +++ b/core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUser.php @@ -5,7 +5,7 @@ * Contains \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl. */ -namespace Drupal\language\Plugin\LanguageNegotiation; +namespace Drupal\user\Plugin\LanguageNegotiation; use Drupal\language\LanguageNegotiationMethodBase; use Symfony\Component\HttpFoundation\Request; @@ -14,7 +14,7 @@ * Class for identifying language from the user preferences. * * @Plugin( - * id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUser::METHOD_ID, + * id = \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser::METHOD_ID, * weight = -4, * name = @Translation("User"), * description = @Translation("Follow the user's language preference.") @@ -31,7 +31,7 @@ class LanguageNegotiationUser extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $langcode = NULL; // User preference (only for authenticated users). if ($this->languageManager && $this->currentUser->isAuthenticated()) { diff --git a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php b/core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php similarity index 87% rename from core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php rename to core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php index fd1639c..c7a42eb 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php +++ b/core/modules/user/lib/Drupal/user/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin. + * Contains \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin. */ -namespace Drupal\language\Plugin\LanguageNegotiation; +namespace Drupal\user\Plugin\LanguageNegotiation; use Drupal\language\LanguageNegotiationMethodBase; use Symfony\Component\HttpFoundation\Request; @@ -14,7 +14,7 @@ * Identifies admin language from the user preferences. * * @Plugin( - * id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin::METHOD_ID, + * id = Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin::METHOD_ID, * types = {Drupal\Core\Language\Language::TYPE_INTERFACE}, * weight = 10, * name = @Translation("Account administration pages"), @@ -32,7 +32,7 @@ class LanguageNegotiationUserAdmin extends LanguageNegotiationMethodBase { * {@inheritdoc} */ public function getLangcode(Request $request = NULL) { - $langcode = FALSE; + $langcode = NULL; // User preference (only for authenticated users). if ($this->languageManager && $this->currentUser->isAuthenticated() && $this->isAdminPath($request)) {