Skip to content

Commit

Permalink
Preserve "locale" cookie value on redirect to localized URL
Browse files Browse the repository at this point in the history
  • Loading branch information
okovpashko committed Dec 4, 2021
1 parent 13f418e commit ae105c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions tests/LocalizerTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit ae105c0

Please sign in to comment.