【发布时间】:2018-04-18 10:36:20
【问题描述】:
所以我正在使用 symfony 2.8 创建一个多语言网站,但在翻译表单时遇到了这个问题,我设法使用 translation_domain 选项更改标签,如下例所示:
->add('save','submit',
array('label'=>'btn.send',
'translation_domain' => 'FrontBundle',
'attr'=>array(
'class'=>'btn btn-blue',
)))
但是我在翻译实体类型时遇到了问题,因为名称来自数据库,所以我添加了其他语言的字段 像这样:
name_fr ,name_en , name_es ,...
问题是如何将它们与表单一起使用,经过数小时的谷歌搜索后,即使我不喜欢它,我也找到了这个解决方案。
使用 documentation 我将 _local 从请求传递到我的表单,如下所示:
联系人控制器:
public function contactAction(Request $request)
{
$contact = new contact();
$contact->setSendTime(new \DateTime('now'));
$form = $this->createForm(new contactType(), $contact,array('lang'=>$request->getLocale()));
//...
}
联系人类型:
class TaskType extends AbstractType
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
// ...
$resolver->setRequired('lang');
}
//...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$local = $options['lang'];
// ...
$builder
->add('civility', 'entity', array(
'class'=>'BackBundle\Entity\civility',
//use this
'property' => $local == 'fr'?'name_fr':'name_en',
//or this or dont use them both
//'choice_label' => 'name',
'label'=>'Civilité:',
'expanded'=>true,
))
/...
;
}
}
我想知道是否有一个更简洁更好的解决方案来翻译表单中的实体
【问题讨论】:
-
我会阅读文档并给您答复
标签: php symfony symfony-forms