Symfony 2 JMSTranslationsBundle Form,可选择如何创建翻译术语


Symfony 2 JMSTranslationsBundle Form with choice how to create translation terms?

我有一个简单的表单,其选择类型是一个简单的实体:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'entity', [
            'class' => 'AppBundle'Entity'Type',
                'property' => 'name',
                'expanded' => true
        ]
    );
}

实体type是一个数据库表,只有一个idType AI column和一个name column

数据库内容也非常简单:

idType      name
"1"         "term.inquiry"
"2"         "term.tender"

树枝文件的结果看起来也很简单:

{{ form_start(form) }}
    {{ form_widget(form.name) }}
{{ form_end(form) }}

我真的很纠结于如何实现翻译方法来翻译term.inquiryterm.tender

我在这里查找了文档,但我不知道如何通过TranslationContainerInterface实现(正确的方式)getTranslationMessages方法。我也在这里尝试了答案,但不知道如何将返回数组与我的buildForm方法中的选择相匹配。

对最佳实践实施有什么建议或提示吗?提前非常感谢

您可能正在搜索 Symfony 2.7 中引入choice_translation_domain选项以及choice_label弃用property

见 http://symfony.com/doc/current/reference/forms/types/choice.html#choice-translation-domain

如果您的翻译是src/AppBundle/Resources/translations/entity.en.yml,则包含:

term:
    inquiry: 'A name'
    tender: 'Another name'

你可以这样写:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'entity', array(
            'class' => 'AppBundle'Entity'Type',
            //'property' => 'name', is deprecated use 'choice_label' instead
            'expanded' => true,
            'choice_label' => 'name',
            'choice_translation_domain' => 'entity',
        ));
}