42 lines
955 B
PHP
42 lines
955 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 (! 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);
|
||
|
}
|
||
|
}
|