From ae105c0977ba9a91e6388a9011717745056c73a2 Mon Sep 17 00:00:00 2001 From: Oleksandr Kovpashko Date: Sat, 28 Mar 2020 20:26:05 +0200 Subject: [PATCH 1/2] Preserve "locale" cookie value on redirect to localized URL --- .../Middleware/LocaleCookieRedirect.php | 2 +- tests/LocalizerTests.php | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php index d043a94..9eb396b 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php @@ -56,7 +56,7 @@ public function handle($request, Closure $next) $redirection = app('laravellocalization')->getLocalizedURL($locale); $redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']); - return $redirectResponse->withCookie(cookie()->forever('locale', $params[0])); + return $redirectResponse->withCookie(cookie()->forever('locale', $locale)); } return $next($request); diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 80f83f6..ca0a7c6 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -880,4 +880,38 @@ public function testSetLocaleWithMapping() $this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom')); $this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en')); } + + public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale() + { + app('router')->group([ + 'prefix' => app('laravellocalization')->setLocale(), + 'middleware' => [ + 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter', + 'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect', + ], + ], function (){ + app('router')->get('/', ['as'=> 'index', function () { + return 'Index page'; + }, ]); + }); + + app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); + + $savedLocale = 'es'; + + $crawler = $this->call( + 'GET', + $this->test_url, + [], + ['locale' => $savedLocale], + [], + [] + ); + + $this->assertResponseStatus(302); + $this->assertRedirectedTo($this->test_url . $savedLocale); + + $localeCookie = $crawler->headers->getCookies()[0]; + $this->assertEquals($savedLocale, $localeCookie->getValue()); + } } From 443610a99e9fadb883a4101990b631fa38376946 Mon Sep 17 00:00:00 2001 From: Adam Nielsen Date: Sun, 1 Dec 2024 23:52:50 +0100 Subject: [PATCH 2/2] Fix test --- tests/LaravelLocalizationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/LaravelLocalizationTest.php b/tests/LaravelLocalizationTest.php index 44c00ff..a56ab47 100644 --- a/tests/LaravelLocalizationTest.php +++ b/tests/LaravelLocalizationTest.php @@ -884,7 +884,7 @@ public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale() $crawler = $this->call( 'GET', - $this->test_url, + self::$testUrl, [], ['locale' => $savedLocale], [], @@ -892,7 +892,7 @@ public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale() ); $this->assertResponseStatus(302); - $this->assertRedirectedTo($this->test_url . $savedLocale); + $this->assertRedirectedTo(self::$testUrl . $savedLocale); $localeCookie = $crawler->headers->getCookies()[0]; $this->assertEquals($savedLocale, $localeCookie->getValue());