From a30db533bd26b383f50042b9fc95da10fad3f926 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Rapha=C3=ABl=20Gertz?= <git@rapsys.eu>
Date: Mon, 28 Dec 2020 08:40:29 +0100
Subject: [PATCH] Remove redirection in favor of varying page

---
 EventSubscriber/LocaleSubscriber.php | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/EventSubscriber/LocaleSubscriber.php b/EventSubscriber/LocaleSubscriber.php
index 7941a80..29417a7 100644
--- a/EventSubscriber/LocaleSubscriber.php
+++ b/EventSubscriber/LocaleSubscriber.php
@@ -33,9 +33,27 @@ class LocaleSubscriber implements EventSubscriberInterface {
 				//Save preferred locale in session
 				$request->getSession()->set('_locale', $preferred);
 
+				//Send vary header as current page locale depend on it
+				header('Vary: accept-language');
+
+				//Set locale
+				$request->setLocale($preferred);
+
+				//Set default locale
+				$request->setDefaultLocale($preferred);
+
+				//Get router context
+				$context = $this->router->getContext();
+
+				//Set context locale
+				$context->setParameter('_locale', $preferred);
+
+				//Set back router context
+				$this->router->setContext($context);
+
 				//Generate current route with preferred language
 				//XXX: may trigger a Symfony\Component\Routing\Exception\RouteNotFoundException if route is not found for preferred locale
-				$uri = $this->router->generate(
+				/*$uri = $this->router->generate(
 					//Current route
 					$request->get('_route'),
 					//Force preferred locale
@@ -43,7 +61,7 @@ class LocaleSubscriber implements EventSubscriberInterface {
 				);
 
 				//Regenerate route with preferred locale
-				$event->setResponse(new RedirectResponse($uri, 302));
+				$event->setResponse(new RedirectResponse($uri, 302));*/
 
 				//End process
 				return;
-- 
2.41.1