Florent Destremau
Florent Destremau

Reputation: 653

Configure symfony form label in doctrine config

I am currently on a project where i have a lot of entities with a lot of fields (clients with addresses, phones, age, firm number...), and i am doing it for a French client. So i code in English:

class Client
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $firstName;
}

And i have a config like this:

AppBundle\Entity\Client:
type: entity
table: null
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    firstName:
        type: string
        length: 255
        options:
            label: Prénom

So the options.label doesn't seem to work, i'm wondering where i could do these translations as it will concern a lot of fields, and as I am using Sonata admin i don't want to be obliged to put them in the ClientAdmin class:

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('firstName')
        ->add('lastName')
        ->add('birthDate', 'birthday')
    ;
}

/**
 * @param ShowMapper $showMapper
 */
protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('id')
        ->add('firstName')
        ->add('lastName')
        ->add('birthDate')
    ;
}

because i would have to translate at least twice (form, display...) so I would love it to handle one translation in all the app.

Any idea ? I looked into the Gedmo translatable extension, but it doesn't correspond to what I am looking for: i simply want to translate the labels of the form, the whole application will be in a unique language: french.

Symfony 2.6 Doctrine 2.2 to 2.5

Upvotes: 0

Views: 398

Answers (3)

Florent Destremau
Florent Destremau

Reputation: 653

For those who don't know it, turned out that the automatic labels are already translation keys, you simply need to specify the translation_domain in your form, so i have now a messages.fr.yml:

First Name: Prénom

And it's enough! No need to generate / specify a translation label. Plus it is reusable in between forms with similar fields ! This works within our out of sonata admin's focus.

Upvotes: 1

K-Alex
K-Alex

Reputation: 380

try this:

 /**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Client',
        'label' => 'Prénom'
    ));
}

Upvotes: 0

Stev
Stev

Reputation: 1112

You should take a look at this. It will help you automate the translation process. http://jmsyst.com/bundles/JMSTranslationBundle You'll just have to use keys instead of real labels(although it's optional in your case), like this:

$formMapper
        ->add('firstName',null, array('label'=>'sonata.user.firstName')

Then you just run a command and it will extract all the keys in a nice web GUI, and you can translate them from there in whatever language you want.

Upvotes: 0

Related Questions