Files
construprogress/app/Livewire/LanguageSwitcher.php
T

43 lines
1.0 KiB
PHP
Raw Normal View History

<?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
{
public string $currentLocale;
public function mount(): void
{
$this->currentLocale = App::getLocale();
}
public function switchLanguage(string $locale): void
{
if (!in_array($locale, ['en', 'es'])) {
return;
}
Session::put('locale', $locale);
if (Auth::check()) {
$user = Auth::user();
$user->locale = $locale;
$user->save();
}
// 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');
}
public function render()
{
return view('livewire.language-switcher');
}
}