1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Http/Middleware/Locale.php

90 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2021-12-09 06:34:23 +01:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-12-09 06:34:23 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Middleware;
use Closure;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Request;
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',
];
/**
* Handle an incoming request.
*
2020-10-28 11:10:49 +01:00
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
2023-11-26 08:41:42 +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');
App::setLocale($locale);
} elseif (auth()->guard('contact')->user()) {
App::setLocale(auth()->guard('contact')->user()->client()->setEagerLoads([])->first()->locale());
} elseif (auth()->user()) {
try {
2022-01-16 09:09:46 +01:00
App::setLocale(auth()->user()->company()->getLocale());
} catch (\Exception $e) {
2022-01-16 09:09:46 +01:00
}
} else {
App::setLocale(config('ninja.i18n.locale'));
}
return $next($request);
}
}