Skip to content

Commit 15d3786

Browse files
niels-numbersMarc Cámara
authored andcommitted
Added LocaleCookieRedirect Middleware to Readme (#669)
Also made it compatible for the case that its used with hidden default locale and accept header. Closes #613
1 parent 676608c commit 15d3786

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ In there is no locale present in the url, then this middleware will check the fo
175175

176176
For example, if a user navigates to http://url-to-laravel/test and `en` is the current locale, it would redirect him automatically to http://url-to-laravel/en/test.
177177

178+
#### LocaleCookieRedirect
179+
180+
Similar to LocaleSessionRedirect, but it stores value in a cookie instead of a session.
181+
182+
Whenever a locale is present in the url, it will be stored in the session by this middleware.
183+
184+
In there is no locale present in the url, then this middleware will check the following
185+
186+
- If no locale is saved in cookie and `useAcceptLanguageHeader` is set to true, compute locale from browser and redirect to url with locale.
187+
- If a locale is saved in cookie redirect to url with locale, unless its the default locale and `hideDefaultLocaleInURL` is set to true.
188+
189+
For example, if a user navigates to http://url-to-laravel/test and `de` is the current locale, it would redirect him automatically to http://url-to-laravel/de/test.
190+
191+
178192
#### LaravelLocalizationRedirectFilter
179193

180194
When the default locale is present in the url and `hideDefaultLocaleInURL` is set to true, then the middleware redirects to the url without locale.

src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Closure;
44
use Illuminate\Http\RedirectResponse;
5+
use Illuminate\Support\Facades\Cookie;
56

67
class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
78
{
@@ -26,6 +27,19 @@ public function handle($request, Closure $next) {
2627
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
2728
return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
2829
}
30+
elseif(empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
31+
// When default locale is hidden and accept language header is true,
32+
// then compute browser language when no session has been set.
33+
// Once the session has been set, there is no need
34+
// to negotiate language from browser again.
35+
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
36+
$locale = $negotiator->negotiateLanguage();
37+
Cookie::queue(Cookie::forever('locale', $locale));
38+
}
39+
40+
if($locale === false){
41+
$locale = app('laravellocalization')->getCurrentLocale();
42+
}
2943

3044
if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
3145
$redirection = app('laravellocalization')->getLocalizedURL($locale);

0 commit comments

Comments
 (0)