Translate select options in Symfony2 class forms
Asked Answered
S

5

30

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.

Sfax answered 31/5, 2011 at 13:3 Comment(0)
S
89

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.female',
        ),
        'required' => false,
        'label'     => 'profile.show.sex.label',
        'translation_domain' => 'AcmeUserBundle'
    ));

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

Update from @CptSadface:

In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

Without specifying the domain, options are not translated.

Sword answered 4/1, 2013 at 2:0 Comment(8)
This is the real answer. +1Schramm
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")Dyer
I forgot: without needing to inject a container or the translator service explicitly.Dyer
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…Siloa
hy @Sword Where I put the translations of your example?Expedition
@webyseo As I said in my original answer, in Resources->translations folder (sorry for the late response, I have been a little bit disconnected)Sword
@Zopieux female gender typo fixed. Thanks for noticing. ;-)Sword
'choice_label' and 'choice_translation_domain' continue to work in Symfony 3. You can also use the EntityType::class to read options from an entity (database) instead of manually adding them. The 'choice_label' would be the field in the database you want to display in the select options. For instance I use a Category entity which reads the category table and instead of reading the "name" field I created a "translation" field that holds the translation labels and I display them instead.Papacy
P
4

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.)

Publication answered 18/8, 2011 at 18:14 Comment(3)
I forgot to add the URL of all default form themes - a good reference: github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/…Publication
One of the link is broken.Epsilon
I fixed the broken link.Publication
N
3

In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

Without specifying the domain, options are not translated.

Nimiety answered 31/7, 2015 at 9:33 Comment(0)
T
0

CptSadface's answer was what helped me with translating my entity choices.

$builder
    ->add(
        'authorizationRoles',
        null,
        [
            'label' => 'app.user.fields.authorization_roles',
            'multiple' => true,
            'choice_label' => 'name', // entity field storing your translation key
            'choice_translation_domain' => 'messages',
        ]
    );
Tound answered 8/9, 2015 at 11:46 Comment(0)
K
0

Symfony 5.2 adds translatable messages which simplifies this.

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', [
            'choices' => ['yes' => 1, 'no' => 0],
            'choice_label' => function ($choice, $key) {
                return new TranslatableMessage($key);
            },
        ]);
        ...
Kirkcudbright answered 26/7, 2023 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.