2014-01-12 27 views
0

我的项目是为了多语言,我很远没有完成,但到目前为止,我所有的树枝模板都使用了trans过滤器。构建表单时,哪里是设置标签值的最佳位置?

现在当构建表单时,我希望我的标签也能被翻译,到目前为止,这里是几个formbuilder类之一,问题是:我应该在这里还是在树枝模板内设置标签的值,以便我可以再次使用小枝“反式”过滤器?

<?php 

namespace MG\AdminBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class CustomersHomesType extends AbstractType 
{ 

    private $customersId = null; 

    public function __construct($customersId){ 
     $this->customersId = $customersId; 
; 
    } 
     /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('customers_id', 'hidden', array('data'=>$this->customersId,'required'=>true)) 
      ->add('name', 'text', array('label'=>'Name:','required'=>true)) 
      ->add('streetAddress1', 'text', array('label'=>'Street Address:','required'=>true)) 
      ->add('streetAddress2', 'text', array('label'=>'Street Address:','required'=>false)) 
      ->add('city', 'text', array('label'=>'City:','required'=>true)) 
      ->add('state', 'text', array('label'=>'State:','required'=>true)) 
      ->add('zipcode', 'text', array('label'=>'Zip Code:','required'=>true)) 
      ->add('type', 'choice', array(
        'expanded'=>false, 
        'label'=>'Address Type:', 
        'choices' => array('billing' => 'Billing Address ', 'work' => 'Work Address ', 'shipping'=>'Shipping Address '), 
        'required' => true, 
      )) 
      ->add('subdivision', 'text', array('label'=>'Subdivision:','required'=>false)) 
      ->add('directions', 'textarea', array('label'=>'Directions Or Comments:','required'=>false)) 

      ->add('phone', 'text', array('label'=>'Phone #1:','required'=>false)) 
      ->add('phone2', 'text', array('label'=>'Phone #2:','required'=>false)) 
      ->add('save', 'submit', array('label'=>'Save')) 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'MG\AdminBundle\Entity\CustomersHomes' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'mg_adminbundle_customershomes'; 
    } 
} 

回答

2

朗天回答你的问题....

到在形式,而是使用翻译添加标签最佳位置。

在您的形式..

->add('blah', 'text', array(
    ... etc ... 
    'label' => 'vendor.bundle.field.label', 
    // For bundle named Vendor\Bundle 
) 

然后在你的翻译文件(位于`参考资料/译/消息。[区域]。[文件](见http://symfony.com/doc/current/book/translation.html)(这是YAML格式)

vendor: 
    bundle: 
     field: 
      label: the label that you wanted to show 
     field2: 
      biscuits: you can call it what ever you want 

这种方式可以让一帮翻译文件,然后让你的网站多语种(提供文件都在那里,它返回到您的地方设置或以其他方式回退),而不是以后挖在编辑实际文本的日期。还有只有一个文件夹(或一组文件夹Resources/translations,您需要稍后更新,而不是编辑实际的类数据。

这个系统也适用于任何种类的验证,但这些翻译需要放在Resources/translations/validators.[locale].[filetype]的地方。仍然非常方便。

如果它们没有立即显示,请尝试清除缓存。

+0

我非常感激你的时间,我发现通过提问我学得更快,我还保留一个很好的参考来回到这里在stackoverflow。再次,谢谢你。 – MikeGA

+1

没问题,很乐意帮忙。正如我在另一个答案中提到的那样。寻找Symfony所能提供的东西的好地方是Sylius Bundle(https://github.com/Sylius/Sylius)以及FOSUserBundle(https:// github.com/FriendsOfSymfony/FOSUserBundle)。 Symfony CMF包也可能是一个很好的来源,但据我所知它利用了我没有投入任何时间的PHPCR,所以我几乎远离它,这意味着我不能证明它是在学习美德。 – qooplmao

相关问题