snipe-it/app/Http/Middleware/CheckLocale.php

52 lines
1.5 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Http\Middleware;
2016-03-25 19:26:22 -07:00
use App\Models\Setting;
use Closure;
use \App\Helpers\Helper;
2016-03-25 01:18:05 -07:00
class CheckLocale
{
private function warn_legacy_locale($language, $source)
{
if ($language != Helper::mapLegacyLocale($language)) {
\Log::warning("$source $language and should be updated to be ".Helper::mapLegacyLocale($language));
}
}
/**
* Handle the locale for the user, default to settings otherwise.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
2016-03-25 01:18:05 -07:00
public function handle($request, Closure $next, $guard = null)
{
// Default app settings from config
$language = config('app.locale');
$this->warn_legacy_locale($language, "APP_LOCALE in .env is set to");
if ($settings = Setting::getSettings()) {
2016-05-14 15:05:28 -07:00
// User's preference
if (($request->user()) && ($request->user()->locale)) {
$language = $request->user()->locale;
$this->warn_legacy_locale($language, "username ".$request->user()->username." (".$request->user()->id.") has a language");
2016-03-25 01:18:05 -07:00
// App setting preference
} elseif ($settings->locale != '') {
$language = $settings->locale;
$this->warn_legacy_locale($language, "App Settings is set to");
2016-05-14 15:05:28 -07:00
}
2016-03-25 01:18:05 -07:00
}
\App::setLocale(Helper::mapLegacyLocale($language));
2016-03-25 01:18:05 -07:00
return $next($request);
}
}