我在 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')));
...
我想翻译"is"和“否”选项,但我不知道如何在此处使用翻译器。
最佳答案
您可以照常使用翻译资源。这对我有用:
$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',
不指定域,选项不会被翻译。
关于forms - 以 Symfony2 类形式翻译选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6187984/