Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve "locale" cookie value on redirect to localized URL #715

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}