向表单标签添加前缀以进行翻译
我有一些 *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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Symfony 2.1
将此扩展声明为服务
有关更多信息,查看文档。
With Symfony 2.1
Declare this extension as a service
For more informations, see the documentation.
为此,只需显式设置标签:
To do that, just set the label explicitly:
有一个简单的解决方案:“类型扩展”
创建一个如下所示的类:
将此扩展声明为服务
并在表单中使用此选项:
您将拥有一个标签“mylabelprefix.link”。
享受!
There is a simple solution: "Type extension"
Create a class like this:
Declare this extension as a service
And use this option in your form:
You'll have a label "mylabelprefix.link".
Enjoy!
我发现给定的答案不起作用,但我不被允许发表评论。它不起作用的原因是
$view->getVar()
和$view->setVar()
方法实际上并不存在。为了完成这项工作,我必须使我的buildView()
方法如下所示:然后将其设置为默认为 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 mybuildView()
method look like this:then set it to default to null like so:
Hope this helps!