如何在FormType中呈现标签转换?

时间:2016-01-31 22:10:18

标签: php symfony twig

我有表格。

$builder
        ->add('name', TextType::class, array(
            'label' => 'ad_menu.fields.name.label',
            'required' => true,
            'translation_domain' => 'forms'
        ))
        ->add('title', TextType::class, array(
            'label' => 'ad_menu.fields.title.label',
            'required' => true,
            'translation_domain' => 'forms'
        ))
        ->add('description', TextareaType::class, array(
            'label' => 'ad_menu.fields.description.label',
            'translation_domain' => 'forms'
        ))
        ->add('visible', CheckboxType::class, array(
            'label' => 'ad_menu.buttons.edit.label',
            'translation_domain' => 'forms'
        ))
    ;

我在forms.en.yml目录中有translations个文件。

ad_menu:
   fields:
      name:
        label: Name
      title:
        label: Title
      description:
        label: Description
      visible:
        label: Visible
  buttons:
    add:
        label: Add
    edit:
        label: Edit

但是我的表格没有翻译过。

enter image description here

如果我在twig模板中使用例如:{{ 'ad_menu.fields.name.label'|trans({}, 'forms') }} - 它可以工作。

有什么问题?感谢。

更新:

在控制器中:

public function addAction(Request $request)
{
    $adMenu = new AdMenu();
    $form = $this->createForm(new AdMenuType(), $adMenu);

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($adMenu);
        $em->flush();
    }

    return $this->render('AdBackendBundle:Menu:add.html.twig', array(
        'form' => $form->createView()
    ));
}

形式:

    class AdMenuType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, array(
                'label' => 'ad_menu.fields.name.label',
                'required' => true,
//                'translation_domain' => 'forms'
            ))
            ->add('title', TextType::class, array(
                'label' => 'ad_menu.fields.title.label',
                'required' => true,
//                'translation_domain' => 'forms'
            ))
            ->add('description', TextareaType::class, array(
                'label' => 'ad_menu.fields.description.label',
//                'translation_domain' => 'forms'
            ))
            ->add('visible', CheckboxType::class, array(
                'label' => 'ad_menu.buttons.edit.label',
//                'translation_domain' => 'forms'
            ))
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdBackendBundle\Entity\AdMenu',
            'translation_domain' => 'forms'
        ));
    }
}

嫩枝:

{{ form_start(form) }}
    <div class="form-group">
        <label for="ad_menu_title">{{ form.children.title.vars.label }}</label>
        {{ form_widget(form.title, {'attr': {'class': 'form-control', 'id': 'ad_menu_title'}}) }}
    </div>
    <div class="form-group">
        <label for="ad_menu_name">{{ form.children.name.vars.label }}</label>
        {{ form_widget(form.name, {'attr': {'class': 'form-control', 'id': 'ad_menu_name'}}) }}
    </div>
    <div class="form-group">
        <label for="ad_menu_description">{{ form.children.description.vars.label }}</label>
        {{ form_widget(form.description, {'attr': {'class': 'form-control', 'id': 'ad_menu_description'}}) }}
    </div>
    <div class="checkbox">
        <label>
            {{ form_widget(form.visible) }}
            {{ form.children.visible.vars.label }}
        </label>
    </div>
    <input type="submit" class="btn btn-success" value="{{ 'ad_menu.buttons.add.label'|trans({}, 'forms') }}">
{{ form_end(form) }}

2 个答案:

答案 0 :(得分:2)

您必须在“formType”的translation_domain方法中设置configureOptions(),如下例所示:

use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * @param OptionsResolver $resolver
 */
 public function configureOptions(OptionsResolver $resolver)
 {
     $resolver
         ->setDefaults([
             'translation_domain' => 'forms',
         ]);
 }

阅读有关translation domain on form type的相关文档。

基于评论的更新:

错误与渲染标签的方式有关。您需要呈现每个标签:{{ form_label(form.name) }}而不是{{ form.children.name.vars.label }}

阅读how to customize forms

答案 1 :(得分:0)

要在Symfony2完整堆栈框架中获取自动加载目录,您需要使用

project_root/app/Resources/translations/domain.lang.format

project_root/src/(*/)*Bundle/Resources/translations作为路径。

您似乎使用translation代替translations

请参阅http://symfony.com/doc/current/best_practices/i18n.html#translation-source-file-location

修改目录后,您可能需要清除缓存