Skip to content

Commit

Permalink
handling user preferred locale
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Jan 25, 2024
1 parent effed09 commit 1426d0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
14 changes: 4 additions & 10 deletions src/Http/Livewire/FilamentLanguageSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

namespace BezhanSalleh\FilamentLanguageSwitch\Http\Livewire;

use BezhanSalleh\FilamentLanguageSwitch\Events\LocaleChanged;
use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Component;

class FilamentLanguageSwitch extends Component
{
#[On('language-switched')]
public function changeLocale($locale)
{
session()->put('locale', $locale);

cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale));

$this->dispatch('filament-language-changed');

event(new LocaleChanged($locale));

$this->redirect(request()->header('Referer'));
LanguageSwitch::trigger($locale);

}

Expand Down
25 changes: 13 additions & 12 deletions src/LanguageSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BezhanSalleh\FilamentLanguageSwitch;

use BezhanSalleh\FilamentLanguageSwitch\Enums\Placement;
use BezhanSalleh\FilamentLanguageSwitch\Events\LocaleChanged;
use Closure;
use Exception;
use Filament\Panel;
Expand Down Expand Up @@ -40,9 +41,6 @@ class LanguageSwitch extends Component

protected Closure | string | null $userPreferredLocale = null;

protected Closure | string | null $preferredLocale = null;


public static function make(): static
{
$static = app(static::class);
Expand Down Expand Up @@ -158,13 +156,6 @@ public function userPreferredLocale(Closure | string | null $locale): static
return $this;
}

public function preferredLocale(Closure | string $locale): static
{
$this->preferredLocale = $locale;

return $this;
}

public function visible(bool | Closure $insidePanels = true, bool | Closure $outsidePanels = false): static
{
$this->visibleInsidePanels = $insidePanels;
Expand Down Expand Up @@ -255,8 +246,7 @@ public function getUserPreferredLocale(): ?string

public function getPreferredLocale(): string
{
$locale = $this->perferredLocale ??
session()->get('locale') ??
$locale = session()->get('locale') ??
request()->get('locale') ??
request()->cookie('filament_language_switch_locale') ??
$this->getUserPreferredLocale() ??
Expand Down Expand Up @@ -304,4 +294,15 @@ public function getCharAvatar(string $locale): string
? str($locale)->substr(0, 2)->upper()->toString()
: str($locale)->upper()->toString();
}

public static function trigger($locale)
{
session()->put('locale', $locale);

cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale));

event(new LocaleChanged($locale));

return redirect(request()->header('Referer'));
}
}

0 comments on commit 1426d0e

Please sign in to comment.