Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[READ ONLY] Subtree split of the Filament spatie/laravel-translatable Plugin (see filamentphp/filament)

NotificationsYou must be signed in to change notification settings

filamentphp/spatie-laravel-translatable-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Installation

Install the plugin with Composer:

composer require filament/spatie-laravel-translatable-plugin:"^3.2" -W

Adding the plugin to a panel

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

useFilament\SpatieLaravelTranslatablePlugin;publicfunctionpanel(Panel$panel):Panel{return$panel// ...        ->plugin(SpatieLaravelTranslatablePlugin::make());}

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 thedefaultLocales() plugin method:

useFilament\SpatieLaravelTranslatablePlugin;publicfunctionpanel(Panel$panel):Panel{return$panel// ...        ->plugin(            SpatieLaravelTranslatablePlugin::make()                ->defaultLocales(['en','es']),        );}

Preparing your model class

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

Preparing your resource class

You must apply theFilament\Resources\Concerns\Translatable trait to your resource class:

useFilament\Resources\Concerns\Translatable;useFilament\Resources\Resource;class BlogPostResourceextends Resource{use Translatable;// ...}

Making resource pages translatable

Afterpreparing your resource class, you must make each of your resource's pages translatable too. You can find your resource's pages in thePages directory of each resource folder. To prepare a page, you must apply the correspondingTranslatable trait to it, and install aLocaleSwitcher header action:

useFilament\Actions;useFilament\Resources\Pages\ListRecords;class ListBlogPostsextends ListRecords{useListRecords\Concerns\Translatable;protectedfunctiongetHeaderActions():array    {return [Actions\LocaleSwitcher::make(),// ...        ];    }// ...}
useFilament\Actions;useFilament\Resources\Pages\CreateRecord;class CreateBlogPostextends CreateRecord{useCreateRecord\Concerns\Translatable;protectedfunctiongetHeaderActions():array    {return [Actions\LocaleSwitcher::make(),// ...        ];    }// ...}
useFilament\Actions;useFilament\Resources\Pages\EditRecord;class EditBlogPostextends EditRecord{useEditRecord\Concerns\Translatable;protectedfunctiongetHeaderActions():array    {return [Actions\LocaleSwitcher::make(),// ...        ];    }// ...}

And if you have aViewRecord page for your resource:

useFilament\Actions;useFilament\Resources\Pages\ViewRecord;class ViewBlogPostextends ViewRecord{useViewRecord\Concerns\Translatable;protectedfunctiongetHeaderActions():array    {return [Actions\LocaleSwitcher::make(),// ...        ];    }// ...}

If you're using a simple resource, you can make theManageRecords page translatable instead:

useFilament\Actions;useFilament\Resources\Pages\ManageRecords;class ManageBlogPostsextends ListRecords{useManageRecords\Concerns\Translatable;protectedfunctiongetHeaderActions():array    {return [Actions\LocaleSwitcher::make(),// ...        ];    }// ...}

Setting the translatable locales for a particular resource

By default, the translatable locales can beset globally for all resources in the plugin configuration. Alternatively, you can customize the translatable locales for a particular resource by overriding thegetTranslatableLocales() method in your resource class:

useFilament\Resources\Concerns\Translatable;useFilament\Resources\Resource;class BlogPostResourceextends Resource{use Translatable;// ...publicstaticfunctiongetTranslatableLocales():array    {return ['en','fr'];    }}

Translating relation managers

First, you must apply theFilament\Resources\RelationManagers\Concerns\Translatable trait to the relation manager class:

useFilament\Resources\RelationManagers\Concerns\Translatable;useFilament\Resources\RelationManagers\RelationManager;class BlogPostsRelationManagerextends RelationManager{use Translatable;// ...}

Now, you can add a newLocaleSwitcher action to the header of the relation manager'stable():

useFilament\Tables;useFilament\Tables\Table;publicfunctiontable(Table$table):Table{return$table        ->columns([// ...        ])        ->headerActions([// ...Tables\Actions\LocaleSwitcher::make(),        ]);}

Inheriting the relation manager's active locale from the resource page

If you wish to reactively inherit the locale of theTranslatable resource page that the relation manager is being displayed on, you can override the$activeLocale property and add Livewire'sReactive attribute to it:

useFilament\Resources\RelationManagers\Concerns\Translatable;useFilament\Resources\RelationManagers\RelationManager;useLivewire\Attributes\Reactive;class BlogPostsRelationManagerextends RelationManager{use Translatable;        #[Reactive]public ?string$activeLocale =null;// ...}

If you do this, you no longer need aLocaleSwitcher action in thetable().

Setting the translatable locales for a particular relation manager

By default, the translatable locales can beset globally for all relation managers in the plugin configuration. Alternatively, you can customize the translatable locales for a particular relation manager by overriding thegetTranslatableLocales() method in your relation manager class:

useFilament\Resources\RelationManagers\Concerns\Translatable;useFilament\Resources\RelationManagers\RelationManager;class BlogPostsRelationManagerextends RelationManager{use Translatable;// ...publicfunctiongetTranslatableLocales():array    {return ['en','fr'];    }}

Publishing translations

If you wish to translate the package, you may publish the language files using:

php artisan vendor:publish --tag=filament-spatie-laravel-translatable-plugin-translations

About

[READ ONLY] Subtree split of the Filament spatie/laravel-translatable Plugin (see filamentphp/filament)

Resources

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp