spatie-translatable

Installation

Install the plugin with Composer:

1composer require lara-zeus/spatie-translatable

Adding the plugin to a panel

To add a plugin to a panel, you must include it in the configuration file using the plugin() method:

1use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin;
2 
3public function panel(Panel $panel): Panel
4{
5 return $panel
6 // ...
7 ->plugin(SpatieTranslatablePlugin::make());
8}

Persist active local in Session

to remember the user's selected locale throughout their session, you can pass the method persist()

1use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin;
2 
3public function panel(Panel $panel): Panel
4{
5 return $panel
6 // ...
7 ->plugin(
8 SpatieTranslatablePlugin::make()
9 ->persist(),
10 );
11}

Setting the default translatable locales

To set up the locales that can be used to translate content, you can pass an array of locales to the defaultLocales() plugin method:

1use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin;
2 
3public function panel(Panel $panel): Panel
4{
5 return $panel
6 // ...
7 ->plugin(
8 SpatieTranslatablePlugin::make()
9 ->defaultLocales(['en', 'es']),
10 );
11}

Validating all configured locales

By default, Filament only runs form validation for locales that have been actively visited or modified by the user. If you want to ensure that all required fields across every configured locale are validated before a record is saved, you can use the validateAllLocales() method:

1use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin;
2 
3public function panel(Panel $panel): Panel
4{
5 return $panel
6 // ...
7 ->plugin(
8 SpatieTranslatablePlugin::make()
9 ->defaultLocales(['en', 'es'])
10 ->validateAllLocales()
11 );
12}

When enabled, if validation fails for any locale (even if the user hasn't switched to it), the form will automatically switch to the failing locale tab and display the validation errors.

Preparing your model class

You need to make your model translatable. You can read how to do this in Spatie's documentation.

Preparing your resource class

You must apply the LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable trait to your resource class:

1use LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable;
2use Filament\Resources\Resource;
3 
4class BlogPostResource extends Resource
5{
6 use Translatable;
7 
8 // ...
9}

Our Sponsors

Zeus is an open-source project. Thanks to our sponsors for helping maintain these packages.