2019-12-10 21:25:54 +01:00
|
|
|
<?php
|
2021-12-09 06:34:23 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-12-09 06:34:23 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
2019-12-10 21:25:54 +01:00
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-10 21:25:54 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
|
|
|
|
class Locale
|
|
|
|
{
|
2023-11-21 04:43:02 +01:00
|
|
|
private array $locales = [
|
|
|
|
'en',
|
|
|
|
'it',
|
|
|
|
'de',
|
|
|
|
'fr',
|
|
|
|
'pt_BR',
|
|
|
|
'nl',
|
|
|
|
'es',
|
|
|
|
'nb_NO',
|
|
|
|
'da',
|
|
|
|
'ja',
|
|
|
|
'sv',
|
|
|
|
'es_ES',
|
|
|
|
'fr_CA',
|
|
|
|
'lt',
|
|
|
|
'pl',
|
|
|
|
'cs',
|
|
|
|
'hr',
|
|
|
|
'sq',
|
|
|
|
'el',
|
|
|
|
'en_GB',
|
|
|
|
'pt_PT',
|
|
|
|
'sl',
|
|
|
|
'fi',
|
|
|
|
'ro',
|
|
|
|
'tr_TR',
|
|
|
|
'th',
|
|
|
|
'mk_MK',
|
|
|
|
'zh_TW',
|
|
|
|
'ru_RU',
|
|
|
|
'ar',
|
|
|
|
'fa',
|
|
|
|
'lv_LV',
|
|
|
|
'sr',
|
|
|
|
'sk',
|
|
|
|
'et',
|
|
|
|
'bg',
|
|
|
|
'he',
|
|
|
|
'km_KH',
|
2024-01-12 11:04:54 +01:00
|
|
|
'lo_LA',
|
2023-11-21 04:43:02 +01:00
|
|
|
'hu',
|
|
|
|
'fr_CH',
|
|
|
|
];
|
2019-12-10 21:25:54 +01:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-12-10 21:25:54 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
2023-11-26 08:41:42 +01:00
|
|
|
{
|
2024-06-15 08:18:33 +02:00
|
|
|
|
|
|
|
|
2019-12-10 21:25:54 +01:00
|
|
|
/*LOCALE SET */
|
2023-11-26 08:41:42 +01:00
|
|
|
if ($request->has('lang') && in_array($request->input('lang', 'en'), $this->locales)) {
|
2023-11-21 04:43:02 +01:00
|
|
|
$locale = $request->input('lang');
|
2019-12-10 21:25:54 +01:00
|
|
|
App::setLocale($locale);
|
2022-02-16 03:24:10 +01:00
|
|
|
} elseif (auth()->guard('contact')->user()) {
|
2024-06-15 08:18:33 +02:00
|
|
|
|
|
|
|
auth()->guard('contact')->user()->loadMissing(['client' => function ($query) { // @phpstan-ignore method.undefined
|
|
|
|
$query->without('gateway_tokens', 'documents', 'contacts.company', 'contacts'); // Exclude 'grandchildren' relation of 'client'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
App::setLocale(auth()->guard('contact')->user()->client->locale());
|
2019-12-10 21:25:54 +01:00
|
|
|
} elseif (auth()->user()) {
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2024-06-15 08:18:33 +02:00
|
|
|
App::setLocale(auth()->user()->company()->getLocale()); // @phpstan-ignore method.undefined
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2022-01-16 09:09:46 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-12-10 21:25:54 +01:00
|
|
|
App::setLocale(config('ninja.i18n.locale'));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-10 21:25:54 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|