academico-sis /
academico
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Http\Middleware; |
||||
| 4 | |||||
| 5 | use Carbon\Carbon; |
||||
| 6 | use Closure; |
||||
| 7 | use Illuminate\Http\Request; |
||||
| 8 | use Illuminate\Support\Facades\App; |
||||
| 9 | use Illuminate\Support\Facades\Date; |
||||
| 10 | |||||
| 11 | class SetLocale |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * This function checks if language to set is an allowed lang of config. |
||||
| 15 | */ |
||||
| 16 | private function setLocale(string $locale) |
||||
| 17 | { |
||||
| 18 | // Check if is allowed and set default locale if not |
||||
| 19 | if (! language()->allowed($locale)) { |
||||
| 20 | $locale = config('app.locale'); |
||||
| 21 | } |
||||
| 22 | |||||
| 23 | // Set app language |
||||
| 24 | App::setLocale($locale); |
||||
| 25 | |||||
| 26 | // Set carbon language |
||||
| 27 | if (config('language.carbon')) { |
||||
| 28 | // Carbon uses only language code |
||||
| 29 | if (config('language.mode.code') == 'long') { |
||||
| 30 | $locale = explode('-', $locale)[0]; |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | Carbon::setLocale($locale); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | // Set date language |
||||
| 37 | if (config('language.date')) { |
||||
| 38 | // Date uses only language code |
||||
| 39 | if (config('language.mode.code') == 'long') { |
||||
| 40 | $locale = explode('-', $locale)[0]; |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | Date::setLocale($locale); |
||||
| 44 | } |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | public function setDefaultLocale() |
||||
| 48 | { |
||||
| 49 | $this->setLocale(config('app.locale')); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | public function setUserLocale() |
||||
| 53 | { |
||||
| 54 | $user = backpack_auth()->user(); |
||||
| 55 | |||||
| 56 | if ($user->locale) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 57 | $this->setLocale($user->locale); |
||||
| 58 | } else { |
||||
| 59 | $this->setDefaultLocale(); |
||||
| 60 | } |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | public function setSystemLocale($request) |
||||
| 64 | { |
||||
| 65 | if ($request->session()->has('locale')) { |
||||
| 66 | $this->setLocale(session('locale')); |
||||
|
0 ignored issues
–
show
It seems like
session('locale') can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $locale of App\Http\Middleware\SetLocale::setLocale() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 67 | } else { |
||||
| 68 | $this->setDefaultLocale(); |
||||
| 69 | } |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * Handle an incoming request. |
||||
| 74 | * |
||||
| 75 | * @param Request $request |
||||
| 76 | * |
||||
| 77 | * @return mixed |
||||
| 78 | */ |
||||
| 79 | public function handle($request, Closure $next) |
||||
| 80 | { |
||||
| 81 | if (backpack_auth()->check()) { |
||||
| 82 | $this->setUserLocale(); |
||||
| 83 | } else { |
||||
| 84 | $this->setSystemLocale($request); |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | return $next($request); |
||||
| 88 | } |
||||
| 89 | } |
||||
| 90 |