在Symfony2类表单中翻译选择选项

29

我正在使用Symfony2 Beta3中的一个类表单,如下所示:

namespace Partners\FrontendBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ConfigForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));
        ...

我想翻译“是”和“否”的选项,但我不知道如何在这里使用翻译器。

5个回答

88

您可以像往常一样使用翻译资源。这对我有效:

    $builder->add('sex', 'choice', array( 
        'choices'   => array(
            1 => 'profile.show.sex.male', 
            2 => 'profile.show.sex.female',
        ),
        'required' => false,
        'label'     => 'profile.show.sex.label',
        'translation_domain' => 'AcmeUserBundle'
    ));

然后将翻译内容添加到您的Bundle的Resources->translations目录中。

@CptSadface更新:

symfony 2.7中,您可以使用choice_label参数来指定翻译域,代码如下:

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

没有指定域,选项不会被翻译。


1
这是真正的答案。+1 - Carrie Kendall
我忘了:无需显式地注入容器或翻译器服务。 - Stefan
7
这有点抽象,但是其中一个选项不应该是“女性”吗?我知道我们在一个与计算机科学相关的网站上,但是… - zopieux
嗨@bingen,我应该把你的示例翻译放在哪里? - jcarlosweb
1
@webyseo 正如我在原始回答中所说,在资源->翻译文件夹中(抱歉回复晚了,我有点断开连接)。 - bingen
显示剩余3条评论

4
我搜索了一段时间才找到答案,最终我发现Symfony如何翻译表单内容。在您的情况下,最简单的方法似乎是通过向应用程序(例如app/Resources/translations/messages.de.yml)或您的bundle添加YAML或XLIFF翻译文件来添加“yes”和“no”的翻译。这里有描述:http://symfony.com/doc/current/book/translation.html 问题在于 - 在我看来 - 您似乎无法使用自定义翻译键。FOSUserBundle中的人们通过“表单主题”(http://symfony.com/doc/2.0/cookbook/form/form_customization.html)解决了这个(或类似的)问题。以下是两行重要代码,以实现将表单元素ID用作翻译键: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig#L1 / https://github.com/FriendsOfSymfony/FOSUserBundle/blob/50ab4d8fdfd324c1e722cb982e685abdc111be0b/Resources/views/form.html.twig#L4 通过添加表单主题,您可以在模板中修改几乎所有表单的内容 - 这似乎是正确的做法。
(抱歉,我必须分割两个链接,因为我没有足够的声望发布超过两个链接。)

我忘记添加所有默认表单主题的URL了 - 一个好的参考:https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig - rkallensee
1
其中一个链接已经失效。 - Mick
3
我修复了那个坏掉的链接。 - rkallensee

3
在Symfony 2.7中,使用choice_label参数时,您可以像这样指定翻译域:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

如果没有指定域名,选项将无法被翻译。


感谢CptSadface和@jxmallett! - bingen

0
Symfony 5.2 添加了可翻译的消息,这简化了这个过程。
namespace Partners\FrontendBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ConfigForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('no_containers', 'choice', [
            'choices' => ['yes' => 1, 'no' => 0],
            'choice_label' => function ($choice, $key) {
                return new TranslatableMessage($key);
            },
        ]);
        ...

0

CptSadface的回答帮助我翻译实体选择。

$builder
    ->add(
        'authorizationRoles',
        null,
        [
            'label' => 'app.user.fields.authorization_roles',
            'multiple' => true,
            'choice_label' => 'name', // entity field storing your translation key
            'choice_translation_domain' => 'messages',
        ]
    );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接