Make components globally translatable
- Published: 29 May 2024 Updated: 30 May 2024
Translating components can often be a repetitive task, Fortunately, there's a neat trick to automate this process, making your components instantly translatable.
Introduction
Translating components can often be a repetitive task, requiring you to individually add translateLabel
or __()
to each component. Fortunately, there's a neat trick to automate this process, making your components instantly translatable. By modifying your AppServiceProvider.php
, you can apply a global translation configuration to all relevant components. Here's how to do it:
AppServiceProvider
In this example we will make the following components translatable by default if you would like to add more components you can do so by adding them to the array
Filament\Forms\Components\Field
Filament\Tables\Filters\BaseFilter
Filament\Forms\Components\Placeholder
Filament\Tables\Columns\Column
Filament\Infolists\Components\Entry
Boot
Navigate to your AppServiceProvider.php
file located in the app/Providers
directory of your Laravel project and add the following to your boot method:
1foreach ([Field::class, BaseFilter::class, Placeholder::class, Column::class, Entry::class] as $component) {2 /* @var Configurable $component */3 $component::configureUsing(function (Component $translatable): void {4 $translatable->translateLabel();5 });6}
Imports
Make sure you import the necessary classes at the top of your AppServiceProvider.php
file:
1use Filament\Forms\Components\Placeholder;2use Filament\Support\Components\Component;3use Filament\Infolists\Components\Entry;4use Filament\Tables\Filters\BaseFilter;5use Filament\Forms\Components\Field;6use Filament\Tables\Columns\Column;
I hope this trick helps you save time and effort when translating components in your Laravel project. If you have any questions or need further assistance, feel free to reach out to me. Happy coding!
Related Tricks:
toggle the visibility of an action when hover the component
Sometimes, when we need to test our application locally, we always have to fill forms manually. Let's take the Filament approach and achieve this with more accurate data.
make all Field or any components translatable
Translating components can often be a repetitive task, Fortunately, there's a neat trick to automate this process, making your components instantly translatable.
I’ll guide you on how to test your Form Builder using Livewire Volt with a class-based component.