Answer the question
In order to leave comments, you need to log in
Multilingual Laravel 8 depending on the user's GEO?
Hello. The question is of interest - how to set multilingualism for your site, depending on the user's preferred language?
I have already implemented the switch - and the basic translation for the test. The base value is always indicated by en - and I understand the app config I can change - but this is not the case, since there is a base language and a language that will be replaced. I need the user's language to be checked through the middleware and set as a priority!
SetLocale - Middleware
public function handle(Request $request, Closure $next)
{
$locale = session('locale');
$defaultLocale = App::getLocale(); - вот тут я думаю ошибка где то, ну или вы мне подскажите :((
App::setLocale($locale ?? $defaultLocale);
return $next($request);
}
Route::get('locale/{locale}', 'Site\[email protected]')->name('locale');
Route::group(['namespace' => 'Site', 'middleware' => 'set locale'], function() {
// Base websites Urls
Route::get('/', '[email protected]')->name('home');
.......
.......
});
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'admin' => \App\Http\Middleware\CheckIfAdmin::class,
'set locale' => \App\Http\Middleware\SetLocale::class,
public function changeLocale($locale)
{
session(['locale' => $locale]);
App::setLocale($locale);
return redirect()->back();
}
Answer the question
In order to leave comments, you need to log in
I did it according to oak - but if someone offers a better one - I will not refuse!
SetLocale.php (Middleware)
public function handle(Request $request, Closure $next)
{
$locale = session('locale');
$defaultLocale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
App::setLocale($locale ? $defaultLocale : $defaultLocale);
return $next($request);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question