From 4c1548d2a8f0b945ea927912078a870cf65524bd Mon Sep 17 00:00:00 2001 From: Adam Nielsen <1765602+iwasherefirst2@users.noreply.github.com> Date: Tue, 15 Oct 2019 01:01:20 +0200 Subject: [PATCH] Added LocaleCookieRedirect Middleware to Readme Also made it compatible for the case that its used with hidden default locale and accept header. Closes #613 --- README.md | 14 ++++++++++++++ .../Middleware/LocaleCookieRedirect.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 156a65f..f5b085b 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,20 @@ In there is no locale present in the url, then this middleware will check the fo 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. +#### LocaleCookieRedirect + +Similar to LocaleSessionRedirect, but it stores value in a cookie instead of a session. + +Whenever a locale is present in the url, it will be stored in the session by this middleware. + +In there is no locale present in the url, then this middleware will check the following + + - If no locale is saved in cookie and `useAcceptLanguageHeader` is set to true, compute locale from browser and redirect to url with locale. + - If a locale is saved in cookie redirect to url with locale, unless its the default locale and `hideDefaultLocaleInURL` is set to true. + +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. + + #### LaravelLocalizationRedirectFilter When the default locale is present in the url and `hideDefaultLocaleInURL` is set to true, then the middleware redirects to the url without locale. diff --git a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php index b6277ec..988901f 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php @@ -2,6 +2,7 @@ use Closure; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Cookie; class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase { @@ -26,6 +27,19 @@ public function handle($request, Closure $next) { 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);