Skip to content

Commit

Permalink
Add copy translation action
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkidn committed Oct 17, 2023
1 parent 32b228a commit 992d49c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
31 changes: 31 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 7 additions & 0 deletions resources/lang/en/copy-translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'label' => 'Copy Translation',
'copy translation for :label' => 'Copy translation for :label',
'success notification' => 'Translation copied successfully.',
];
62 changes: 62 additions & 0 deletions src/Actions/CopyTranslationAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Codedor\TranslatableTabs\Actions;

use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Livewire\Component;
use Throwable;

class CopyTranslationAction extends Action
{
private array $locales = [];

public static function getDefaultName(): ?string
{
return 'copy-translation';
}

public function locales(array $locales) : self
{
$this->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();
}
});
}
}
3 changes: 2 additions & 1 deletion src/Providers/TranslatableTabsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function configurePackage(Package $package): void
$package
->name('filament-translatable-tabs')
->hasViews()
->setBasePath(__DIR__ . '/../');
->setBasePath(__DIR__ . '/../')
->hasTranslations();
}
}
1 change: 1 addition & 0 deletions src/Resources/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codedor\TranslatableTabs\Resources\Traits;

use Filament\Actions\Action;
use Illuminate\Support\Arr;

trait HasTranslations
Expand Down

0 comments on commit 992d49c

Please sign in to comment.