Making resource pages translatable
After preparing your resource class, you must make each of your resource's pages translatable too. You can find your resource's pages in the Pages
directory of each resource folder. To prepare a page, you must apply the corresponding Translatable
trait to it, and install a LocaleSwitcher
header action:
1use Filament\Actions; 2use Filament\Resources\Pages\ListRecords; 3 4class ListBlogPosts extends ListRecords 5{ 6 use ListRecords\Concerns\Translatable; 7 8 protected function getHeaderActions(): array 9 {10 return [11 Actions\LocaleSwitcher::make(),12 // ...13 ];14 }15 16 // ...17}
1use Filament\Actions; 2use Filament\Resources\Pages\CreateRecord; 3 4class CreateBlogPost extends CreateRecord 5{ 6 use CreateRecord\Concerns\Translatable; 7 8 protected function getHeaderActions(): array 9 {10 return [11 Actions\LocaleSwitcher::make(),12 // ...13 ];14 }15 16 // ...17}
1use Filament\Actions; 2use Filament\Resources\Pages\EditRecord; 3 4class EditBlogPost extends EditRecord 5{ 6 use EditRecord\Concerns\Translatable; 7 8 protected function getHeaderActions(): array 9 {10 return [11 Actions\LocaleSwitcher::make(),12 // ...13 ];14 }15 16 // ...17}
And if you have a ViewRecord
page for your resource:
1use Filament\Actions; 2use Filament\Resources\Pages\ViewRecord; 3 4class ViewBlogPost extends ViewRecord 5{ 6 use ViewRecord\Concerns\Translatable; 7 8 protected function getHeaderActions(): array 9 {10 return [11 Actions\LocaleSwitcher::make(),12 // ...13 ];14 }15 16 // ...17}
If you're using a simple resource, you can make the ManageRecords
page translatable instead:
1use Filament\Actions; 2use Filament\Resources\Pages\ManageRecords; 3 4class ManageBlogPosts extends ListRecords 5{ 6 use ManageRecords\Concerns\Translatable; 7 8 protected function getHeaderActions(): array 9 {10 return [11 Actions\LocaleSwitcher::make(),12 // ...13 ];14 }15 16 // ...17}
Setting the translatable locales for a particular resource
By default, the translatable locales can be set globally for all resources in the plugin configuration. Alternatively, you can customize the translatable locales for a particular resource by overriding the getTranslatableLocales()
method in your resource class:
1use Filament\Resources\Concerns\Translatable; 2use Filament\Resources\Resource; 3 4class BlogPostResource extends Resource 5{ 6 use Translatable; 7 8 // ... 9 10 public static function getTranslatableLocales(): array11 {12 return ['en', 'fr'];13 }14}
Translating relation managers
First, you must apply the Filament\Resources\RelationManagers\Concerns\Translatable
trait to the relation manager class:
1use Filament\Resources\RelationManagers\Concerns\Translatable;2use Filament\Resources\RelationManagers\RelationManager;3 4class BlogPostsRelationManager extends RelationManager5{6 use Translatable;7 8 // ...9}
Now, you can add a new LocaleSwitcher
action to the header of the relation manager's table()
:
1use Filament\Tables; 2use Filament\Tables\Table; 3 4public function table(Table $table): Table 5{ 6 return $table 7 ->columns([ 8 // ... 9 ])10 ->headerActions([11 // ...12 Tables\Actions\LocaleSwitcher::make(),13 ]);14}
Inheriting the relation manager's active locale from the resource page
If you wish to reactively inherit the locale of the Translatable
resource page that the relation manager is being displayed on, you can override the $activeLocale
property and add Livewire's Reactive
attribute to it:
1use Filament\Resources\RelationManagers\Concerns\Translatable; 2use Filament\Resources\RelationManagers\RelationManager; 3use Livewire\Attributes\Reactive; 4 5class BlogPostsRelationManager extends RelationManager 6{ 7 use Translatable; 8 9 #[Reactive]10 public ?string $activeLocale = null;11 12 // ...13}
If you do this, you no longer need a LocaleSwitcher
action in the table()
.
Setting the translatable locales for a particular relation manager
By default, the translatable locales can be set globally for all relation managers in the plugin configuration. Alternatively, you can customize the translatable locales for a particular relation manager by overriding the getTranslatableLocales()
method in your relation manager class:
1use Filament\Resources\RelationManagers\Concerns\Translatable; 2use Filament\Resources\RelationManagers\RelationManager; 3 4class BlogPostsRelationManager extends RelationManager 5{ 6 use Translatable; 7 8 // ... 9 10 public function getTranslatableLocales(): array11 {12 return ['en', 'fr'];13 }14}
Publishing translations
If you wish to translate the package, you may publish the language files using:
1php artisan vendor:publish --tag=filament-spatie-laravel-translatable-plugin-translations