Symfony2 - 为整个表单设置翻译域 [英] Symfony2 - Set translation domain for a whole form

查看:17
本文介绍了Symfony2 - 为整个表单设置翻译域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想翻译一个用 symfony 的表单生成器创建的表单.因为我不想要一个大的翻译文件,所以将其 拆分 为域".

I want to translate a form created with symfony's formbuilder. As i don't want one big translation file it is splitted up into "domains".

现在我必须为每个表单域指定 translation_domain,否则 symfony 会查找错误的文件.这个选项必须添加到每个字段中,我想知道是否有办法将此选项设置为整个表单?

Now i have to specify the translation_domain for each form-field, otherwise symfony will look in the wrong file. This option has to be added to every field and i'm wondering if there is a way to set this option to a whole form?

我不满意的示例代码:

$builder->add(
    'author_name',
    'text',
    array('label' => 'Comment.author_name', 'translation_domain' => 'comment')
)->add(
    'email',
    'email',
    array('label' => 'Comment.email', 'translation_domain' => 'comment')
)->add(
    'content',
    'textarea',
    array('label' => 'Comment.content', 'translation_domain' => 'comment')
);

推荐答案

然后您必须将其设置为表单的默认选项,添加以下内容:

You've then to set it as a default option of your form, add this:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{    
    $resolver->setDefaults(array(
        'translation_domain' => 'comment'
    ));

}

到您的 setDefaultOptions 方法,在您的表单中.

to your setDefaultOptions method, in your form.

更新:已弃用.改用 configureOptions 方法(感谢@Sudhakar Krishnan)

Update: It is deprecated. Use configureOptions method instead (thanks @Sudhakar Krishnan)

这篇关于Symfony2 - 为整个表单设置翻译域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆