fixed language switching in the frontend

This commit is contained in:
Codex Agent
2025-12-02 13:31:58 +01:00
parent 28539754a7
commit dd3198cb79
20 changed files with 395 additions and 203 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Support\LocaleConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
@@ -11,23 +12,13 @@ class LocaleController extends Controller
public function set(Request $request)
{
$locale = $request->input('locale');
$supportedLocales = array_values(array_unique(array_filter([
config('app.locale'),
config('app.fallback_locale'),
...array_filter(array_map(
static fn ($value) => trim((string) $value),
explode(',', (string) env('APP_SUPPORTED_LOCALES', ''))
)),
])));
$supportedLocales = LocaleConfig::normalized();
$canonical = LocaleConfig::canonicalize($locale);
if (empty($supportedLocales)) {
$supportedLocales = ['de', 'en'];
}
if (in_array($locale, $supportedLocales)) {
App::setLocale($locale);
Session::put('locale', $locale);
Session::put('preferred_locale', $locale);
if (in_array($canonical, $supportedLocales, true)) {
App::setLocale($canonical);
Session::put('locale', $canonical);
Session::put('preferred_locale', $canonical);
}
if ($request->expectsJson()) {

View File

@@ -105,7 +105,7 @@ class MarketingController extends Controller
public function contactView(Request $request)
{
$locale = app()->getLocale();
$locale = \App\Support\LocaleConfig::canonicalize((string) ($request->route('locale') ?? app()->getLocale()));
$secondSegment = $request->segment(2);
$slug = $secondSegment ? '/'.trim((string) $secondSegment, '/') : '/';

View File

@@ -36,6 +36,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\SetLocaleFromRequest::class,
\App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
],

View File

@@ -5,6 +5,7 @@ namespace App\Http\Middleware;
use Illuminate\Foundation\Inspiring;
use Illuminate\Http\Request;
use Inertia\Middleware;
use App\Support\LocaleConfig;
class HandleInertiaRequests extends Middleware
{
@@ -38,19 +39,10 @@ class HandleInertiaRequests extends Middleware
{
[$message, $author] = str(Inspiring::quotes()->random())->explode('-');
$supportedLocales = collect(explode(',', (string) env('APP_SUPPORTED_LOCALES', 'de,en')))
->map(fn ($l) => trim((string) $l))
->filter()
->unique()
->values()
->all();
$supportedLocales = LocaleConfig::normalized();
$defaultLocale = LocaleConfig::canonicalize($supportedLocales[0] ?? null);
if (empty($supportedLocales)) {
$supportedLocales = array_values(array_unique(array_filter([
config('app.locale'),
config('app.fallback_locale'),
])));
}
$currentLocale = LocaleConfig::canonicalize($request->route('locale') ?? $request->segment(1) ?? app()->getLocale());
return [
...parent::share($request),
@@ -60,9 +52,10 @@ class HandleInertiaRequests extends Middleware
'user' => $request->user(),
],
'supportedLocales' => $supportedLocales,
'defaultLocale' => $defaultLocale,
'appUrl' => rtrim(config('app.url'), '/'),
'sidebarOpen' => $request->cookie('sidebar_state', 'false') === 'true',
'locale' => app()->getLocale(),
'locale' => $currentLocale,
'translations' => [
'marketing' => __('marketing'),
'auth' => __('auth'),

View File

@@ -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));
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace App\Support;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class LocaleConfig
{
/**
* Return configured locales from env/config as provided (may include region codes).
* @return array<int, string>
*/
public static function configured(): array
{
$configured = array_filter(array_map(
static fn ($value) => trim((string) $value),
explode(',', (string) env('APP_SUPPORTED_LOCALES', ''))
));
$baseLocales = array_filter([
config('app.locale'),
config('app.fallback_locale'),
]);
$fallback = ['de', 'en'];
return array_values(array_unique([
...$configured,
...$baseLocales,
...$fallback,
]));
}
/**
* Return normalized short codes (language only, lowercase).
* @return array<int, string>
*/
public static function normalized(): array
{
return collect(static::configured())
->map(fn (string $code) => Str::of($code)->lower()->before('-')->before('_')->toString())
->filter()
->unique()
->values()
->all();
}
/**
* Pattern for route constraints: accept both configured and normalized variants.
*/
public static function routePattern(): string
{
$all = collect(array_merge(static::configured(), static::normalized()))
->map(fn (string $code) => preg_quote($code, '/'))
->unique()
->values()
->all();
return implode('|', $all);
}
/**
* Canonicalize any incoming locale to a normalized short code if supported, otherwise fallback.
*/
public static function canonicalize(?string $locale): string
{
$normalized = static::normalized();
$fallback = Arr::first($normalized, default: 'de');
if (! $locale) {
return $fallback;
}
$short = Str::of($locale)->lower()->before('-')->before('_')->toString();
return in_array($short, $normalized, true) ? $short : $fallback;
}
}