简体   繁体   中英

Translation From Entity Does Not Work [Symfony2]

I just update Symfony2 from 2.4 to 2.7. Besides a lot of deprecated calls, I have found one weird problem.

I have a "select" tag within a form and I get the options from database. In database are stored the translation keys, and always has worked fine, symfony showed the correct language in the form. But not now.

Some code and screenshots:

Form:

->add('category', 'entity', array(
    'empty_value' => 'Event.form.label.category_empty',
    'class' => 'EventBundle:Category',
    'choice_label' => 'name',
    'error_bubbling' => true,
    'constraints' => array(
        new NotBlank(array('message' => 'Event.form.error.category.notblank'))
    )
))
->add('subcategory', 'entity', array(
    'class' => 'EventBundle:Subcategory',
    'choice_label' => 'name',
    'error_bubbling' => true,
    'constraints' => array(
        new NotBlank(array('message' => 'Event.form.error.subcategory.notblank'))
    )
))

Template:

<div class="form-group col-sm-6 input-group">
    <label for="event_category" class="input-group-addon">{{ 'Event.form.label.category' | trans }}</label>
    {{ form_widget(form.category, { 'attr': { 'data-ott-subcaturl' : path('get_subcategories') , 'class' : 'form-control ev-category'} }) }}
</div>
<div class="form-group col-sm-6 input-group">
    <label for="event_subcategory" class="input-group-addon">{{ 'Event.form.label.subcategory' | trans }}</label>
    {{ form_widget(form.subcategory, { 'attr': { 'class' : 'form-control ev-subcategory'} }) }}
</div>

Form result with Symfony2.4:

用Symfony2.4形成结果

Form result with Symfony2.7:

用Symfony2.7形成表格结果

As you see, the empty_value key is translated in both cases. And the option values keys does not appear in debugger, as if there were. I think is because translations load before doctrine, but I don't know how fix it.

Thanks.

I found the solution:

In 2.7 was introduced the choice_translation_domain to avoid translating options.

http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#added-choice-translation-domain-domain-to-avoid-translating-options for more details

->add('category', 'entity', array(
    'choice_translation_domain' => true,
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM