来自实体的翻译不起作用 [Symfony2]
Translation From Entity Does Not Work [Symfony2]
我刚刚将 Symfony2 从 2.4 更新到 2.7。除了很多已弃用的调用外,我还发现了一个奇怪的问题。
我在表单中有一个 "select" 标签,我从数据库中获取选项。在数据库中存储了翻译键,并且一直运行良好,symfony 在表格中显示了正确的语言。但不是现在。
一些代码和截图:
表格:
->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'))
)
))
模板:
<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>
Symfony2.4 的表单结果:
使用 Symfony2.7 的表单结果:
如您所见,empty_value 键在两种情况下均已翻译。并且选项值键没有出现在调试器中,就好像存在一样。我认为是因为翻译先于学说加载,但我不知道如何解决它。
谢谢。
我找到了解决方案:
在 2.7 中引入了 choice_translation_domain 以避免翻译选项。
->add('category', 'entity', array(
'choice_translation_domain' => true,
));
我刚刚将 Symfony2 从 2.4 更新到 2.7。除了很多已弃用的调用外,我还发现了一个奇怪的问题。
我在表单中有一个 "select" 标签,我从数据库中获取选项。在数据库中存储了翻译键,并且一直运行良好,symfony 在表格中显示了正确的语言。但不是现在。
一些代码和截图:
表格:
->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'))
)
))
模板:
<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>
Symfony2.4 的表单结果:
使用 Symfony2.7 的表单结果:
如您所见,empty_value 键在两种情况下均已翻译。并且选项值键没有出现在调试器中,就好像存在一样。我认为是因为翻译先于学说加载,但我不知道如何解决它。
谢谢。
我找到了解决方案:
在 2.7 中引入了 choice_translation_domain 以避免翻译选项。
->add('category', 'entity', array(
'choice_translation_domain' => true,
));