Feature: Adding Translation Service and Translation Seeder
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Filament\Resources;
|
||||
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Lunar\Models\Language;
|
||||
use Modules\Core\Localization\Filament\Resources\LanguageLineResource\Pages;
|
||||
use Spatie\TranslationLoader\LanguageLine;
|
||||
|
||||
class LanguageLineResource extends Resource
|
||||
{
|
||||
protected static ?string $model = LanguageLine::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-language';
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $modelLabel = 'Translation';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Translations';
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form->schema([
|
||||
Forms\Components\TextInput::make('group')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->default('storefront')
|
||||
->helperText('Namespace for this label, e.g. "storefront" for e-shop UI text.'),
|
||||
|
||||
Forms\Components\TextInput::make('key')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->helperText('Dot-notation key, e.g. "nav.cart".'),
|
||||
|
||||
Forms\Components\Fieldset::make('Translations')
|
||||
->schema(static::localeInputs()),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('group')
|
||||
->badge()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('key')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
...static::localeColumns(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('group')
|
||||
->options(fn () => LanguageLine::query()->distinct()->pluck('group', 'group')),
|
||||
])
|
||||
->defaultSort('key');
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListLanguageLines::route('/'),
|
||||
'create' => Pages\CreateLanguageLine::route('/create'),
|
||||
'edit' => Pages\EditLanguageLine::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Forms\Components\Textarea>
|
||||
*/
|
||||
private static function localeInputs(): array
|
||||
{
|
||||
return static::localeCodes()
|
||||
->map(fn (string $code) => Forms\Components\Textarea::make("text.{$code}")
|
||||
->label(strtoupper($code))
|
||||
->rows(2))
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Tables\Columns\TextColumn>
|
||||
*/
|
||||
private static function localeColumns(): array
|
||||
{
|
||||
return static::localeCodes()
|
||||
->map(fn (string $code) => Tables\Columns\TextColumn::make("text.{$code}")
|
||||
->label(strtoupper($code))
|
||||
->limit(40)
|
||||
->toggleable())
|
||||
->all();
|
||||
}
|
||||
|
||||
private static function localeCodes(): \Illuminate\Support\Collection
|
||||
{
|
||||
return Language::query()->pluck('code');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Filament\Resources\LanguageLineResource\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Modules\Core\Localization\Filament\Resources\LanguageLineResource;
|
||||
use Modules\Core\Localization\TranslationService;
|
||||
|
||||
class CreateLanguageLine extends CreateRecord
|
||||
{
|
||||
protected static string $resource = LanguageLineResource::class;
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
return app(TranslationService::class)->create(
|
||||
$data['group'],
|
||||
$data['key'],
|
||||
$data['text'] ?? [],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Filament\Resources\LanguageLineResource\Pages;
|
||||
|
||||
use Filament\Actions;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Modules\Core\Localization\Filament\Resources\LanguageLineResource;
|
||||
use Modules\Core\Localization\TranslationService;
|
||||
use Spatie\TranslationLoader\LanguageLine;
|
||||
|
||||
class EditLanguageLine extends EditRecord
|
||||
{
|
||||
protected static string $resource = LanguageLineResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make()
|
||||
->action(function (LanguageLine $record) {
|
||||
app(TranslationService::class)->delete($record);
|
||||
|
||||
$this->redirect($this->getResource()::getUrl('index'));
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filament's default Cancel button uses window.history.back(), which
|
||||
* restores the browser's cached previous page instead of re-fetching —
|
||||
* so an edit made just before clicking Cancel doesn't show up in the
|
||||
* list until a manual refresh. Redirect through Livewire instead, which
|
||||
* always re-queries.
|
||||
*/
|
||||
protected function getCancelFormAction(): Action
|
||||
{
|
||||
return Action::make('cancel')
|
||||
->label(__('filament-panels::resources/pages/edit-record.form.actions.cancel.label'))
|
||||
->url(static::getResource()::getUrl('index'))
|
||||
->color('gray');
|
||||
}
|
||||
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
return app(TranslationService::class)->update(
|
||||
$record,
|
||||
$data['group'],
|
||||
$data['key'],
|
||||
$data['text'] ?? [],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Filament\Resources\LanguageLineResource\Pages;
|
||||
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Core\Localization\Filament\Resources\LanguageLineResource;
|
||||
|
||||
class ListLanguageLines extends ListRecords
|
||||
{
|
||||
protected static string $resource = LanguageLineResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user