Try Live
Add Docs
Rankings
Pricing
Enterprise
Docs
Install
Install
Docs
Pricing
Enterprise
More...
More...
Try Live
Rankings
Add Docs
Lara Zeus Spatie Translatable
https://github.com/lara-zeus/spatie-translatable
Admin
Lara Zeus Spatie Translatable is a Filament plugin that provides enhanced support for Spatie's
...
Tokens:
6,072
Snippets:
69
Trust Score:
8
Update:
2 weeks ago
Context
Skills
Chat
Benchmark
88.5
Suggestions
Latest
Show doc for...
Code
Info
Show Results
Context Summary (auto-generated)
Raw
Copy
Link
# Lara Zeus Spatie Translatable Lara Zeus Spatie Translatable is a Filament plugin that provides seamless integration with Spatie's Laravel Translatable package. It enables multi-language content management in Filament admin panels by adding a locale switcher action and making Filament resources, pages, and relation managers translation-aware. The plugin requires PHP 8.1+, Filament v5, and spatie/laravel-translatable v6. This plugin is a fork of the original Filament Spatie Laravel Translatable plugin, maintained by Lara Zeus with the goal of addressing issues, adding features, and enhancing functionality. It supports creating, editing, listing, and viewing translatable content, allows setting default translatable locales globally or per-resource, persists locale selection in session, and provides a reactive locale inheritance system for relation managers. ## Installation Install the plugin via Composer and register it with a Filament panel. ```bash composer require lara-zeus/spatie-translatable ``` ```php use Filament\Panel; use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin; public function panel(Panel $panel): Panel { return $panel ->plugins([ SpatieTranslatablePlugin::make() ->defaultLocales(['en', 'es', 'fr']) ->persist() // Remember locale selection in session ->useFallbackLocale(), // Use fallback locale for missing translations ]); } ``` ## SpatieTranslatablePlugin Configuration The main plugin class provides configuration methods for locale settings, persistence, and label customization. ```php use Filament\Panel; use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin; public function panel(Panel $panel): Panel { return $panel ->plugins([ SpatieTranslatablePlugin::make() // Set available locales for translation ->defaultLocales(['en', 'es', 'fr', 'de']) // Persist selected locale in user session ->persist(true) // Use fallback locale when translation is missing ->useFallbackLocale(true) // Customize locale labels (optional) ->getLocaleLabelUsing(function (string $locale, ?string $displayLocale): ?string { return match ($locale) { 'en' => 'English', 'es' => 'Spanish', 'fr' => 'French', 'de' => 'German', default => null, // Falls back to locale_get_display_name() }; }), ]); } ``` ## Making a Resource Translatable Apply the Translatable trait to your Filament resource class to enable translation support. ```php use Filament\Resources\Resource; use LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable; class BlogPostResource extends Resource { use Translatable; protected static ?string $model = BlogPost::class; protected static ?string $navigationIcon = 'heroicon-o-document-text'; // Override to set custom locales for this specific resource public static function getTranslatableLocales(): array { return ['en', 'fr']; // Only English and French for this resource } public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('title') ->required() ->maxLength(255), Forms\Components\Textarea::make('content') ->required(), ]); } } ``` ## LocaleSwitcher Action for List Pages Add the LocaleSwitcher action to List pages to enable locale switching in the header. ```php use Filament\Resources\Pages\ListRecords; use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; use LaraZeus\SpatieTranslatable\Resources\Pages\ListRecords\Concerns\Translatable; class ListBlogPosts extends ListRecords { use Translatable; protected static string $resource = BlogPostResource::class; protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), Actions\CreateAction::make(), ]; } } ``` ## LocaleSwitcher Action for Create Pages Add the LocaleSwitcher to Create pages to specify the locale when creating new records. ```php use Filament\Resources\Pages\CreateRecord; use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; use LaraZeus\SpatieTranslatable\Resources\Pages\CreateRecord\Concerns\Translatable; class CreateBlogPost extends CreateRecord { use Translatable; protected static string $resource = BlogPostResource::class; protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), ]; } } ``` ## LocaleSwitcher Action for Edit Pages Add the LocaleSwitcher to Edit pages to switch between locale translations while editing. ```php use Filament\Actions; use Filament\Resources\Pages\EditRecord; use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; use LaraZeus\SpatieTranslatable\Resources\Pages\EditRecord\Concerns\Translatable; class EditBlogPost extends EditRecord { use Translatable; protected static string $resource = BlogPostResource::class; protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), Actions\ViewAction::make(), Actions\DeleteAction::make(), ]; } } ``` ## LocaleSwitcher Action for View Pages Add the LocaleSwitcher to View pages to view records in different locales. ```php use Filament\Actions; use Filament\Resources\Pages\ViewRecord; use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; use LaraZeus\SpatieTranslatable\Resources\Pages\ViewRecord\Concerns\Translatable; class ViewBlogPost extends ViewRecord { use Translatable; protected static string $resource = BlogPostResource::class; protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), Actions\EditAction::make(), ]; } } ``` ## LocaleSwitcher for ManageRecords (Simple Resources) For simple resources using ManageRecords, apply the Translatable trait and add the LocaleSwitcher. ```php use Filament\Actions; use Filament\Resources\Pages\ManageRecords; use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; use LaraZeus\SpatieTranslatable\Resources\Pages\ManageRecords\Concerns\Translatable; class ManageBlogPosts extends ManageRecords { use Translatable; protected static string $resource = BlogPostResource::class; protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), Actions\CreateAction::make(), ]; } } ``` ## Translatable Relation Manager Make relation managers translatable by applying the trait and adding the LocaleSwitcher to the table header actions. ```php use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Table; use LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable; class CommentsRelationManager extends RelationManager { use Translatable; protected static string $relationship = 'comments'; // Override to set custom locales for this relation manager public function getTranslatableLocales(): array { return ['en', 'fr', 'de']; } public function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('content') ->searchable(), Tables\Columns\TextColumn::make('created_at') ->dateTime(), ]) ->headerActions([ Tables\Actions\CreateAction::make(), \LaraZeus\SpatieTranslatable\Tables\Actions\LocaleSwitcher::make(), ]) ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make(), ]); } } ``` ## Reactive Locale Inheritance for Relation Managers Inherit the active locale from the parent resource page reactively using Livewire's Reactive attribute. ```php use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Table; use LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable; use Livewire\Attributes\Reactive; class CommentsRelationManager extends RelationManager { use Translatable; protected static string $relationship = 'comments'; // Automatically syncs with parent page's locale #[Reactive] public ?string $activeLocale = null; public function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('content') ->searchable(), ]) ->headerActions([ Tables\Actions\CreateAction::make(), // No LocaleSwitcher needed - inherits from parent ]); } } ``` ## Model Setup with Spatie Translatable Configure your Eloquent model to use Spatie's HasTranslations trait. ```php use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; class BlogPost extends Model { use HasTranslations; protected $fillable = [ 'title', 'slug', 'content', 'published_at', ]; // Define which attributes are translatable public array $translatable = [ 'title', 'content', ]; protected $casts = [ 'published_at' => 'datetime', ]; } ``` ## Publishing Translation Files Publish the package's language files to customize action labels and messages. ```bash php artisan vendor:publish --tag=lara-zeus-spatie-translatable-translations ``` ```php // resources/lang/vendor/lara-zeus-spatie-translatable/en/actions.php return [ 'active_locale' => [ 'label' => 'Language', ], ]; ``` ## Summary Lara Zeus Spatie Translatable is ideal for building multilingual Filament admin panels where content managers need to create and edit content in multiple languages. Common use cases include e-commerce product catalogs with localized descriptions, CMS platforms with multilingual articles, and SaaS applications requiring internationalized user-facing content. The plugin handles the complexity of storing and retrieving translations while providing an intuitive locale switcher interface. Integration follows a consistent pattern: register the plugin with your panel configuration, apply the appropriate Translatable trait to resources, pages, and relation managers, then add LocaleSwitcher actions to page headers or table header actions. The plugin seamlessly integrates with Filament's form and table builders, automatically handling translation storage and retrieval through Spatie's laravel-translatable package without requiring manual JSON manipulation in your application code.