diff --git a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php index f36d43e..7d10a88 100644 --- a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php +++ b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php @@ -109,14 +109,19 @@ public function processOutbound($path, &$options = [], Request $request = NULL, // Cached URLs that have been processed by this outbound path // processor must be: $bubbleable_metadata - // - invalidated when the language negotiation config changes, since - // another query parameter may be used to determine the language. + // - invalidated when the language negotiation config changes, since + // another query parameter may be used to determine the language. ->addCacheTags($this->config->get('language.negotiation')->getCacheTags()) - // - varied by the configured query parameter. + // - varied by the configured query parameter. ->addCacheContexts(['url.query_args:' . $this->queryParam]); } } } + // To avoid page cache duplicates, remove the language query if the default + // language is set. + if ($this->currentUser->isAnonymous() && $cache && isset($options['query'][$this->queryParam]) && $options['query'][$this->queryParam] == $default) { + unset($options['query'][$this->queryParam]); + } return $path; } @@ -145,11 +150,16 @@ public function getLanguageSwitchLinks(Request $request, $type, Url $url) { $links[$langcode]['query'][$param] = $langcode; } else { - $links[$langcode]['attributes']['class'][] = 'session-active'; + if ($langcode == $language_query) { + $links[$langcode]['attributes']['class'][] = 'session-active'; + } + // Always set the language query parameter. It may be removed in + // LanguageNegotiationSession::processOutbound(). + $links[$langcode]['query'][$param] = $langcode; } - } - return $links; + return $links; + } } }