向表单标签添加前缀以进行翻译

发布于 2024-12-25 00:13:32 字数 934 浏览 0 评论 0原文

我有一些 *Type 表单类和一个 forms.html.twig 来自定义表单外观。默认情况下,在此文件中,标签使用此块呈现:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>

我想向标签添加前缀,以便组织我的翻译。例如,假设我有一个 CustomerType,那么我希望我的标签如下:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ 'Customer.' ~ label|trans }}
</label>

我希望能够将 'Customer' 字符串传递给 FormBuilder这样我就可以像这样使用它:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ prefix ~ '.' ~ label|trans }}
</label>

或者也许:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ form.prefix ~ '.' ~ label|trans }
}</label>

有人知道如何实现这一目标吗?

I have some *Type form classes and a forms.html.twig to customize form appearance. By default, in this file the labels are rendered with this block:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>

I'd like to add a prefix to the label ir order to organize my translations. For example let's say I have a CustomerType, then I'd like my labels to be like:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ 'Customer.' ~ label|trans }}
</label>

I want to be able to pass that 'Customer' string to the FormBuilder in such a way that I am able to use it like:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ prefix ~ '.' ~ label|trans }}
</label>

or maybe:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ form.prefix ~ '.' ~ label|trans }
}</label>

Does someone know how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还不是爱你 2025-01-01 00:13:32

使用 Symfony 2.1

<?php
namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormViewInterface $view, FormInterface $form, array $options)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->setVar('label', $labelPrefix.$view->getVar('label'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'label_prefix' => ''
        ));
    }

    public function getExtendedType()
    {
        return 'form';
    }
}

将此扩展声明为服务

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="form" />
</service>

有关更多信息,查看文档

With Symfony 2.1

<?php
namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormViewInterface $view, FormInterface $form, array $options)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->setVar('label', $labelPrefix.$view->getVar('label'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'label_prefix' => ''
        ));
    }

    public function getExtendedType()
    {
        return 'form';
    }
}

Declare this extension as a service

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="form" />
</service>

For more informations, see the documentation.

请你别敷衍 2025-01-01 00:13:32

为此,只需显式设置标签:

$builder->add('firstName', 'text', array(
    'label' => 'customer.first_name'
));

To do that, just set the label explicitly:

$builder->add('firstName', 'text', array(
    'label' => 'customer.first_name'
));
溺ぐ爱和你が 2025-01-01 00:13:32

有一个简单的解决方案:“类型扩展”

创建一个如下所示的类:

<?php

namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormView $view, FormInterface $form)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->set('label', $labelPrefix.$form->getAttribute('label'));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'label_prefix' => '',
        );
    }

    public function getExtendedType()
    {
        return 'field';
    }
}

将此扩展声明为服务

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="field" />
</service>

并在表单中使用此选项:

$form = $this->createFormBuilder($entity, array('label_prefix' => 'mylabelprefix.'))
    ->add('link', 'url')
    ->getForm();

您将拥有一个标签“mylabelprefix.link”。

享受!

There is a simple solution: "Type extension"

Create a class like this:

<?php

namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormView $view, FormInterface $form)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->set('label', $labelPrefix.$form->getAttribute('label'));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'label_prefix' => '',
        );
    }

    public function getExtendedType()
    {
        return 'field';
    }
}

Declare this extension as a service

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="field" />
</service>

And use this option in your form:

$form = $this->createFormBuilder($entity, array('label_prefix' => 'mylabelprefix.'))
    ->add('link', 'url')
    ->getForm();

You'll have a label "mylabelprefix.link".

Enjoy!

微暖i 2025-01-01 00:13:32

我发现给定的答案不起作用,但我不被允许发表评论。它不起作用的原因是 $view->getVar()$view->setVar() 方法实际上并不存在。为了完成这项工作,我必须使我的 buildView() 方法如下所示:

public function buildView(Form\FormView $view, Form\FormInterface $form, array $options)
{
    if ($options['label_prefix']) {
        $view->vars['label'] = $options['label_prefix'] . ': ' . $view->vars['label'];
    }
}

然后将其设置为默认为 null,如下所示:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults([
        'label_prefix' => null,
    ]);
}

希望这会有所帮助!

I found that the given answer didn't work, but I'm not allowed to comment. The reason it didn't work is that the $view->getVar() and $view->setVar() methods don't actually exist. To get this work, I had to make my buildView() method look like this:

public function buildView(Form\FormView $view, Form\FormInterface $form, array $options)
{
    if ($options['label_prefix']) {
        $view->vars['label'] = $options['label_prefix'] . ': ' . $view->vars['label'];
    }
}

then set it to default to null like so:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults([
        'label_prefix' => null,
    ]);
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文