From c3bef1580d79652457cd109f5da800039a2778d5 Mon Sep 17 00:00:00 2001 From: daurensky Date: Thu, 15 Feb 2024 17:45:58 +0600 Subject: [PATCH] fix: change widget parent and `emit` to `dispatch` --- resources/lang/en/localizations.php | 3 +- resources/lang/ru/localizations.php | 3 +- .../widgets/localization-files.blade.php | 44 +++++++++++-------- src/Traits/LocalizationFiles.php | 24 ++++++++++ .../Resources/LocalizationResource.php | 13 +++--- .../Pages/CreateLocalization.php | 30 ++++--------- .../Pages/EditLocalization.php | 30 ++++--------- .../Pages/ListLocalization.php | 19 +++++--- .../Widgets/LocalizationInformer.php | 6 +-- 9 files changed, 92 insertions(+), 80 deletions(-) create mode 100644 src/Traits/LocalizationFiles.php diff --git a/resources/lang/en/localizations.php b/resources/lang/en/localizations.php index c60f50b..eff4f0f 100644 --- a/resources/lang/en/localizations.php +++ b/resources/lang/en/localizations.php @@ -16,7 +16,6 @@ 'localization_files_saved' => 'Localization files saved successfully', ], - 'translation_file' => 'translation file', - 'count_keys' => 'has :count key|has :count keys', + 'count_keys' => ':count key|:count keys', 'not_created' => 'not created', ]; diff --git a/resources/lang/ru/localizations.php b/resources/lang/ru/localizations.php index c61f888..d75b71b 100644 --- a/resources/lang/ru/localizations.php +++ b/resources/lang/ru/localizations.php @@ -16,7 +16,6 @@ 'localization_files_saved' => 'Файлы локализаций успешно сохранены', ], - 'translation_file' => 'файл перевода', - 'count_keys' => 'имеет :count ключ|имеет :count ключа|имеет :count ключей', + 'count_keys' => ':count ключ|:count ключа|:count ключей', 'not_created' => 'не создан', ]; diff --git a/resources/views/widgets/localization-files.blade.php b/resources/views/widgets/localization-files.blade.php index 3674819..5a05a4f 100644 --- a/resources/views/widgets/localization-files.blade.php +++ b/resources/views/widgets/localization-files.blade.php @@ -1,20 +1,28 @@ - - {{-- Widget content --}} - @foreach(AdminKit::locales() as $locale) - {{Str::upper($locale)}} - {{__('admin-kit-localizations::localizations.translation_file')}} - @if ($exists[$locale]) - {{$locale}}.json - ({{$sizes[$locale]}}), {{$counts[$locale]}}. - @else - {{$locale}}.json {{__('admin-kit-localizations::localizations.not_created')}} - @endif -
- @endforeach -
+ +
+ @foreach(AdminKit::locales() as $locale) +
+ + + @if ($exists[$locale]) + {{$counts[$locale]}} + @else + {{__('admin-kit-localizations::localizations.not_created')}} + @endif + + + + {{$exists[$locale] ? $sizes[$locale] : '0.00 Kb'}} + + {{$locale}}.json +
+ @endforeach +
+
- - diff --git a/src/Traits/LocalizationFiles.php b/src/Traits/LocalizationFiles.php new file mode 100644 index 0000000..f491da0 --- /dev/null +++ b/src/Traits/LocalizationFiles.php @@ -0,0 +1,24 @@ +actions([ Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make(), ]) ->bulkActions([ Tables\Actions\DeleteBulkAction::make(), diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php index c1ff553..fdeb34d 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php @@ -2,15 +2,17 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; -use AdminKit\Core\Facades\AdminKit; -use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Resources\Pages\CreateRecord; +use AdminKit\Localizations\Traits\LocalizationFiles; +use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; class CreateLocalization extends CreateRecord { + use LocalizationFiles; + protected static string $resource = LocalizationResource::class; - protected function getActions(): array + protected function getHeaderActions(): array { return [ // @@ -24,23 +26,9 @@ protected function getRedirectUrl(): string public function beforeCreate(): void { - $this->updateLocalizationFile(); - } - - protected function updateLocalizationFile(): void - { - foreach (AdminKit::locales() as $locale) { - $key = $this->data['key']; - $value = $this->data['content'][$locale]; - $path = lang_path($locale.'.json'); - - if (! file_exists($path)) { - file_put_contents($path, json_encode([$key => $value], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); - } else { - $jsonContent = json_decode(file_get_contents($path), true); - $jsonContent[$key] = $value; - file_put_contents($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); - } - } + $this->addLocalization( + $this->data['key'], + $this->data['content'] + ); } } diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php index bc41536..20cf3c2 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php @@ -2,13 +2,15 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; -use AdminKit\Core\Facades\AdminKit; -use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use AdminKit\Localizations\Traits\LocalizationFiles; +use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; class EditLocalization extends EditRecord { + use LocalizationFiles; + protected static string $resource = LocalizationResource::class; protected function getHeaderActions(): array @@ -18,25 +20,11 @@ protected function getHeaderActions(): array ]; } - protected function beforeSave() + protected function beforeSave(): void { - $this->updateLocalizationFile(); - } - - protected function updateLocalizationFile(): void - { - foreach (AdminKit::locales() as $locale) { - $key = $this->data['key']; - $value = $this->data['content'][$locale]; - $path = lang_path("$locale.json"); - - if (! file_exists($path)) { - file_put_contents($path, json_encode([$key => $value], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); - } else { - $jsonContent = json_decode(file_get_contents($path), true); - $jsonContent[$key] = $value; - file_put_contents($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); - } - } + $this->addLocalization( + $this->data['key'], + $this->data['content'] + ); } } diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php index 0094f6a..b5285a3 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php @@ -2,14 +2,15 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; +use Filament\Actions; +use Filament\Actions\Action; use AdminKit\Core\Facades\AdminKit; +use Illuminate\Support\Facades\File; +use Filament\Notifications\Notification; +use Filament\Resources\Pages\ListRecords; use AdminKit\Localizations\Models\Localization; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer; -use Filament\Actions; -use Filament\Notifications\Notification; -use Filament\Resources\Pages\ListRecords; -use Illuminate\Support\Facades\File; class ListLocalization extends ListRecords { @@ -33,16 +34,20 @@ protected function getHeaderActions(): array ]; } - public function publish() + public function publish(): void { $localizations = Localization::query() ->select('key', 'content') ->orderBy('id') ->get(); + foreach (AdminKit::locales() as $locale) { $path = lang_path("$locale.json"); + $jsonContent = $localizations - ->mapWithKeys(fn ($value, $key) => [$value->key => $value->getTranslation('content', $locale)]) + ->mapWithKeys(fn ($value, $key) => [ + $value->key => $value->getTranslation('content', $locale), + ]) ->toArray(); File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); @@ -53,6 +58,6 @@ public function publish() ->success() ->send(); - $this->emit('refreshLocalizationInformer'); // livewire event + $this->dispatch('refreshLocalizationInformer'); // livewire event } } diff --git a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php index e022cf0..f10a501 100644 --- a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php +++ b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php @@ -2,13 +2,13 @@ namespace AdminKit\Localizations\UI\Filament\Resources\Widgets; +use Filament\Widgets\Widget; use AdminKit\Core\Facades\AdminKit; -use Filament\Widgets\TableWidget; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; -class LocalizationInformer extends TableWidget +class LocalizationInformer extends Widget { protected int|string|array $columnSpan = 2; @@ -22,7 +22,7 @@ public function render(): View $exists[$locale] = File::exists($path); if ($exists[$locale]) { - $sizes[$locale] = number_format(File::size($path) / 1024, 2).' Kb'; + $sizes[$locale] = number_format(File::size($path) / 1024, 2) . ' Kb'; } if ($exists[$locale]) {