Skip to content

Commit

Permalink
feat: change ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ast21 committed Jul 31, 2024
1 parent e5645e2 commit 65e55fd
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions resources/lang/ru/localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => 'Нечего переводить!',
];
32 changes: 32 additions & 0 deletions src/UI/Filament/Filters/NotTranslatedFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace AdminKit\Localizations\UI\Filament\Filters;

use AdminKit\Core\DTO\LocaleData;
use AdminKit\Core\Facades\AdminKit;
use Filament\Forms\Components\Select;
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

class NotTranslatedFilter extends Filter
{
public static function make(?string $name = null): static
{
return parent::make('not-translated')
->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'])
);
});
}
}
80 changes: 50 additions & 30 deletions src/UI/Filament/Resources/LocalizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 65e55fd

Please sign in to comment.