fixed language switching in the frontend
This commit is contained in:
@@ -6,16 +6,18 @@ use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use App\Support\LocaleConfig;
|
||||
|
||||
class SetLocaleFromRequest
|
||||
{
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$supportedLocales = $this->supportedLocales();
|
||||
$supportedLocales = LocaleConfig::normalized();
|
||||
|
||||
$locale = (string) $request->route('locale');
|
||||
$locale = (string) ($request->route('locale') ?? $request->segment(1));
|
||||
$normalizedLocale = LocaleConfig::canonicalize($locale);
|
||||
|
||||
if (! $locale || ! in_array($locale, $supportedLocales, true)) {
|
||||
if (! $locale || ! in_array($normalizedLocale, $supportedLocales, true)) {
|
||||
$preferred = Session::get('preferred_locale');
|
||||
|
||||
if ($preferred && in_array($preferred, $supportedLocales, true)) {
|
||||
@@ -27,35 +29,11 @@ class SetLocaleFromRequest
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
App::setLocale($locale);
|
||||
Session::put('preferred_locale', $locale);
|
||||
Session::put('locale', $locale);
|
||||
$request->attributes->set('preferred_locale', $locale);
|
||||
App::setLocale($normalizedLocale);
|
||||
Session::put('preferred_locale', $normalizedLocale);
|
||||
Session::put('locale', $normalizedLocale);
|
||||
$request->attributes->set('preferred_locale', $normalizedLocale);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function supportedLocales(): array
|
||||
{
|
||||
$configured = array_filter(array_map(
|
||||
static fn ($value) => trim((string) $value),
|
||||
explode(',', (string) env('APP_SUPPORTED_LOCALES', ''))
|
||||
));
|
||||
|
||||
if (empty($configured)) {
|
||||
$configured = array_filter([
|
||||
config('app.locale'),
|
||||
config('app.fallback_locale'),
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($configured)) {
|
||||
$configured = ['de', 'en'];
|
||||
}
|
||||
|
||||
return array_values(array_unique($configured));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user