Skip to content

Commit bfa441f

Browse files
Switch locale via route and sync config
Replace Livewire-only locale switching with a dedicated GET route and session persistence. The locale switcher blade now uses anchor links to route('locale.switch') (with wire:key) instead of invoking a Livewire button. A new /locale/{locale} route stores the chosen locale in session and redirects back. The Livewire component was updated to use session()->put()/save() and return a redirect based on the Referer header. Middleware now also sets config('app.locale') when setting App::setLocale so components read the correct locale.
1 parent 1bf28f8 commit bfa441f

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

modules/Core/src/Http/Livewire/LocaleSwitcher.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ public function switchLocale(string $locale)
2626
return;
2727
}
2828

29-
Session::put('locale', $locale);
30-
App::setLocale($locale);
31-
$this->currentLocale = $locale;
29+
session()->put('locale', $locale);
30+
session()->save();
3231

33-
$this->redirect(url()->previous(fallback: '/'), navigate: false);
32+
return redirect(request()->header('Referer', '/'));
3433
}
3534

3635
public function render()

modules/Core/src/Http/Middleware/SetLocale.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function handle(Request $request, Closure $next): Response
2121

2222
if (in_array($locale, ['en', 'hi', 'ar'])) {
2323
App::setLocale($locale);
24+
config(['app.locale' => $locale]); // Sync config to ensure components pick it up
2425
}
2526

2627
return $next($request);

modules/Core/views/livewire/locale-switcher.blade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class="absolute {{ app()->getLocale() == 'ar' ? 'left-0' : 'right-0' }} mt-2 w-4
2323
>
2424
<div class="py-1">
2525
@foreach($availableLocales as $locale => $details)
26-
<button
27-
wire:click="switchLocale('{{ $locale }}')"
26+
<a
27+
href="{{ route('locale.switch', $locale) }}"
28+
wire:key="locale-{{ $locale }}"
2829
class="w-full text-{{ app()->getLocale() == 'ar' ? 'right' : 'left' }} flex items-center justify-between px-4 py-2 text-sm hover:bg-gray-100 dark:hover:bg-gray-700 transition {{ $currentLocale === $locale ? 'bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300' }}"
2930
>
3031
<span>{{ $details['native'] }}</span>
@@ -33,7 +34,7 @@ class="w-full text-{{ app()->getLocale() == 'ar' ? 'right' : 'left' }} flex item
3334
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
3435
</svg>
3536
@endif
36-
</button>
37+
</a>
3738
@endforeach
3839
</div>
3940
</div>

routes/web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
use Modules\Docs\Http\Controllers\DocsController;
55
use Modules\Core\Http\Livewire\Changelog;
66

7+
Route::get('/locale/{locale}', function (string $locale) {
8+
if (in_array($locale, ['en', 'hi', 'ar'])) {
9+
session()->put('locale', $locale);
10+
}
11+
return redirect()->back();
12+
})->name('locale.switch');
13+
714
Route::get('/', Modules\Core\Http\Livewire\Home::class)->name('home');
815

916
Route::get('/changelog', Changelog::class)->name('changelog');

0 commit comments

Comments
 (0)