【问题标题】:Translate select options in Symfony2 class forms翻译 Symfony2 类表单中的选择选项
【发布时间】:2011-09-05 11:45:26
【问题描述】:

我在 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')));
        ...

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

【问题讨论】:

    标签: forms symfony internationalization translation choicefield


    【解决方案1】:

    您可以照常使用翻译资源。这对我有用:

        $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
    • 我保存了一条带有复数形式的翻译信息。是否可以在表单类型代码(如@bingen 的代码)中定义要使用的计数?例如。 “[...] 数组(1 => 'profile.show.sex.male{count=3}', 2 => 'profile.show.sex.male')[...]”。因为我有问题,只提供消息名称会显示整个消息,而不仅仅是默认消息(“{0}Links|{1} Link|]1,+Inf[ Links”)
    • 我忘了:不需要显式注入容器或翻译服务。
    • 这有点元,但选项之一不应该是“女性”吗?我知道我们在一个与计算机科学相关的网站上,但仍然……
    • @webyseo 正如我在原始答案中所说,在 Resources->translations 文件夹中(抱歉回复晚了,我有点断线了)
    【解决方案2】:

    我搜索了一段时间以找到答案,但最终我发现了 Symfony 是如何翻译表单内容的。在您的情况下,最简单的方法似乎是通过将 YAML 或 XLIFF 翻译文件添加到您的应用程序(例如 app/Resources/translations/messages.de.yml)或您的包中来添加“是”和“否”的翻译.此处对此进行了描述: 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

    通过添加表单主题,您几乎可以修改模板中的所有表单 - 这似乎是正确的做法。

    (抱歉,我不得不拆分两个链接,因为我没有足够的声誉来发布两个以上的链接。很遗憾。)

    【讨论】:

    【解决方案3】:

    在 symfony 2.7 中,使用 choice_label 参数,您可以像这样指定翻译域:

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

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

    【讨论】:

    • 感谢 CptSadface 和 @jxmallett!
    【解决方案4】:

    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',
            ]
        );
    

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多