diff --git a/docs/index.md b/docs/index.md index 68ee9e6..b8e65bb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -137,3 +137,34 @@ LocalesColumn::configureUsing(function (LocalesColumn $column) { ``` Read more about this behavior [here](https://filamentphp.com/docs/3.x/forms/fields/getting-started#global-settings). + +## Actions + +### CopyTranslationAction + +This package provides an action to copy a locale to another locale. + +To use it add it to your Edit page, e.g.: + +```php +use Codedor\TranslatableTabs\Actions\CopyTranslationAction; +use Filament\Actions\DeleteAction; +use Filament\Resources\Pages\EditRecord; + +class EditJobPage extends EditRecord +{ + // ... + + protected function getHeaderActions(): array + { + return [ + CopyTranslationAction::make(), + DeleteAction::make(), + ]; + } + + // ... +} +``` + +When clicked, this will open a modal and will then copy the data from the selected locale to the other locale. diff --git a/resources/lang/en/copy-translation.php b/resources/lang/en/copy-translation.php new file mode 100644 index 0000000..3dd878b --- /dev/null +++ b/resources/lang/en/copy-translation.php @@ -0,0 +1,7 @@ + 'Copy Translation', + 'copy translation for :label' => 'Copy translation for :label', + 'success notification' => 'Translation copied successfully.', +]; diff --git a/src/Actions/CopyTranslationAction.php b/src/Actions/CopyTranslationAction.php new file mode 100644 index 0000000..31cbd67 --- /dev/null +++ b/src/Actions/CopyTranslationAction.php @@ -0,0 +1,62 @@ +locales = $locales; + + return $this; + } + + public function getLocales(): array + { + return $this->locales; + } + + protected function setUp(): void + { + parent::setUp(); + + $this->label(__('filament-translatable-tabs::copy-translation.label')); + + $this->modalHeading(fn (): string => __('filament-translatable-tabs::copy-translation.copy translation for :label', ['label' => $this->getRecordTitle()])); + + $this->successNotificationTitle(__('filament-translatable-tabs::copy-translation.success notification')); + + $this->form([ + Select::make('from_locale') + ->options($this->getLocales()) + ->required(), + + Select::make('to_locale') + ->options($this->getLocales()) + ->required() + ->different('from_locale'), + ]); + + $this->action(function (array $data, Component $livewire) { + try { + $livewire->data[$data['to_locale']] = $livewire->data[$data['from_locale']]; + + $this->success(); + } catch (Throwable $e) { + $this->failure(); + } + }); + } +} diff --git a/src/Providers/TranslatableTabsServiceProvider.php b/src/Providers/TranslatableTabsServiceProvider.php index 1793dbf..251ba62 100644 --- a/src/Providers/TranslatableTabsServiceProvider.php +++ b/src/Providers/TranslatableTabsServiceProvider.php @@ -12,6 +12,7 @@ public function configurePackage(Package $package): void $package ->name('filament-translatable-tabs') ->hasViews() - ->setBasePath(__DIR__ . '/../'); + ->setBasePath(__DIR__ . '/../') + ->hasTranslations(); } } diff --git a/src/Resources/Traits/HasTranslations.php b/src/Resources/Traits/HasTranslations.php index d68b7e8..d24b516 100644 --- a/src/Resources/Traits/HasTranslations.php +++ b/src/Resources/Traits/HasTranslations.php @@ -2,6 +2,7 @@ namespace Codedor\TranslatableTabs\Resources\Traits; +use Filament\Actions\Action; use Illuminate\Support\Arr; trait HasTranslations