From 9a8ecb22d42b36071966beb35da22ef2569abe00 Mon Sep 17 00:00:00 2001 From: alkoumi Date: Wed, 10 Jan 2024 18:40:17 +0300 Subject: [PATCH 1/2] get the default locale from laravel config before browser --- src/Http/Middleware/SwitchLanguageLocale.php | 79 ++++++++++---------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index 5ea7c08..af324f8 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -1,40 +1,41 @@ 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); - } - - 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; - } -} + + namespace BezhanSalleh\FilamentLanguageSwitch\Http\Middleware; + + use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch; + use Closure; + use Illuminate\Http\RedirectResponse; + use Illuminate\Http\Request; + use Illuminate\Http\Response; + + class SwitchLanguageLocale + { + public function handle(Request $request , Closure $next): Response|RedirectResponse + { + $locale = session()->get('locale') + ?? $request->get('locale') + ?? $request->cookie('filament_language_switch_locale') + ?? config('app.locale' , 'en') + ?? $this->getBrowserLocale($request); + if (in_array($locale , LanguageSwitch::make()->getLocales())) { + app()->setLocale($locale); + } + + 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; + } + } From 8c1d623b3e835e5119c7847c9b14fe6ffb956e72 Mon Sep 17 00:00:00 2001 From: alkoumi Date: Wed, 10 Jan 2024 19:26:22 +0300 Subject: [PATCH 2/2] get the default locale from laravel config before browser --- src/Http/Middleware/SwitchLanguageLocale.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index af324f8..d104652 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -17,6 +17,7 @@ public function handle(Request $request , Closure $next): Response|RedirectRespo ?? $request->cookie('filament_language_switch_locale') ?? config('app.locale' , 'en') ?? $this->getBrowserLocale($request); + if (in_array($locale , LanguageSwitch::make()->getLocales())) { app()->setLocale($locale); }