Issue with FilamentSpatieTranslatable plugin LocaleSwitcher not saving record after v 3 upgrade
Hi everyone,
I'm encountering a problem with the LocaleSwitcher from the FilamentSpatieTranslatable plugin (https://filamentphp.com/plugins/filament-spatie-translatable) after upgrading from Filament 2 to Filament 3.
In Filament 2, the LocaleSwitcher would change the language and automatically save the current record.(By using the updateRecord function in the SpatieLaravelTranslatableContentDriver) However, after upgrading to Filament 3 and updating the FilamentSpatieTranslatable plugin, it only updates the language without saving the record.
I've tried to implement record saving using approaches I've seen mentioned for Filament 3 actions, such as:
- Using
reactive()
- Using onStateUpdated()
- Using afterStateUpdated()
However, none of these methods seem to be available on the LocaleSwitcher provided by the plugin.Filament
Spatie Translatable by Filament - Filament
Filament support for Spatie's Laravel Translatable package.
6 Replies
the local Locale Switcher wont save any data to your model.
it will only switch the local, and save all data for all locales after pressing save button.
it also depend on your form schema, is it a relation or direct inputs in the model?
How can i use here for unique title is i selected de by select drop down not working ?
i Mean validation not work
https://prnt.sc/Nuf2lE0oVyJ_
->rules([
function (Forms\Get $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
$locale = $get('activeLocale'); // Get selected language from Filament's LocaleSwitcher
dd($locale);
if (!$locale) {
$locale = config('app.locale'); // Fallback to default locale
}
// Check uniqueness in the specific language field (title->en or title->de)
$existing = Blog::where("title->{$locale}", $value)->exists();
if ($existing) {
$fail(__('This title already exists in the selected language: ' . strtoupper($locale)));
}
};
},
])
Any one have idea how its work ?
To save data as unique title with Translatable ?
I solved by this is there any another way or will work according selected lang?
->rules([
function () {
return function (string $attribute, $value, Closure $fail) {
$locales = ['en', 'de']; // List of supported locales (can be dynamic)
foreach ($locales as $locale) {
$existing = Blog::where("title->{$locale}", $value)->exists();
if ($existing) {
$fail(__('This title already exists in: ' . strtoupper($locale)));
break; // Stop checking after the first duplicate
}
}
};
},
])
yes your way is the same I used here
you can get the active local from:
$livewire->activeLocale
https://github.com/lara-zeus/sky/pull/227/files
I will create a rule and include it in the translatable plugin so it can be re usedYes Working fine it
->rules([
function (?Blog $record, $livewire) {
return function (string $attribute, $value, Closure $fail) use ($record, $livewire) {
// Ensure Livewire component has the activeLocale property
$locale = $livewire->activeLocale ?? config('app.locale');
if (!$locale) {
$locale = config('app.locale'); // Fallback to default locale
}
// Debugging: Uncomment to check locale value
// dd($locale);
if (Blog::query()
->where('id', '!=', $record?->id)
->where("title->{$locale}", $value)
->exists()
) {
$fail(__("This :attribute already exists in the selected language: :locale", [
'attribute' => Str::replace('data.', '', $attribute),
'locale' => strtoupper($locale)
]));
}
};
},
])
Thanks
And Alternate we can use this
->rules([
fn(Forms\Get $get) => UniqueTranslationRule::for('blogs', 'slug')
->ignore($get('id')),
])
thank you, this is my final version:
https://github.com/lara-zeus/sky/blob/3.x/src/Rules/UniqueTranslationRule.php