From 5ef6f2b25cc10099ddbfd968ddb223ffd9b34953 Mon Sep 17 00:00:00 2001 From: Nicolas Frandeboeuf Date: Mon, 30 Jan 2023 16:28:24 +0000 Subject: [PATCH] Stop Symfony from overwriting the localefrom the query string when set_locale_from_accept_language is true --- src/Event/Subscriber/LocaleSubscriber.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Event/Subscriber/LocaleSubscriber.php b/src/Event/Subscriber/LocaleSubscriber.php index 8ce03d46f..5e7da597e 100644 --- a/src/Event/Subscriber/LocaleSubscriber.php +++ b/src/Event/Subscriber/LocaleSubscriber.php @@ -31,6 +31,14 @@ public function onKernelRequest(RequestEvent $event): void $locale = $request->query->get('_locale'); $request->getSession()->set('_locale', $locale); $request->setLocale($locale); + + // Symfony does not take this query parameter into account in its own subscriber. + // Symfony's listener has a lower priority and will thus be called later. + // It will not check that the locale was set already and in case useAcceptLanguageHeader + // (see https://symfony.com/doc/5.4/reference/configuration/framework.html#set-locale-from-accept-language) is set, it will + // overwrite the locale that was just set. + // @see https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php#L71 + $request->attributes->set('_locale', $locale); } elseif ($request->attributes->get('zone', false) === 'backend' && $request->getSession()->has('_backend_locale')) { $request->setLocale($request->getSession()->get('_backend_locale')); } else {