Aknaproff/app/Http/Middleware/LocaleMiddleware.php
2024-03-17 17:40:23 +05:00

38 lines
871 B
PHP

<?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 (!empty($userSetting)) {
if (!in_array($userSetting->language, ['en', 'ru', 'est'])) {
$locale = $userSetting->language;
}
}
Session::put('locale', $locale);
app()->setLocale($locale);
}
return $next($request);
}
}