From effed0925808a09ca1a2fbc781b6c259eaa36268 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 25 Jan 2024 04:36:12 +0430 Subject: [PATCH] wip --- src/Http/Middleware/SwitchLanguageLocale.php | 27 ++------------ src/LanguageSwitch.php | 37 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index 5ea7c08..bebdbe3 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -10,31 +10,10 @@ class SwitchLanguageLocale { public function handle(Request $request, Closure $next): \Illuminate\Http\Response | \Illuminate\Http\RedirectResponse { - $locale = session()->get('locale') - ?? $request->get('locale') - ?? $request->cookie('filament_language_switch_locale') - ?? $this->getBrowserLocale($request) - ?? config('app.locale', 'en'); - - if (in_array($locale, LanguageSwitch::make()->getLocales())) { - app()->setLocale($locale); - } + app()->setLocale( + LanguageSwitch::make()->getPreferredLocale() + ); return $next($request); } - - private function getBrowserLocale(Request $request): ?string - { - $appLocales = LanguageSwitch::make()->getLocales(); - - $userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE')); - - foreach ($userLocales as $locale) { - if (in_array($locale, $appLocales)) { - return $locale; - } - } - - return null; - } } diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index f72f045..1547c6a 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -38,6 +38,11 @@ class LanguageSwitch extends Component protected Closure | string $renderHook = 'panels::global-search.after'; + protected Closure | string | null $userPreferredLocale = null; + + protected Closure | string | null $preferredLocale = null; + + public static function make(): static { $static = app(static::class); @@ -146,6 +151,20 @@ public function renderHook(string $hook): static return $this; } + public function userPreferredLocale(Closure | string | null $locale): static + { + $this->userPreferredLocale = $locale; + + 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; @@ -229,6 +248,24 @@ public function getRenderHook(): string return (string) $this->evaluate($this->renderHook); } + public function getUserPreferredLocale(): ?string + { + return $this->evaluate($this->userPreferredLocale) ?? null; + } + + public function getPreferredLocale(): string + { + $locale = $this->perferredLocale ?? + session()->get('locale') ?? + request()->get('locale') ?? + request()->cookie('filament_language_switch_locale') ?? + $this->getUserPreferredLocale() ?? + config('app.locale', 'en') ?? + request()->getPreferredLanguage(); + + return in_array($locale, $this->getLocales(), true) ? $locale : config('app.locale'); + } + /** * @return array */