doxd96148 2016-01-28 10:49
浏览 14

Symfony 2 JMSTranslationsBundle表格可以选择如何创建翻译术语?

I have a simple form with a choice type which is an simple entity:

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

The entity type is a database table with just an idType AI column and a name column.

The database content is also pretty simple:

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

And the result in a twig file looks pretty simple too:

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

I'm really stucked in how to implement the translation method to translate the term.inquiry and term.tender

I've looked up the documentation here, but I don't know how to implement (the correct way) the getTranslationMessages method via the TranslationContainerInterface. I've also tried the answer here but didn't know how to match the return array with the choices in my buildForm method.

Any suggestions or hints for a best practice implementation? Many thanks in advance

  • 写回答

1条回答 默认 最新

  • duanou2016 2016-01-30 08:19
    关注

    you're maybe searching for the option choice_translation_domain introduced in symfony 2.7 as well as choice_label deprecating property.

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

    if your translations are in src/AppBundle/Resources/translations/entity.en.yml, containing :

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

    You can write something like :

    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',
            ));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题