Take the 2-minute tour ×

I'm using a class form in Symfony2 Beta3 as follows:

namespace Partners\FrontendBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ConfigForm extends AbstractType
    public function buildForm(FormBuilder $builder, array $options)
        $builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));

I want to translate the 'yes' and 'no' options, but I don't know how to use the translator here.

share|improve this question

2 Answers 2

up vote 43 down vote accepted

You can use the translation resources as usual. This worked for me:

        $builder->add('sex', 'choice', array( 
            'choices'   => array(1 => 'profile.show.sex.male', 2 => 'profile.show.sex.male'),
            'required' => false,
            'label'     => 'profile.show.sex.label',
            'translation_domain' => 'AcmeUserBundle'

And then add your translations to the Resources->translations directory of your Bundle.

share|improve this answer
This is the real answer. +1 –  Carrie Kendall Jun 16 '13 at 3:07
I have saved a translation message with pluralization. Is it possible to define in a form type code (like the one of @bingen) which count to use? E.g. "[...] array(1 => 'profile.show.sex.male{count=3}', 2 => 'profile.show.sex.male') [...]". Because I have the problem, that just providing the message name shows the whole message instead of just a default one ( "{0}Links|{1} Link|]1,+Inf[ Links") –  Stefan Dec 12 '13 at 10:27
I forgot: without needing to inject a container or the translator service explicitly. –  Stefan Dec 12 '13 at 10:28
This is a bit meta, but shouldn't one of the choice items be "female"? I know we're on a computer science related website, but still… –  Zopieux Dec 30 '13 at 20:18
hy @bingen Where I put the translations of your example? –  webyseo Feb 22 at 0:06

I searched a while to find an answer, but finally I found out how Symfony translates form content. The easiest way in your case seems to be to just add a translation for "yes" and "no" by adding a YAML or XLIFF translation file to your application (e.g. app/Resources/translations/messages.de.yml) or your bundle. This is described here: http://symfony.com/doc/current/book/translation.html

The problem - in my opinion - is that you don't seem to be able to use custom translation keys. The guys from FOSUserBundle solve this (or a similar) problem with "Form Themes" (http://symfony.com/doc/2.0/cookbook/form/form_customization.html). Here are two significant lines of code to achieve the usage of the form element id as translation key:

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig#L1 / https://github.com/FriendsOfSymfony/FOSUserBundle/blob/50ab4d8fdfd324c1e722cb982e685abdc111be0b/Resources/views/form.html.twig#L4

By adding a Form Theme you're able to modify pretty much everything of the forms in the templates - this seems to be the right way of doing this.

(Sorry, I had to split two of the links b/c I don't have enough reputation to post more than two links. Sad.)

share|improve this answer
I forgot to add the URL of all default form themes - a good reference: github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/… –  rkallensee Aug 18 '11 at 18:16
One of the link is broken. –  Patt Jan 17 at 20:26
I fixed the broken link. –  rkallensee Jan 20 at 9:37

Your Answer


