From 65e55fd5c68498163f653724780bc47ced150744 Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Wed, 31 Jul 2024 16:00:54 +0900 Subject: [PATCH] feat: change ui --- composer.json | 2 +- resources/lang/ru/localizations.php | 24 ++++++ .../Filament/Filters/NotTranslatedFilter.php | 32 ++++++++ .../Resources/LocalizationResource.php | 80 ++++++++++++------- 4 files changed, 107 insertions(+), 31 deletions(-) create mode 100644 src/UI/Filament/Filters/NotTranslatedFilter.php diff --git a/composer.json b/composer.json index 4faa9b0..16a14dd 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": "^8.1", "filament/filament": "^3.0-stable", - "ibecsystems/admin-kit-core": "^3.5", + "ibecsystems/admin-kit-core": "^3.6", "illuminate/contracts": "^10.0|^11.0", "spatie/laravel-package-tools": "^1.14.0", "spatie/laravel-data": "^4.5", diff --git a/resources/lang/ru/localizations.php b/resources/lang/ru/localizations.php index d75b71b..517d727 100644 --- a/resources/lang/ru/localizations.php +++ b/resources/lang/ru/localizations.php @@ -18,4 +18,28 @@ 'count_keys' => ':count ключ|:count ключа|:count ключей', 'not_created' => 'не создан', + + 'translation-navigation-label' => 'Перевод', + 'translation-navigation-plural-label' => 'Переводы', + 'translation-navigation-group' => 'Локализации', + 'translation-label' => 'Переводы ошибок', + 'group' => 'Группа перевода', + 'key' => 'Ключ перевода', + 'preview-in-your-lang' => 'Просмотр на языке (:lang)', + 'synchronize' => 'Синхронизировать', + 'synchronization-success' => 'Синхронизировано :count переводов!', + 'synchronization-deleted' => 'Удалено :count неиспользуемых переводов', + 'preview' => 'Предварительный просмотр', + 'preview-description' => 'Это пример на выбранном вами языке (:lang)', + 'add-translation-button' => 'Добавить новый перевод', + 'translation-language' => 'Язык', + 'translation-text' => 'Текст перевода', + 'filter-not-translated' => 'Не переведено на', + 'quick-translate' => 'Быстрый перевод', + 'quick-translate-select-locale' => 'Выберите язык', + 'quick-translate-translation-number' => 'Осталось перевести :total переводов', + 'quick-translate-skip' => 'Пропустить этот перевод', + 'quick-translate-enter' => 'Введите перевод на ":lang":', + 'quick-translate-save-and-continue' => 'Сохранить и продолжить', + 'quick-translate-nothing' => 'Нечего переводить!', ]; diff --git a/src/UI/Filament/Filters/NotTranslatedFilter.php b/src/UI/Filament/Filters/NotTranslatedFilter.php new file mode 100644 index 0000000..a1ead3c --- /dev/null +++ b/src/UI/Filament/Filters/NotTranslatedFilter.php @@ -0,0 +1,32 @@ +form([ + Select::make('lang') + ->label(__('admin-kit-localizations::localizations.filter-not-translated')) + ->options(LocaleData::makeCollection() + ->mapWithKeys(fn (LocaleData $locale) => [$locale->code => $locale->native]) + ->toArray() + ), + ]) + ->query(function (Builder $query, array $data): Builder { + return $query + ->when( + $data['lang'], + fn (Builder $query, $date): Builder => $query->whereNull('content->'.$data['lang']) + ); + }); + } +} diff --git a/src/UI/Filament/Resources/LocalizationResource.php b/src/UI/Filament/Resources/LocalizationResource.php index 0e7d22d..50ff6ce 100644 --- a/src/UI/Filament/Resources/LocalizationResource.php +++ b/src/UI/Filament/Resources/LocalizationResource.php @@ -2,69 +2,89 @@ 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\Filters\NotTranslatedFilter; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer; use Filament\Forms; +use Filament\Forms\Components\Textarea; use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables; +use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; class LocalizationResource extends Resource { protected static ?string $model = Localization::class; - protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; + protected static ?string $navigationIcon = 'heroicon-o-language'; public static function form(Form $form): Form { - $keys = []; - foreach (config('admin-kit.locales') as $value) { - $keys[] = Forms\Components\TextInput::make("content.$value") - ->label($value) - ->required(config('app.locale') === $value); + $components = [ + Forms\Components\TextInput::make('key') + ->label(__('admin-kit-localizations::localizations.key')) + ->prefixIcon('heroicon-o-key') + ->unique(ignoreRecord: true) + ->required() + ->columnSpanFull(), + ]; + + foreach (AdminKit::locales() as $locale) { + $components[] = Textarea::make("content.$locale") + ->label(__('admin-kit-localizations::localizations.translation-content')." ($locale)") + ->rows(5) + ->required($locale === app()->getLocale()); } - return $form - ->schema([ - Forms\Components\Section::make('')->schema([ - Forms\Components\TextInput::make('key') - ->label(__('admin-kit-localizations::localizations.resource.key')) - ->unique(ignoreRecord: true) - ->regex('/^[a-z0-9]+(?:-[a-z0-9]+)*$/i') // slug - ->required(), - ]), - Forms\Components\Section::make('')->schema($keys), - ]); + return $form->schema($components); } public static function table(Table $table): Table { - $columns = []; + $columns = [ + TextColumn::make('key') + ->label(__('admin-kit-localizations::localizations.key')) + ->size('sm') + ->limit(30) + ->searchable(), + + TextColumn::make('content') + ->searchable(query: function (Builder $query, string $search): Builder { + return $query + ->where('content', 'ILIKE', "%{$search}%"); + }) + ->label(__('admin-kit-localizations::localizations.preview-in-your-lang', ['lang' => app()->getLocale()])) + ->icon('heroicon-o-language') + ->size('sm') + ->sortable(false) + ->limit(50), + ]; + foreach (AdminKit::locales() as $locale) { - $columns[] = Tables\Columns\TextColumn::make("content.$locale") - ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) - ->label((new LocaleData($locale))->native); + + $columns[] = IconColumn::make($locale) + ->label(strtoupper($locale)) + ->searchable(false) + ->sortable(false) + ->getStateUsing(function (Localization $record) use ($locale) { + return in_array($locale, array_keys($record->getTranslations('content'))) && ! is_null($record->getTranslation('content', $locale)); + }) + ->boolean(); } return $table - ->columns([ - TextColumn::make('key') - ->label(__('admin-kit-localizations::localizations.resource.key')) - ->searchable(), - ...$columns, - ]) + ->columns($columns) ->defaultSort('id', 'desc') ->filters([ - // + NotTranslatedFilter::make(), ]) ->actions([ Tables\Actions\EditAction::make(), - Tables\Actions\DeleteAction::make(), ]) ->bulkActions([ Tables\Actions\DeleteBulkAction::make(),