From 2aa1b359b5aa8159362850f44909b56260899d61 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Tue, 9 Jan 2024 10:49:19 +0430 Subject: [PATCH 1/2] done --- src/Events/LocaleChanged.php | 16 +++------------- src/Http/Middleware/SwitchLanguageLocale.php | 14 +++++++++----- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Events/LocaleChanged.php b/src/Events/LocaleChanged.php index 41034cd..9cb8575 100644 --- a/src/Events/LocaleChanged.php +++ b/src/Events/LocaleChanged.php @@ -2,20 +2,10 @@ namespace BezhanSalleh\FilamentLanguageSwitch\Events; -use Illuminate\Broadcasting\InteractsWithSockets; -use Illuminate\Foundation\Events\Dispatchable; -use Illuminate\Queue\SerializesModels; - class LocaleChanged { - use Dispatchable; - use InteractsWithSockets; - use SerializesModels; - - /** - * Create a new event instance. - */ - public function __construct(public string $locale) - { + public function __construct( + public string $locale + ){ } } diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index d20050b..f5cf294 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -25,12 +25,16 @@ public function handle(Request $request, Closure $next): \Illuminate\Http\Respon private function getBrowserLocale(Request $request): ?string { - $userLangs = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE')); + $appLocales = LanguageSwitch::make()->getLocales(); + + $userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE')); - foreach ($userLangs as $locale) { - return in_array($locale, LanguageSwitch::make()->getLocales()) - ? $locale - : null; + foreach ($userLocales as $locale) { + if (in_array($locale, $appLocales)) { + return $locale; + } } + + return null; } } From 993e4d360befdf3f37671c4305f32b917bc518f8 Mon Sep 17 00:00:00 2001 From: bezhanSalleh Date: Tue, 9 Jan 2024 06:19:40 +0000 Subject: [PATCH 2/2] Fix styling --- src/Events/LocaleChanged.php | 2 +- src/Http/Middleware/SwitchLanguageLocale.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Events/LocaleChanged.php b/src/Events/LocaleChanged.php index 9cb8575..e518f30 100644 --- a/src/Events/LocaleChanged.php +++ b/src/Events/LocaleChanged.php @@ -6,6 +6,6 @@ class LocaleChanged { public function __construct( public string $locale - ){ + ) { } } diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index f5cf294..5ea7c08 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -26,7 +26,7 @@ public function handle(Request $request, Closure $next): \Illuminate\Http\Respon private function getBrowserLocale(Request $request): ?string { $appLocales = LanguageSwitch::make()->getLocales(); - + $userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE')); foreach ($userLocales as $locale) {