From f0a86e186e39529bd590cf25771f3bdb92608b7c Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Thu, 18 Jul 2024 15:58:37 +0900 Subject: [PATCH 1/3] feat: add caching logic --- src/Actions/LocalizationCacheForgetAction.php | 20 ++++++++++ src/LocalizationsServiceProvider.php | 12 ++++++ src/Models/Localization.php | 18 +++++++++ .../Controllers/LocalizationController.php | 37 ++++++++++++++++--- src/UI/API/Data/LocalizationData.php | 20 +--------- src/UI/API/Data/LocalizationFullData.php | 13 +++++++ .../CachedLocalizationRepository.php | 32 ++++++++++++++++ .../Repositories/LocalizationRepository.php | 35 ++++++++++++++++++ .../LocalizationRepositoryInterface.php | 12 ++++++ src/UI/API/Routes/api.php | 5 ++- 10 files changed, 178 insertions(+), 26 deletions(-) create mode 100644 src/Actions/LocalizationCacheForgetAction.php create mode 100644 src/UI/API/Data/LocalizationFullData.php create mode 100644 src/UI/API/Repositories/CachedLocalizationRepository.php create mode 100644 src/UI/API/Repositories/LocalizationRepository.php create mode 100644 src/UI/API/Repositories/LocalizationRepositoryInterface.php diff --git a/src/Actions/LocalizationCacheForgetAction.php b/src/Actions/LocalizationCacheForgetAction.php new file mode 100644 index 0000000..699ceb0 --- /dev/null +++ b/src/Actions/LocalizationCacheForgetAction.php @@ -0,0 +1,20 @@ +registerConfigs(); } + public function bootingPackage() + { + $repository = match ((bool) config('admin-kit.cache.enabled')) { + true => CachedLocalizationRepository::class, + false => LocalizationRepository::class, + }; + $this->app->bind(LocalizationRepositoryInterface::class, $repository); + } + protected function registerConfigs(): self { $this->mergeConfigFrom(__DIR__.'/../config/filesystems_disks.php', 'filesystems.disks'); diff --git a/src/Models/Localization.php b/src/Models/Localization.php index aac63e0..a334c01 100644 --- a/src/Models/Localization.php +++ b/src/Models/Localization.php @@ -3,6 +3,7 @@ namespace AdminKit\Localizations\Models; use AdminKit\Core\Abstracts\Models\AbstractModel; +use AdminKit\Localizations\Actions\LocalizationCacheForgetAction; use AdminKit\Localizations\Database\Factories\LocalizationFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Spatie\Translatable\HasTranslations; @@ -22,6 +23,23 @@ class Localization extends AbstractModel protected $table = 'admin_kit_localizations'; + protected static function boot() + { + parent::boot(); + + self::created(function () { + app(LocalizationCacheForgetAction::class)->run(); + }); + + self::updated(function () { + app(LocalizationCacheForgetAction::class)->run(); + }); + + self::deleted(function () { + app(LocalizationCacheForgetAction::class)->run(); + }); + } + protected static function newFactory(): LocalizationFactory { return new LocalizationFactory(); diff --git a/src/UI/API/Controllers/LocalizationController.php b/src/UI/API/Controllers/LocalizationController.php index 91d0ec3..77a7e63 100644 --- a/src/UI/API/Controllers/LocalizationController.php +++ b/src/UI/API/Controllers/LocalizationController.php @@ -4,18 +4,45 @@ namespace AdminKit\Localizations\UI\API\Controllers; -use AdminKit\Localizations\Models\Localization; +use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\UI\API\Data\LocalizationData; +use AdminKit\Localizations\UI\API\Data\LocalizationFullData; +use AdminKit\Localizations\UI\API\Repositories\LocalizationRepositoryInterface; +use Illuminate\Http\Request; +use Illuminate\Validation\Rule; +use Spatie\LaravelData\DataCollection; +/** + * @tags Локализация + */ class LocalizationController extends Controller { - public function index() + public function __construct( + private readonly LocalizationRepositoryInterface $repository, + ) {} + + /** + * Получить все переводы + */ + public function getFullList(Request $request) { - return LocalizationData::collection(Localization::all()); + /** @deprecated in next major version, use `getLocaledList` */ + if ($locale = $request->input('locale')) { + $request->validate([ + 'locale' => ['nullable', Rule::in(AdminKit::locales())], + ]); + + return LocalizationData::collect($this->repository->getList($locale), DataCollection::class); + } + + return LocalizationFullData::collect($this->repository->getFullList(), DataCollection::class); } - public function show(int $id) + /** + * Получить переводы на нужном языке + */ + public function getLocaledList($locale) { - return Localization::findOrFail($id); + return LocalizationData::collect($this->repository->getList($locale), DataCollection::class); } } diff --git a/src/UI/API/Data/LocalizationData.php b/src/UI/API/Data/LocalizationData.php index 5d37ab0..9b4c246 100644 --- a/src/UI/API/Data/LocalizationData.php +++ b/src/UI/API/Data/LocalizationData.php @@ -2,30 +2,12 @@ namespace AdminKit\Localizations\UI\API\Data; -use AdminKit\Localizations\Models\Localization; -use Spatie\LaravelData\Concerns\WithDeprecatedCollectionMethod; use Spatie\LaravelData\Data; class LocalizationData extends Data { - use WithDeprecatedCollectionMethod; - public function __construct( public string $key, - public array|string $content, + public string $content, ) {} - - public static function fromModel(Localization $localization): LocalizationData - { - $content = $localization->getTranslations('content'); - - if ($locale = request('locale')) { - $content = $content[$locale] ?? $content; - } - - return new self( - key: $localization->key, - content: $content, - ); - } } diff --git a/src/UI/API/Data/LocalizationFullData.php b/src/UI/API/Data/LocalizationFullData.php new file mode 100644 index 0000000..f1342ae --- /dev/null +++ b/src/UI/API/Data/LocalizationFullData.php @@ -0,0 +1,13 @@ +repository->getFullList(); + }); + } + + public function getList($locale): array + { + $key = "ak_localizations_$locale"; + + return Cache::remember($key, config('admin-kit.cache.ttl'), function () use ($locale) { + return $this->repository->getList($locale); + }); + } +} diff --git a/src/UI/API/Repositories/LocalizationRepository.php b/src/UI/API/Repositories/LocalizationRepository.php new file mode 100644 index 0000000..c66d508 --- /dev/null +++ b/src/UI/API/Repositories/LocalizationRepository.php @@ -0,0 +1,35 @@ +model() + ->select(['key', 'content']) + ->get() + ->toArray(); + } + + public function getList($locale): array + { + return $this->model() + ->selectRaw("key, content->'$locale' as content") + ->get() + ->toArray(); + } +} diff --git a/src/UI/API/Repositories/LocalizationRepositoryInterface.php b/src/UI/API/Repositories/LocalizationRepositoryInterface.php new file mode 100644 index 0000000..b3224a1 --- /dev/null +++ b/src/UI/API/Repositories/LocalizationRepositoryInterface.php @@ -0,0 +1,12 @@ +where('locale', implode('|', AdminKit::locales())); From 3a079f974a92b08ec2c959ff260e007769051489 Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Thu, 18 Jul 2024 18:00:35 +0900 Subject: [PATCH 2/3] feat: change localizations path, and more view fixes --- config/admin-kit-localizations.php | 2 +- config/filesystems_disks.php | 2 +- .../views/widgets/localization-files.blade.php | 15 +++++---------- src/Facades/Localizations.php | 2 ++ src/Localizations.php | 13 ++++++++++++- src/LocalizationsServiceProvider.php | 13 +++++++++++++ src/Traits/LocalizationFiles.php | 7 ++++--- .../Filament/Resources/LocalizationResource.php | 6 +++--- .../Resources/Widgets/LocalizationInformer.php | 3 ++- stubs/.gitignore.stub | 2 ++ 10 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 stubs/.gitignore.stub diff --git a/config/admin-kit-localizations.php b/config/admin-kit-localizations.php index 400c94f..e26c17b 100644 --- a/config/admin-kit-localizations.php +++ b/config/admin-kit-localizations.php @@ -2,5 +2,5 @@ // config for AdminKit/Localizations return [ - // + 'path' => storage_path('localizations/{locale}.json'), ]; diff --git a/config/filesystems_disks.php b/config/filesystems_disks.php index 8023bb5..85ad158 100644 --- a/config/filesystems_disks.php +++ b/config/filesystems_disks.php @@ -6,7 +6,7 @@ */ 'languages' => [ 'driver' => 'local', - 'root' => lang_path(), + 'root' => storage_path('localizations'), 'throw' => false, ], ]; diff --git a/resources/views/widgets/localization-files.blade.php b/resources/views/widgets/localization-files.blade.php index 5a05a4f..86fcddd 100644 --- a/resources/views/widgets/localization-files.blade.php +++ b/resources/views/widgets/localization-files.blade.php @@ -8,19 +8,14 @@ class="flex flex-col gap-2 items-center w-full cursor-pointer dark:hover:bg-whit - - @if ($exists[$locale]) - {{$counts[$locale]}} - @else - {{__('admin-kit-localizations::localizations.not_created')}} - @endif - - + /> + {{$locale}}.json {{$exists[$locale] ? $sizes[$locale] : '0.00 Kb'}} - {{$locale}}.json + + {{$exists[$locale] ? $counts[$locale] : __('admin-kit-localizations::localizations.not_created')}} + @endforeach diff --git a/src/Facades/Localizations.php b/src/Facades/Localizations.php index 2a20894..40ba875 100644 --- a/src/Facades/Localizations.php +++ b/src/Facades/Localizations.php @@ -5,6 +5,8 @@ use Illuminate\Support\Facades\Facade; /** + * @method static string getPath(string $locale) + * * @see \AdminKit\Localizations\Localizations */ class Localizations extends Facade diff --git a/src/Localizations.php b/src/Localizations.php index cc1b1be..f229e57 100755 --- a/src/Localizations.php +++ b/src/Localizations.php @@ -2,4 +2,15 @@ namespace AdminKit\Localizations; -class Localizations {} +class Localizations +{ + public function getPath(string $locale): string + { + $path = config('admin-kit-localizations.path'); + if (! str_contains($path, '{locale}')) { + throw new \Exception('The config "admin-kit-localizations.path" must contain "{locale}"'); + } + + return str_replace('{locale}', $locale, $path); + } +} diff --git a/src/LocalizationsServiceProvider.php b/src/LocalizationsServiceProvider.php index 3cc4fed..6ba2231 100644 --- a/src/LocalizationsServiceProvider.php +++ b/src/LocalizationsServiceProvider.php @@ -37,6 +37,8 @@ public function registeringPackage() public function bootingPackage() { + $this->publishFiles(); + $repository = match ((bool) config('admin-kit.cache.enabled')) { true => CachedLocalizationRepository::class, false => LocalizationRepository::class, @@ -50,4 +52,15 @@ protected function registerConfigs(): self return $this; } + + protected function publishFiles(): self + { + if ($this->app->runningInConsole()) { + $this->publishes([ + __DIR__.'/../stubs/.gitignore.stub' => storage_path('localizations/.gitignore'), + ], 'admin-kit-localizations-stubs'); + } + + return $this; + } } diff --git a/src/Traits/LocalizationFiles.php b/src/Traits/LocalizationFiles.php index f491da0..da3d481 100644 --- a/src/Traits/LocalizationFiles.php +++ b/src/Traits/LocalizationFiles.php @@ -3,20 +3,21 @@ namespace AdminKit\Localizations\Traits; use AdminKit\Core\Facades\AdminKit; +use AdminKit\Localizations\Facades\Localizations; use Illuminate\Support\Facades\File; trait LocalizationFiles { - protected function addLocalization(string $key, array $values): void + protected function addLocalization(string $key, array $content): void { foreach (AdminKit::locales() as $locale) { - $path = lang_path("$locale.json"); + $path = Localizations::getPath($locale); $jsonContent = file_exists($path) ? File::json($path) : []; - $jsonContent[$key] = $values[$locale]; + $jsonContent[$key] = $content[$locale]; File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } diff --git a/src/UI/Filament/Resources/LocalizationResource.php b/src/UI/Filament/Resources/LocalizationResource.php index 61f7397..471a4cb 100644 --- a/src/UI/Filament/Resources/LocalizationResource.php +++ b/src/UI/Filament/Resources/LocalizationResource.php @@ -2,6 +2,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources; +use AdminKit\Core\DTO\LocaleData; use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\Models\Localization; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; @@ -46,9 +47,8 @@ public static function table(Table $table): Table $columns = []; foreach (AdminKit::locales() as $locale) { $columns[] = Tables\Columns\TextColumn::make("content.$locale") - ->getStateUsing(fn (Localization $record) => $record->getTranslation('content', $locale)) - ->label($locale) - ->wrap(); + ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) + ->label((new LocaleData($locale))->native); } return $table diff --git a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php index 9c97a54..1802a88 100644 --- a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php +++ b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php @@ -3,6 +3,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources\Widgets; use AdminKit\Core\Facades\AdminKit; +use AdminKit\Localizations\Facades\Localizations; use Filament\Widgets\Widget; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\File; @@ -18,7 +19,7 @@ public function render(): View { $exists = $sizes = $counts = []; foreach (AdminKit::locales() as $locale) { - $path = lang_path("$locale.json"); + $path = Localizations::getPath($locale); $exists[$locale] = File::exists($path); if ($exists[$locale]) { diff --git a/stubs/.gitignore.stub b/stubs/.gitignore.stub new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/stubs/.gitignore.stub @@ -0,0 +1,2 @@ +* +!.gitignore From a9ddf58aa0731131956a45c01831e4c0ba6a9300 Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Thu, 18 Jul 2024 20:19:47 +0900 Subject: [PATCH 3/3] feat: add InstallCommand and more fixes --- config/admin-kit-localizations.php | 3 +- config/filesystems_disks.php | 4 +- src/Commands/InstallCommand.php | 40 +++++++++++++++++++ src/Commands/LocalizationsCommand.php | 19 --------- src/LocalizationsServiceProvider.php | 4 +- src/Traits/LocalizationFiles.php | 7 ++-- .../Resources/LocalizationResource.php | 2 +- .../Pages/CreateLocalization.php | 7 +--- .../Pages/EditLocalization.php | 2 + .../Pages/ListLocalization.php | 8 ++-- .../Widgets/LocalizationInformer.php | 24 ++++++++--- 11 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 src/Commands/InstallCommand.php delete mode 100644 src/Commands/LocalizationsCommand.php diff --git a/config/admin-kit-localizations.php b/config/admin-kit-localizations.php index e26c17b..8a85416 100644 --- a/config/admin-kit-localizations.php +++ b/config/admin-kit-localizations.php @@ -2,5 +2,6 @@ // config for AdminKit/Localizations return [ - 'path' => storage_path('localizations/{locale}.json'), + 'disk' => 'localizations', + 'path' => '{locale}.json', ]; diff --git a/config/filesystems_disks.php b/config/filesystems_disks.php index 85ad158..b091c83 100644 --- a/config/filesystems_disks.php +++ b/config/filesystems_disks.php @@ -2,9 +2,9 @@ return [ /** - * Custom filesystems.disks.languages config + * Custom filesystems.disks.localizations config */ - 'languages' => [ + 'localizations' => [ 'driver' => 'local', 'root' => storage_path('localizations'), 'throw' => false, diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php new file mode 100644 index 0000000..584f07f --- /dev/null +++ b/src/Commands/InstallCommand.php @@ -0,0 +1,40 @@ +confirm('Publishing stubs and migrations?', true)) { + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-stubs', + ]); + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-migrations', + ]); + } + + if ($this->confirm('Migrate the database tables?', true)) { + $this->call('migrate'); + } + + if ($this->confirm('(Optional) Publishing config file?')) { + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-config', + ]); + } + + return self::SUCCESS; + } +} diff --git a/src/Commands/LocalizationsCommand.php b/src/Commands/LocalizationsCommand.php deleted file mode 100644 index 99a1ec2..0000000 --- a/src/Commands/LocalizationsCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/LocalizationsServiceProvider.php b/src/LocalizationsServiceProvider.php index 6ba2231..2fc0b90 100644 --- a/src/LocalizationsServiceProvider.php +++ b/src/LocalizationsServiceProvider.php @@ -2,7 +2,7 @@ namespace AdminKit\Localizations; -use AdminKit\Localizations\Commands\LocalizationsCommand; +use AdminKit\Localizations\Commands\InstallCommand; use AdminKit\Localizations\Providers\RouteServiceProvider; use AdminKit\Localizations\UI\API\Repositories\CachedLocalizationRepository; use AdminKit\Localizations\UI\API\Repositories\LocalizationRepository; @@ -25,7 +25,7 @@ public function configurePackage(Package $package): void ->hasViews() ->hasTranslations() ->hasMigration('create_admin_kit_localizations_table') - ->hasCommand(LocalizationsCommand::class); + ->hasCommand(InstallCommand::class); } public function registeringPackage() diff --git a/src/Traits/LocalizationFiles.php b/src/Traits/LocalizationFiles.php index da3d481..c711a72 100644 --- a/src/Traits/LocalizationFiles.php +++ b/src/Traits/LocalizationFiles.php @@ -4,22 +4,23 @@ use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\Facades\Localizations; -use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Storage; trait LocalizationFiles { protected function addLocalization(string $key, array $content): void { foreach (AdminKit::locales() as $locale) { + $file = Storage::disk(config('admin-kit-localizations.disk')); $path = Localizations::getPath($locale); $jsonContent = file_exists($path) - ? File::json($path) + ? $file->json($path) : []; $jsonContent[$key] = $content[$locale]; - File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); + $file->put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } } } diff --git a/src/UI/Filament/Resources/LocalizationResource.php b/src/UI/Filament/Resources/LocalizationResource.php index 471a4cb..0e7d22d 100644 --- a/src/UI/Filament/Resources/LocalizationResource.php +++ b/src/UI/Filament/Resources/LocalizationResource.php @@ -47,7 +47,7 @@ public static function table(Table $table): Table $columns = []; foreach (AdminKit::locales() as $locale) { $columns[] = Tables\Columns\TextColumn::make("content.$locale") - ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) + ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) ->label((new LocaleData($locale))->native); } diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php index 444f23a..65b7cd2 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php @@ -2,6 +2,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; +use AdminKit\Core\Traits\Filament\RedirectToListPageAfterSave; use AdminKit\Localizations\Traits\LocalizationFiles; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Resources\Pages\CreateRecord; @@ -9,6 +10,7 @@ class CreateLocalization extends CreateRecord { use LocalizationFiles; + use RedirectToListPageAfterSave; protected static string $resource = LocalizationResource::class; @@ -19,11 +21,6 @@ protected function getHeaderActions(): array ]; } - protected function getRedirectUrl(): string - { - return LocalizationResource::getUrl(); - } - public function beforeCreate(): void { $this->addLocalization( diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php index bfb4948..947b29a 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php @@ -2,6 +2,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; +use AdminKit\Core\Traits\Filament\RedirectToListPageAfterSave; use AdminKit\Localizations\Traits\LocalizationFiles; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Actions; @@ -10,6 +11,7 @@ class EditLocalization extends EditRecord { use LocalizationFiles; + use RedirectToListPageAfterSave; protected static string $resource = LocalizationResource::class; diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php index 5c08455..9b2b928 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php @@ -3,13 +3,14 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; use AdminKit\Core\Facades\AdminKit; +use AdminKit\Localizations\Facades\Localizations; 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; +use Illuminate\Support\Facades\Storage; class ListLocalization extends ListRecords { @@ -41,7 +42,8 @@ public function publish(): void ->get(); foreach (AdminKit::locales() as $locale) { - $path = lang_path("$locale.json"); + $file = Storage::disk(config('admin-kit-localizations.disk')); + $path = Localizations::getPath($locale); $jsonContent = $localizations ->mapWithKeys(fn ($value, $key) => [ @@ -49,7 +51,7 @@ public function publish(): void ]) ->toArray(); - File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); + $file->put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } Notification::make() diff --git a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php index 1802a88..f9cf199 100644 --- a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php +++ b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php @@ -4,9 +4,9 @@ use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\Facades\Localizations; +use Filament\Notifications\Notification; use Filament\Widgets\Widget; use Illuminate\Contracts\View\View; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; class LocalizationInformer extends Widget @@ -19,15 +19,16 @@ public function render(): View { $exists = $sizes = $counts = []; foreach (AdminKit::locales() as $locale) { + $file = Storage::disk(config('admin-kit-localizations.disk')); $path = Localizations::getPath($locale); - $exists[$locale] = File::exists($path); + $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]) { - $count = count(json_decode(File::get($path), true)); + $count = count(json_decode($file->get($path), true)); $counts[$locale] = trans_choice( 'admin-kit-localizations::localizations.count_keys', $count, @@ -45,6 +46,19 @@ public function render(): View public function downloadTranslationFile($locale) { - return Storage::disk('languages')->download("$locale.json"); + $file = Storage::disk(config('admin-kit-localizations.disk')); + + $path = Localizations::getPath($locale); + + if (! $file->exists($path)) { + Notification::make() + ->title(__('File not found')) + ->danger() + ->send(); + + return null; + } + + return $file->download($path); } }