Answer the question
In order to leave comments, you need to log in
Why doesn't the cookie/session of the selected language work?
Good evening!
I am using the Mcamara - laravel-localization package .
Left two available languages: russian and hebrew. The Russian language is set by default in the config. If I go to site.ru/ru , the main page will be in Russian. If I go to sire.ru/he - in Hebrew.
But the fact is that when I go to sire.ru or, for example, sire.ru/search , the default language (ru) is automatically substituted. Example: sire.ru/ru/search
Route.php:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localeCookieRedirect', 'localizationRedirect']
], function() {
Route::group(['middleware' => 'guest'], function() {
Route::get('/authorization', '[email protected]');
Route::post('/authorization', '[email protected]');
});
Route::get('/language/{lang}', '[email protected]'); // Сам переключатель.
});
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'active' => \App\Http\Middleware\LastActive::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class
];
public function setLocale($lang)
{
$locale = ($lang == 'hebrew') ? 'he' : 'ru';
$supported = LaravelLocalization::getSupportedLocales();
if (array_key_exists($locale, $supported)) {
$cookie = Cookie::forever('locale', $locale);
}
return redirect()->to(LaravelLocalization::getNonLocalizedURL(url()->previous()))->withCookie($cookie);
}
Answer the question
In order to leave comments, you need to log in
In general, I solved this issue like this:
1. Used this - https://github.com/mcamara/laravel-localization/co...
2. LocaleCookieRedirect.php:
<?php namespace Mcamara\LaravelLocalization\Middleware;
use Illuminate\Http\RedirectResponse;
use Closure;
use Cookie;
class LocaleCookieRedirect {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle( $request, Closure $next )
{
$params = explode('/', $request->path());
$locale = $request->cookie('locale', false);
if (count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])){
Cookie::forever('locale', $params[0]);
return $next($request);
}
if ( $locale && !(app('laravellocalization')->getDefaultLocale() === $locale && app('laravellocalization')->hideDefaultLocaleInURL())) {
app('session')->reflash();
$redirection = app('laravellocalization')->getLocalizedURL($locale);
return new RedirectResponse($redirection, 302, [ 'Vary' => 'Accept-Language' ]);
}
return $next($request);
}
}
public function setLocale($lang)
{
$locale = ($lang == 'hebrew') ? 'he' : 'ru';
$supported = LaravelLocalization::getSupportedLocales();
if (array_key_exists($locale, $supported)) {
LaravelLocalization::setLocale($locale);
$cookie = Cookie::forever('locale', $locale);
}
return redirect()->to(LaravelLocalization::getNonLocalizedURL(url()->previous()))->withCookie($cookie);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question