Skip to content

Commit ae105c0

Browse files
committed
Preserve "locale" cookie value on redirect to localized URL
1 parent 13f418e commit ae105c0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function handle($request, Closure $next)
5656
$redirection = app('laravellocalization')->getLocalizedURL($locale);
5757
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
5858

59-
return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
59+
return $redirectResponse->withCookie(cookie()->forever('locale', $locale));
6060
}
6161

6262
return $next($request);

tests/LocalizerTests.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,4 +880,38 @@ public function testSetLocaleWithMapping()
880880
$this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom'));
881881
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
882882
}
883+
884+
public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale()
885+
{
886+
app('router')->group([
887+
'prefix' => app('laravellocalization')->setLocale(),
888+
'middleware' => [
889+
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
890+
'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect',
891+
],
892+
], function (){
893+
app('router')->get('/', ['as'=> 'index', function () {
894+
return 'Index page';
895+
}, ]);
896+
});
897+
898+
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);
899+
900+
$savedLocale = 'es';
901+
902+
$crawler = $this->call(
903+
'GET',
904+
$this->test_url,
905+
[],
906+
['locale' => $savedLocale],
907+
[],
908+
[]
909+
);
910+
911+
$this->assertResponseStatus(302);
912+
$this->assertRedirectedTo($this->test_url . $savedLocale);
913+
914+
$localeCookie = $crawler->headers->getCookies()[0];
915+
$this->assertEquals($savedLocale, $localeCookie->getValue());
916+
}
883917
}

0 commit comments

Comments
 (0)