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;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Closure;
|
2023-12-19 09:43:22 -08:00
|
|
|
use \App\Helpers\Helper;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-03-25 01:18:05 -07:00
|
|
|
class CheckLocale
|
|
|
|
{
|
2024-03-08 06:04:21 -08:00
|
|
|
private function warn_legacy_locale($language, $source)
|
|
|
|
{
|
|
|
|
if ($language != Helper::mapLegacyLocale($language)) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::warning("$source $language and should be updated to be ".Helper::mapLegacyLocale($language));
|
2024-03-08 06:04:21 -08:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 19:59:50 -07:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2023-12-19 09:43:22 -08:00
|
|
|
|
|
|
|
// Default app settings from config
|
|
|
|
$language = config('app.locale');
|
2024-03-08 06:04:21 -08:00
|
|
|
$this->warn_legacy_locale($language, "APP_LOCALE in .env is set to");
|
2023-12-19 09:43:22 -08:00
|
|
|
|
2018-11-01 19:59:50 -07:00
|
|
|
if ($settings = Setting::getSettings()) {
|
2023-12-19 09:43:22 -08:00
|
|
|
|
2016-05-14 15:05:28 -07:00
|
|
|
// User's preference
|
|
|
|
if (($request->user()) && ($request->user()->locale)) {
|
2023-12-19 09:43:22 -08:00
|
|
|
$language = $request->user()->locale;
|
2024-03-08 06:04:21 -08:00
|
|
|
$this->warn_legacy_locale($language, "username ".$request->user()->username." (".$request->user()->id.") has a language");
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-11-01 19:59:50 -07:00
|
|
|
// App setting preference
|
|
|
|
} elseif ($settings->locale != '') {
|
2023-12-19 09:43:22 -08:00
|
|
|
$language = $settings->locale;
|
2024-03-08 06:04:21 -08:00
|
|
|
$this->warn_legacy_locale($language, "App Settings is set to");
|
2016-05-14 15:05:28 -07:00
|
|
|
}
|
2023-12-19 09:43:22 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2024-03-08 06:04:21 -08:00
|
|
|
|
2024-07-13 07:15:18 -07:00
|
|
|
app()->setLocale(Helper::mapLegacyLocale($language));
|
2016-03-25 01:18:05 -07:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|