Hotfix: Updating LocaleMiddleware to handle more than 2 languages

This commit is contained in:
2026-08-26 13:26:29 +03:00
parent 356fbd73c5
commit c2eb9bd66a
2 changed files with 50 additions and 11 deletions
+27
View File
@@ -105,6 +105,33 @@ $language = $request->attributes->get('language'); // Lunar\Models\Language in
Use `$language->id` when querying Lunar's translatable content (e.g. `Url::where('language_id', ...)`). Use `$language->id` when querying Lunar's translatable content (e.g. `Url::where('language_id', ...)`).
### Shared view data — language switcher and `hreflang` tags
The middleware also shares two variables with every view, via `View::share()`, so a layout's
language switcher or `hreflang` tags don't have to recompute the language list themselves:
```blade
{{-- current locale --}}
{{ $currentLocale }} {{-- e.g. "el" --}}
{{-- every OTHER configured language, each with its own URL for the current page --}}
@foreach ($altLocales as $altLocale)
<a href="{{ $altLocale['url'] }}" hreflang="{{ $altLocale['code'] }}">{{ $altLocale['name'] }}</a>
@endforeach
```
`$altLocales` is a **collection**, not a single value — deliberately, so it scales to any number
of configured languages rather than assuming exactly two. Each entry is a plain array:
| Key | Description |
|---|---|
| `code` | The language's `Lunar\Models\Language::code` (e.g. `en`) |
| `name` | The language's display name |
| `url` | The **current route**, re-generated with that language's code — via `route($routeName, [...])` when the current request matched a named route, or a bare `/{code}` fallback otherwise |
A 3+ language store gets one `$altLocales` entry per additional language automatically — nothing
about this shape assumes or special-cases a two-language store.
--- ---
## Single-language shops ## Single-language shops
+23 -11
View File
@@ -62,24 +62,36 @@ class LocaleMiddleware
} }
/** /**
* Shares the current/alternate locale (and the alternate's URL) with all * Shares the current locale and every OTHER available locale (each with its
* views, so the header language switcher and layout hreflang tags don't * own URL for the current page) with all views, so the header language
* have to recompute it. * switcher and layout hreflang tags don't have to recompute it.
*
* `altLocales` is a collection, not a single value — firstWhere('code', '!=',
* ...) would only ever surface one alternate, which happens to look correct
* with exactly 2 configured languages (there's only one "other" to find) but
* silently drops every locale past the first for a 3+ language store, with no
* error, just fewer switcher options than actually configured. A view iterates
* `$altLocales` to render as many links/dropdown entries as there are
* alternates, whether that's 1 or 10.
*/ */
private function shareLocaleViewData(Request $request, Language $language, Collection $languages): void private function shareLocaleViewData(Request $request, Language $language, Collection $languages): void
{ {
$altLanguage = $languages->firstWhere('code', '!=', $language->code);
$route = $request->route(); $route = $request->route();
$routeName = $route?->getName(); $routeName = $route?->getName();
$altLocales = $languages
->reject(fn (Language $other) => $other->code === $language->code)
->map(fn (Language $other) => [
'code' => $other->code,
'name' => $other->name,
'url' => $routeName
? route($routeName, array_merge($route->parameters(), ['locale' => $other->code]))
: url('/'.$other->code),
])
->values();
View::share('currentLocale', $language->code); View::share('currentLocale', $language->code);
View::share('altLocale', $altLanguage?->code); View::share('altLocales', $altLocales);
View::share(
'altLocaleUrl',
$altLanguage && $routeName
? route($routeName, array_merge($route->parameters(), ['locale' => $altLanguage->code]))
: ($altLanguage ? url('/'.$altLanguage->code) : null),
);
} }
private function redirectToLocalizedUrl(Request $request, Collection $languages): Response private function redirectToLocalizedUrl(Request $request, Collection $languages): Response