2026-05-09 23:14:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-19 16:54:09 +02:00
|
|
|
namespace App\Livewire\Common;
|
2026-05-09 23:14:48 +02:00
|
|
|
|
|
|
|
|
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-08-03 13:44:10 +02:00
|
|
|
/** Idiomas disponibles con nombre y bandera. */
|
|
|
|
|
public array $languages = [
|
|
|
|
|
'en' => ['name' => 'English', 'flag' => 'gb.svg'],
|
|
|
|
|
'es' => ['name' => 'Español', 'flag' => 'es.svg'],
|
|
|
|
|
'fr' => ['name' => 'Français', 'flag' => 'fr.svg'],
|
|
|
|
|
'ru' => ['name' => 'Русский', 'flag' => 'ru.svg'],
|
|
|
|
|
];
|
|
|
|
|
|
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-08-03 13:44:10 +02:00
|
|
|
public function updatedCurrentLocale(string $locale): void
|
2026-05-09 23:14:48 +02:00
|
|
|
{
|
2026-08-03 13:44:10 +02:00
|
|
|
if (!in_array($locale, ['en', 'es', 'fr', 'ru'])) {
|
2026-05-09 23:14:48 +02:00
|
|
|
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.
|
|
|
|
|
$this->dispatch('locale-changed');
|
2026-05-09 23:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-03 13:44:10 +02:00
|
|
|
public function switchLanguage(string $locale): void
|
|
|
|
|
{
|
|
|
|
|
$this->updatedCurrentLocale($locale);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 23:14:48 +02:00
|
|
|
public function render()
|
|
|
|
|
{
|
2026-06-19 16:54:09 +02:00
|
|
|
return view('livewire.common.language-switcher');
|
2026-05-09 23:14:48 +02:00
|
|
|
}
|
|
|
|
|
}
|