Aknaproff/app/Http/Middleware/LocaleMiddleware.php

42 lines
955 B
PHP
Raw Normal View History

2024-03-13 05:31:30 +00:00
<?php
namespace App\Http\Middleware;
use App\UserSetting;
use Closure;
use Illuminate\Support\Facades\Session;
class LocaleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (empty(Session::get('locale'))) {
$userSetting = UserSetting::where('user_id', auth()->user()->id)->first();
$locale = 'en';
if (! in_array($userSetting->language, ['en', 'ru', 'est'])) {
$locale = $userSetting->language;
}
Session::put('locale', $locale);
app()->setLocale($locale);
if (!empty($userSetting)) {
$userSetting->update([
'language' => $locale
]);
}
}
return $next($request);
}
}