F
Filament2y ago
John

Global translation for all model fields?

I'd like to use a nested array with models and translated field names. And use them globally for columns and fields:
<?php

namespace App\Providers;

use Filament\Forms\Components\Field;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
Field::configureUsing(function (Field $field) {
$model = 'post';
$fieldName = $field->getName();
$field->label(__("models.$model.$fieldName"));
});
}
}
<?php

namespace App\Providers;

use Filament\Forms\Components\Field;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
Field::configureUsing(function (Field $field) {
$model = 'post';
$fieldName = $field->getName();
$field->label(__("models.$model.$fieldName"));
});
}
}
Is there any way to dynamically determine the $model from the calling component?
3 Replies
John
JohnOP2y ago
I tried something like this, in order to set the model which I can then use in the service provider. But I don't know which method I could use for this:
protected function getTableColumns(): array
{
return collect([
TextColumn::make('name'),
TextColumn::make('email'),
])->transform(function (TextColumn $column) {
$column->extraAttributes(['model' => 'Post']);
})->all();
}
protected function getTableColumns(): array
{
return collect([
TextColumn::make('name'),
TextColumn::make('email'),
])->transform(function (TextColumn $column) {
$column->extraAttributes(['model' => 'Post']);
})->all();
}
I've got something working without ::configureUsing:
protected function getFormSchema(): array
{
return collect([
TextInput::make('organisation')
->required(),
TextInput::make('brin'),
TextInput::make('city'),
])
->transform(fn(Field $field) => $field->label(__('models.school.' . $field->getName())))
->all();
}
protected function getFormSchema(): array
{
return collect([
TextInput::make('organisation')
->required(),
TextInput::make('brin'),
TextInput::make('city'),
])
->transform(fn(Field $field) => $field->label(__('models.school.' . $field->getName())))
->all();
}
It would still be nice to do this in a service provider, if anyone could give me a better solution...
Dan Harrin
Dan Harrin2y ago
i wouldnt do this from a service provider even if it was easily possible, would be a nightmare to reverse for example, in table filter forms
John
JohnOP2y ago
I could work around that by checking some attribute or checking if a translation in that form is available. But, I get your point and indeed it's quite... obtrusive. I'll stick with the collect()->transform(). Thanks!
Want results from more Discord servers?
Add your server