From b17a4a7579135bd4dc2e407ce12e38d5cce1f346 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Mon, 17 Apr 2023 18:02:04 +0430 Subject: [PATCH] fixes #20, #22 and #24 --- composer.json | 2 +- resources/views/language-switch.blade.php | 14 ++++++------ src/FilamentLanguageSwitchServiceProvider.php | 22 +++++++++++++------ src/Http/Livewire/SwitchFilamentLanguage.php | 3 +++ src/Http/Middleware/SwitchLanguageLocale.php | 4 ++-- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index 3d7a40d..369d8a4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ } ], "require": { - "php": "^8.0|^8.1", + "php": "^8.0|^8.1|^8.2", "filament/filament": "^2.0", "spatie/laravel-package-tools": "^1.9.2", "illuminate/contracts": "^8.0|^9.0|^10.0" diff --git a/resources/views/language-switch.blade.php b/resources/views/language-switch.blade.php index 82c84cc..1c8df57 100644 --- a/resources/views/language-switch.blade.php +++ b/resources/views/language-switch.blade.php @@ -11,10 +11,10 @@ 'mr-4' => __('filament::layout.direction') === 'rtl', ])>
- {{ str(app()->getLocale())->length() > 2 - ? str(app()->getLocale())->substr(0, 2)->upper() - : str(app()->getLocale())->upper() }} + class="flex items-center justify-center w-10 h-10 font-semibold text-white bg-center bg-cover rounded-full language-switch-trigger bg-primary-500 dark:bg-gray-900"> + {{ \Illuminate\Support\Str::of(app()->getLocale())->length() > 2 + ? \Illuminate\Support\Str::of(app()->getLocale())->substr(0, 2)->upper() + : \Illuminate\Support\Str::of(app()->getLocale())->upper() }}
@@ -31,13 +31,13 @@ class="flex-shrink-0 w-5 h-5 mr-4 rtl:ml-4 group-hover:text-white group-focus:te @else - {{ str($locale['name'])->snake()->upper()->explode('_')->map(function ($string) use ($locale) { - return str($locale['name'])->wordCount() > 1 ? str()->substr($string, 0, 1) : str()->substr($string, 0, 2); + {{ \Illuminate\Support\Str::of($locale['name'])->snake()->upper()->explode('_')->map(function ($string) use ($locale) { + return \Illuminate\Support\Str::of($locale['name'])->wordCount() > 1 ? \Illuminate\Support\Str::substr($string, 0, 1) : \Illuminate\Support\Str::substr($string, 0, 2); })->take(2)->implode('') }} @endif - {{ str($locale[config('filament-language-switch.native') ? 'native' : 'name'])->headline() }} + {{ \Illuminate\Support\Str::of($locale[config('filament-language-switch.native') ? 'native' : 'name'])->headline() }} @endif diff --git a/src/FilamentLanguageSwitchServiceProvider.php b/src/FilamentLanguageSwitchServiceProvider.php index bc32e1c..a240ba3 100644 --- a/src/FilamentLanguageSwitchServiceProvider.php +++ b/src/FilamentLanguageSwitchServiceProvider.php @@ -2,10 +2,11 @@ namespace BezhanSalleh\FilamentLanguageSwitch; -use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale; use Filament\Facades\Filament; use Filament\PluginServiceProvider; use Spatie\LaravelPackageTools\Package; +use Filament\Http\Middleware\DispatchServingFilamentEvent; +use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale; class FilamentLanguageSwitchServiceProvider extends PluginServiceProvider { @@ -31,12 +32,19 @@ public function packageBooted(): void public function registerSwitchLanguageMiddleware(): void { - if (! array_key_exists( - $key = SwitchLanguageLocale::class, - $filamentMiddlewares = config('filament.middleware.base') - )) { - $filamentMiddlewares[] = $key; - config(['filament.middleware.base' => $filamentMiddlewares]); + $middlewareStack = config('filament.middleware.base'); + $switchLanguageIndex = array_search(SwitchLanguageLocale::class, $middlewareStack); + $dispatchServingFilamentEventIndex = array_search(DispatchServingFilamentEvent::class, $middlewareStack); + + if ($switchLanguageIndex === false || $switchLanguageIndex > $dispatchServingFilamentEventIndex) { + + $middlewareStack = array_filter($middlewareStack, function ($middleware) { + return $middleware !== SwitchLanguageLocale::class; + }); + + array_splice($middlewareStack, $dispatchServingFilamentEventIndex, 0, [SwitchLanguageLocale::class]); + + config(['filament.middleware.base' => $middlewareStack]); } } } diff --git a/src/Http/Livewire/SwitchFilamentLanguage.php b/src/Http/Livewire/SwitchFilamentLanguage.php index f28f7aa..c224fe6 100644 --- a/src/Http/Livewire/SwitchFilamentLanguage.php +++ b/src/Http/Livewire/SwitchFilamentLanguage.php @@ -3,6 +3,7 @@ namespace BezhanSalleh\FilamentLanguageSwitch\Http\Livewire; use Illuminate\Contracts\View\View; +use Illuminate\Support\Str; use Livewire\Component; class SwitchFilamentLanguage extends Component @@ -11,6 +12,8 @@ public function changeLocale($locale) { session()->put('locale', $locale); + cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale)); + $this->redirect(request()->header('Referer')); } diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index 482173e..2a266df 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -4,19 +4,19 @@ use Closure; use Illuminate\Http\Request; +use Illuminate\Support\Str; class SwitchLanguageLocale { /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse */ public function handle(Request $request, Closure $next) { - $locale = session()->get('locale') ?? $request->get('locale') ?? config('app.locale', 'en'); + $locale = session()->get('locale') ?? $request->get('locale') ?? $request->cookie('filament_language_switch_locale') ?? config('app.locale', 'en'); if (array_key_exists($locale, config('filament-language-switch.locales'))) { app()->setLocale($locale);