Boris Guéry
Boris Guéry

Reputation: 47585

How to make an I18N ChoiceList in Symfony2?

I'm extending SimpleChoiceList to provide a Gender <select> to my user.

I'm wondering how I can make the label translatable.

For now, my code looks like:

class GenderChoiceList extends SimpleChoiceList
{
    public function __construct()
    {
        $choices = array(
            UserInterface::GENDER_MALE    => 'user.form.gender.label.male',
            UserInterface::GENDER_FEMALE  => 'user.form.gender.label.female',
        );

        parent::__construct($choices);
    }
}

Is there any way to make this work fluently or do I have to inject the Translator myself?

Upvotes: 0

Views: 290

Answers (1)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

Labels are translated by default already. All you have to do is to put the translations into a messages.LANG.FORMAT file.

Upvotes: 1

Related Questions