Symfony 2-表单集合-标签翻译



我正在尝试翻译我的小树枝中的一个标签。我有一个基本的联系表格,包括名字,姓氏,电话,电子邮件。我有一个名为"BookContact"的集合,我可以在其中添加许多联系人。用户可以通过单击"添加联系人"按钮生成一个新的联系人表单(使用原型的jQuery事件,如下所述:http://symfony.com/fr/doc/current/cookbook/form/form_collections.html,我不处理taks和tafgs,而是处理BookContact和Contact)。

当我在树枝上显示我的收藏时:

{% for contact in form_book_contact.contacts %}
Contact n° {{ num_contact }}
    <div class="row" id="bookcontacts" data-prototype="{{ form_widget(form_book_contact.contacts.vars.prototype)|e }}">
        <div class="col-md-6">
            <div class="form-group">
               {{ form_widget(contact.firstname) }}
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
               {{ form_widget(contact.lastname) }}
            </div>
          ....
 {% endfor %}

输入看起来像:

 <input type="text" class="form-control" placeholder="0.lastname" name="book_contact[contacts][0][lastname]" id="book_contact_contacts_0_lastname">

我的翻译文件有:

 book_contact:
     firstname:            "Prénom"
     lastname:             "Nom"
.....

在这种情况下,翻译不起作用(这是正常的,因为输入的名称不是"firstname"而是"0.firsname"。问题是我无法处理联系人表格的生成数量。当用户点击"添加联系人"按钮时,输入看起来像:"1.名字"等…

我该如何处理这种翻译?如何管理翻译文件中的数字更改?

谢谢你的帮助。

由于标签已经在默认的表单布局中进行了转换,您只需要在表单类型中设置它。

因此,如果你有一个表单类型:

class BookContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname')
            ->add('lastname')
        ;
    }
}

然后只需使用模板中的form_label(form.firstName)并将其转换为消息。fr.yml:

Firstname: "Prénom" # Do not forget the first uppercased character
Lastname:  "Nom"

或者,如果您喜欢使用翻译前缀:

class BookContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', null, [
                'label' => 'book_contact.firstname',
            ])
            ->add('lastname', null, [
                'label' => 'book_contact.lastname',
             ])
        ;
    }
}

并使用以下消息.fr.yml:

book_contact:
    firstname:            "Prénom"
    lastname:             "Nom"

最新更新