filament/spatie-laravel-translatable-plugin
Filament v3 plugin integrating spatie/laravel-translatable into resources: set default locales, mark models as translatable, add Translatable traits to resources and pages, and use a LocaleSwitcher action to edit content per locale. Now maintained by Lara Zeus.
Installation
composer require filament/spatie-laravel-translatable-plugin
Publish the plugin assets (if needed):
php artisan vendor:publish --provider="Filament\SpatieLaravelTranslatablePlugin\SpatieLaravelTranslatablePluginServiceProvider" --tag="spatie-laravel-translatable-plugin-assets"
Enable the Plugin
Register the plugin in app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\Filament\SpatieLaravelTranslatablePlugin::make(),
]);
}
First Use Case
Spatie\Translatable\HasTranslations trait.app/Models/Post.php):
use Spatie\Translatable\HasTranslations;
class Post extends Model
{
use HasTranslations;
public $translatable = ['title', 'content'];
}
php artisan make:filament-resource Post) and add the plugin to its form/tables.Form Integration
Use the plugin’s TranslatableFields widget in your resource’s Form:
use Filament\Forms\Components\Fieldset;
use Filament\SpatieLaravelTranslatablePlugin\Forms\Components\TranslatableFields;
public static function form(Form $form): Form
{
return $form
->schema([
TranslatableFields::make('translatable_content')
->fields([
TextInput::make('title')->required(),
RichEditor::make('content'),
])
->availableLocales(['en', 'es', 'fr']),
]);
}
Dynamic Locales Fetch locales dynamically from a config or database:
TranslatableFields::make('translatable_content')
->availableLocales(config('app.available_locales'))
Table Columns Display translatable columns in tables:
use Filament\Tables\Columns\Column;
public static function table(Table $table): Table
{
return $table
->columns([
Column::make('title')
->translatable()
->sortable(),
]);
}
Default Locale Fallback Set a default locale for display:
Column::make('title')
->translatable()
->defaultLocale('en')
app.locale middleware to handle locale switching.public static function getEloquentQuery(Builder $query): Builder
{
return $query->setTranslationLocale(request()->get('locale', 'en'));
}
TranslatableFields::make('translatable_content')
->rules([
'title' => ['required', 'min:3'],
'content' => ['required', 'min:10'],
])
Locale Mismatch
app.locale is set or pass the locale via the form:
TranslatableFields::make('translatable_content')
->locale(request()->get('locale', 'en'))
Missing Translations Table
spatie/laravel-translatable (php artisan migrate).translations table exists.Nested Translatable Fields
TranslatableFields only at the top level of the form schema.translations table:
SELECT * FROM translations WHERE translatable_id = {id};
\Log::info('Current locale:', request()->get('locale'));
Custom Locales Extend the plugin to support custom locale formats or validation:
TranslatableFields::make('custom_translations')
->availableLocales(['en-US', 'es-MX'])
->customLocaleFormatter(fn ($locale) => ucfirst($locale));
Override Default Behavior
Override the plugin’s default locale resolution in app/Providers/AppServiceProvider.php:
use Filament\SpatieLaravelTranslatablePlugin\SpatieLaravelTranslatablePlugin;
SpatieLaravelTranslatablePlugin::macro('getDefaultLocale', function () {
return 'es';
});
Add Custom Actions Attach locale-specific actions to resources:
public static function getActions(Post $record): array
{
return [
Action::make('translate')
->url(fn (Post $record) => route('filament.resources.posts.translate', $record))
->icon('heroicon-o-language'),
];
}
config/spatie/laravel-translatable.php:
'default_locale' => 'en',
'fallback_locales' => ['en'],
How can I help you explore Laravel packages today?