Skip to content

Commit

Permalink
Added LocaleCookieRedirect Middleware to Readme (#669)
Browse files Browse the repository at this point in the history
Also made it compatible for the case that its used
with hidden default locale and accept header.

Closes #613
  • Loading branch information
iwasherefirst2 authored and Marc Cámara committed Oct 15, 2019
1 parent 676608c commit 15d3786
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Cookie;

class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
{
Expand All @@ -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);
Expand Down

0 comments on commit 15d3786

Please sign in to comment.