Skip to content

Commit 72a2285

Browse files
nikazoozMarc Cámara
authored andcommitted
Fix missing import in LocaleCookieRedirect (#674)
* Add missing import for LanguageNegotiator * Fix formatting
1 parent 460292b commit 72a2285

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Closure;
44
use Illuminate\Http\RedirectResponse;
55
use Illuminate\Support\Facades\Cookie;
6+
use Mcamara\LaravelLocalization\LanguageNegotiator;
67

78
class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
89
{
@@ -15,39 +16,49 @@ class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
1516
*
1617
* @return mixed
1718
*/
18-
public function handle($request, Closure $next) {
19-
// If the URL of the request is in exceptions.
20-
if ($this->shouldIgnore($request)) {
21-
return $next($request);
22-
}
19+
public function handle($request, Closure $next)
20+
{
21+
// If the URL of the request is in exceptions.
22+
if ($this->shouldIgnore($request)) {
23+
return $next($request);
24+
}
2325

24-
$params = explode('/', $request->path());
25-
$locale = $request->cookie('locale', false);
26+
$params = explode('/', $request->path());
27+
$locale = $request->cookie('locale', false);
2628

27-
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
29+
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
2830
return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
29-
}
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-
}
43-
44-
if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
45-
$redirection = app('laravellocalization')->getLocalizedURL($locale);
46-
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
47-
48-
return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
49-
}
50-
51-
return $next($request);
52-
}
31+
}
32+
33+
if (empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
34+
// When default locale is hidden and accept language header is true,
35+
// then compute browser language when no session has been set.
36+
// Once the session has been set, there is no need
37+
// to negotiate language from browser again.
38+
$negotiator = new LanguageNegotiator(
39+
app('laravellocalization')->getDefaultLocale(),
40+
app('laravellocalization')->getSupportedLocales(),
41+
$request
42+
);
43+
$locale = $negotiator->negotiateLanguage();
44+
Cookie::queue(Cookie::forever('locale', $locale));
45+
}
46+
47+
if ($locale === false){
48+
$locale = app('laravellocalization')->getCurrentLocale();
49+
}
50+
51+
if (
52+
$locale &&
53+
app('laravellocalization')->checkLocaleInSupportedLocales($locale) &&
54+
!(app('laravellocalization')->isHiddenDefault($locale))
55+
) {
56+
$redirection = app('laravellocalization')->getLocalizedURL($locale);
57+
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
58+
59+
return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
60+
}
61+
62+
return $next($request);
63+
}
5364
}

src/Mcamara/LaravelLocalization/Middleware/LocaleSessionRedirect.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Http\RedirectResponse;
77
use Mcamara\LaravelLocalization\LanguageNegotiator;
8-
use Mcamara\LaravelLocalization\LaravelLocalization;
98

109
class LocaleSessionRedirect extends LaravelLocalizationMiddlewareBase
1110
{
@@ -32,21 +31,30 @@ public function handle($request, Closure $next)
3231

3332
return $next($request);
3433
}
35-
elseif(empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
36-
// When default locale is hidden and accept language header is true,
37-
// then compute browser language when no session has been set.
38-
// Once the session has been set, there is no need
39-
// to negotiate language from browser again.
40-
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
41-
$locale = $negotiator->negotiateLanguage();
42-
session(['locale' => $locale]);
34+
35+
if (empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
36+
// When default locale is hidden and accept language header is true,
37+
// then compute browser language when no session has been set.
38+
// Once the session has been set, there is no need
39+
// to negotiate language from browser again.
40+
$negotiator = new LanguageNegotiator(
41+
app('laravellocalization')->getDefaultLocale(),
42+
app('laravellocalization')->getSupportedLocales(),
43+
$request
44+
);
45+
$locale = $negotiator->negotiateLanguage();
46+
session(['locale' => $locale]);
4347
}
4448

45-
if($locale === false){
46-
$locale = app('laravellocalization')->getCurrentLocale();
49+
if ($locale === false){
50+
$locale = app('laravellocalization')->getCurrentLocale();
4751
}
4852

49-
if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
53+
if (
54+
$locale &&
55+
app('laravellocalization')->checkLocaleInSupportedLocales($locale) &&
56+
!(app('laravellocalization')->isHiddenDefault($locale))
57+
) {
5058
app('session')->reflash();
5159
$redirection = app('laravellocalization')->getLocalizedURL($locale);
5260

0 commit comments

Comments
 (0)