Skip to content

Commit

Permalink
Fix missing import in LocaleCookieRedirect (#674)
Browse files Browse the repository at this point in the history
* Add missing import for LanguageNegotiator

* Fix formatting
  • Loading branch information
nikazooz authored and Marc Cámara committed Oct 25, 2019
1 parent 460292b commit 72a2285
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
75 changes: 43 additions & 32 deletions src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Cookie;
use Mcamara\LaravelLocalization\LanguageNegotiator;

class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
{
Expand All @@ -15,39 +16,49 @@ class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
*
* @return mixed
*/
public function handle($request, Closure $next) {
// If the URL of the request is in exceptions.
if ($this->shouldIgnore($request)) {
return $next($request);
}
public function handle($request, Closure $next)
{
// If the URL of the request is in exceptions.
if ($this->shouldIgnore($request)) {
return $next($request);
}

$params = explode('/', $request->path());
$locale = $request->cookie('locale', false);
$params = explode('/', $request->path());
$locale = $request->cookie('locale', false);

if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
}
elseif(empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
$locale = $negotiator->negotiateLanguage();
Cookie::queue(Cookie::forever('locale', $locale));
}

if($locale === false){
$locale = app('laravellocalization')->getCurrentLocale();
}

if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
$redirection = app('laravellocalization')->getLocalizedURL($locale);
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);

return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
}

return $next($request);
}
}

if (empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(
app('laravellocalization')->getDefaultLocale(),
app('laravellocalization')->getSupportedLocales(),
$request
);
$locale = $negotiator->negotiateLanguage();
Cookie::queue(Cookie::forever('locale', $locale));
}

if ($locale === false){
$locale = app('laravellocalization')->getCurrentLocale();
}

if (
$locale &&
app('laravellocalization')->checkLocaleInSupportedLocales($locale) &&
!(app('laravellocalization')->isHiddenDefault($locale))
) {
$redirection = app('laravellocalization')->getLocalizedURL($locale);
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);

return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
}

return $next($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\LanguageNegotiator;
use Mcamara\LaravelLocalization\LaravelLocalization;

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

return $next($request);
}
elseif(empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
$locale = $negotiator->negotiateLanguage();
session(['locale' => $locale]);

if (empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(
app('laravellocalization')->getDefaultLocale(),
app('laravellocalization')->getSupportedLocales(),
$request
);
$locale = $negotiator->negotiateLanguage();
session(['locale' => $locale]);
}

if($locale === false){
$locale = app('laravellocalization')->getCurrentLocale();
if ($locale === false){
$locale = app('laravellocalization')->getCurrentLocale();
}

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

Expand Down

0 comments on commit 72a2285

Please sign in to comment.