2026-05-09 23:14:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
|
|
use Livewire\Component;
|
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
|
|
|
|
|
class LanguageSwitcher extends Component
|
|
|
|
|
{
|
2026-06-16 18:05:53 +02:00
|
|
|
public string $currentLocale;
|
2026-05-09 23:14:48 +02:00
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function mount(): void
|
2026-05-09 23:14:48 +02:00
|
|
|
{
|
|
|
|
|
$this->currentLocale = App::getLocale();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function switchLanguage(string $locale): void
|
2026-05-09 23:14:48 +02:00
|
|
|
{
|
|
|
|
|
if (!in_array($locale, ['en', 'es'])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Session::put('locale', $locale);
|
|
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
$user->locale = $locale;
|
|
|
|
|
$user->save();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
// Dispatch a browser event — JavaScript reloads the page.
|
|
|
|
|
// PHP-side redirects break because $this->redirect() runs inside
|
|
|
|
|
// /livewire/update (the AJAX endpoint), not on the real page URL.
|
|
|
|
|
$this->dispatch('locale-changed');
|
2026-05-09 23:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.language-switcher');
|
|
|
|
|
}
|
|
|
|
|
}
|