Skip to content

Commit

Permalink
Merge pull request #920 from mcamara/fix-locale-cookie
Browse files Browse the repository at this point in the history
Fix incorrect parameter usage in LocaleCookieRedirect
  • Loading branch information
iwasherefirst2 authored Dec 1, 2024
2 parents a19ddcc + 443610a commit 4d825e2
Show file tree
Hide file tree
Showing 2 changed files with 37 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
36 changes: 36 additions & 0 deletions tests/LaravelLocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,40 @@ public function testSetLocaleWithMapping(): void
$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',
self::$testUrl,
[],
['locale' => $savedLocale],
[],
[]
);

$this->assertResponseStatus(302);
$this->assertRedirectedTo(self::$testUrl . $savedLocale);

$localeCookie = $crawler->headers->getCookies()[0];
$this->assertEquals($savedLocale, $localeCookie->getValue());
}
}

0 comments on commit 4d825e2

Please sign in to comment.