From 4dc6213a0b30705726252f98edf8535a1c13d4d0 Mon Sep 17 00:00:00 2001 From: buzkall Date: Wed, 8 Jan 2025 10:52:03 +0100 Subject: [PATCH 1/3] Add locale to ExportFinishedEvent Use event's locale to localise database notification strings --- src/Events/ExportFinishedEvent.php | 1 + src/Exports/ExcelExport.php | 3 ++- src/FilamentExcelServiceProvider.php | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Events/ExportFinishedEvent.php b/src/Events/ExportFinishedEvent.php index 8b2ef93..74b099b 100644 --- a/src/Events/ExportFinishedEvent.php +++ b/src/Events/ExportFinishedEvent.php @@ -11,6 +11,7 @@ class ExportFinishedEvent public function __construct( public string $filename, public int|string|null $userId, + public string $locale, ) { // } diff --git a/src/Exports/ExcelExport.php b/src/Exports/ExcelExport.php index 12f961d..4c60273 100644 --- a/src/Exports/ExcelExport.php +++ b/src/Exports/ExcelExport.php @@ -222,10 +222,11 @@ public function export() $filename = Str::uuid().'-'.$this->getFilename(); $userId = auth()->id(); + $locale = app()->getLocale(); $this ->queueExport($filename, 'filament-excel', $this->getWriterType()) - ->chain([fn () => ExportFinishedEvent::dispatch($filename, $userId)]); + ->chain([fn () => ExportFinishedEvent::dispatch($filename, $userId, $locale)]); Notification::make() ->title(__('filament-excel::notifications.queued.title')) diff --git a/src/FilamentExcelServiceProvider.php b/src/FilamentExcelServiceProvider.php index 26caa6b..a12d50a 100644 --- a/src/FilamentExcelServiceProvider.php +++ b/src/FilamentExcelServiceProvider.php @@ -67,6 +67,8 @@ public function sendExportFinishedNotification(): void } if (Filament::getCurrentPanel()->hasDatabaseNotifications()) { + app()->setLocale($export['locale']); + Notification::make(data_get($export, 'id')) ->title(__('filament-excel::notifications.download_ready.title')) ->body(__('filament-excel::notifications.download_ready.body')) @@ -112,6 +114,7 @@ public function cacheExportFinishedNotification(ExportFinishedEvent $event): voi 'id' => Str::uuid(), 'filename' => $event->filename, 'userId' => $event->userId, + 'locale' => $event->locale, ]; cache()->put($key, $exports); From 325fc76b99fa7055ca5da20bd7a953cc7a63d5a8 Mon Sep 17 00:00:00 2001 From: buzkall Date: Thu, 16 Jan 2025 17:36:08 +0100 Subject: [PATCH 2/3] Save previous locale --- src/FilamentExcelServiceProvider.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/FilamentExcelServiceProvider.php b/src/FilamentExcelServiceProvider.php index a12d50a..7199d90 100644 --- a/src/FilamentExcelServiceProvider.php +++ b/src/FilamentExcelServiceProvider.php @@ -67,6 +67,7 @@ public function sendExportFinishedNotification(): void } if (Filament::getCurrentPanel()->hasDatabaseNotifications()) { + $previousLocale = App::getLocale(); app()->setLocale($export['locale']); Notification::make(data_get($export, 'id')) @@ -82,6 +83,8 @@ public function sendExportFinishedNotification(): void ->close(), ]) ->sendToDatabase(auth()->user()); + + app()->setLocale($previousLocale); } else { Notification::make(data_get($export, 'id')) ->title(__('filament-excel::notifications.download_ready.title')) From 41c5df0311596e154c0a9701e3b04ee777d01257 Mon Sep 17 00:00:00 2001 From: Dennis Koch Date: Sat, 25 Jan 2025 19:36:03 +0700 Subject: [PATCH 3/3] Safely handle new array key --- src/FilamentExport.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FilamentExport.php b/src/FilamentExport.php index c7b5241..3763870 100644 --- a/src/FilamentExport.php +++ b/src/FilamentExport.php @@ -21,7 +21,10 @@ public static function createExportUrlUsing(Closure $closure): void protected function sendDatabaseNotification(array $export, string $url): void { $previousLocale = app()->getLocale(); - app()->setLocale($export['locale']); + + if (isset($export['locale'])) { + app()->setLocale($export['locale']); + } Notification::make(data_get($export, 'id')) ->title(__('filament-excel::notifications.download_ready.title'))