2015-02-09 79 views
1

我試圖翻譯我的樹枝中的標籤。 我有一個名字,姓氏,電話,電子郵件的基本聯繫方式。 我有一個名爲「BookContact」的集合,我可以添加許多聯繫人。 用戶可以通過單擊「添加聯繫人」按鈕(使用原型的jQuery事件,如此處所述:http://symfony.com/fr/doc/current/cookbook/form/form_collections.html,我沒有使用taks和tafgs,但使用BookContact和Contact)來生成新的聯繫人表單。Symfony 2 - 表單集合 - 標籤翻譯

當我在樹枝顯示我的收藏:

{% 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.firstname」等...

我該如何處理這種翻譯?我如何管理這個數字在我的翻譯文件中變化?

感謝您的幫助。

回答

0

由於標籤已在the default form layout中翻譯,您只需將其設置爲您的表單類型即可。

所以,如果你有一個表單類型:

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

然後只需使用form_label(form.firstName)在你的模板並將其轉化爲messages.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', 
      ]) 
     ; 
    } 
} 

並使用以下messages.fr.yml:

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