Easyadminbundle: Translatable formatValue()

Created on 6 Jul 2020  路  7Comments  路  Source: EasyCorp/EasyAdminBundle

Hi,

It would be nice to make the formatValue() function translatable:

TextField::new('firstName', 'Name')
    ->formatValue(function ($value) {
        return 'name.label' . $value;
    })

Thanks :)

feature wontfix

Most helpful comment

You are right. My opinion about translation domains: unless your application is colossal in size or complexity, don't use them. They give you a false sense of "decoupling things" ... but they only complicate everything. If you need to separate "normal translations" from "admin translations", use admin. as the prefix your admin translations inside the messages file (if you use PHP, or even YAML) adding this prefix to all admin translation takes no time.

All 7 comments

Can you give an example where this would be useful? I'm not opposed or anything, just don't really understand what you're trying to accomplish in this code snippet 馃槄

No problem :)

Here is the code:

yield ChoiceField::new('duration', 'duration')
    ->formatValue(function ($value) {
        return 'duration.' . $value;
    })
    ->setChoices(array(15 => 15, 30 => 30, 45 => 45, 60 => 60));

The idea then is to have:

  • 15 mins
  • 30 mins
  • 45 mins
  • 1 hour

Is that clearer ? :)

Yeah, makes more sense now - maybe just put this in the issue description instead 馃檪

My suggestion would be to create a FieldConfiguratorInterface that runs after the CommonPostConfigurator (which calls formatValue), and then overrides the value with the translation if necessary / desired.

Do you have a suggestion on how to "mark" the value as being translatable? (You probably don't want to try to translate the values of every ChoiceField). Maybe this could also become part of the core functionality, by adding a property to FieldTrait, which is then checked in CommonPostConfigurator 馃

Indeed, I will not try to translate the values of every ChoiceField.

@seb-jean thanks for your proposal, but for now I'm not interested in adding this feature to EasyAdmin because I think it's not common enough.

You can solve this today like this:

class SomeCrudController extends AbstractCrudController
{
    private $translator;

    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    // ...

    public function configureFields(string $pageName): iterable
    {
        $translator = $this->translator;

        // ...

        yield ChoiceField::new('duration', 'duration')
            ->formatValue(static function ($value) use ($translator) {
                return $translator->trans('duration.' . $value);
            })
            ->setChoices([15 => 15, 30 => 30, 45 => 45, 60 => 60]);
    }
}

Please, remember that EasyAdmin is a "thin" layer on top of Symfony ... so you can use all Symfony features, all Symfony services, etc.

With your solution it works but on the other hand you have to add the translation domain :)

You are right. My opinion about translation domains: unless your application is colossal in size or complexity, don't use them. They give you a false sense of "decoupling things" ... but they only complicate everything. If you need to separate "normal translations" from "admin translations", use admin. as the prefix your admin translations inside the messages file (if you use PHP, or even YAML) adding this prefix to all admin translation takes no time.

Was this page helpful?
0 / 5 - 0 ratings