在 Symfony 中为 Bootstrap 复选框渲染带有变量的翻译
Render a translation with variable for a Bootstrap checkbox in Symfony
在 Symfony 4 项目中,我使用 Bootstrap 主题来渲染我的 HTML 内容。
我有一个表单,其中包含一个用于接受以下条款的复选框:
☑ I accept terms
我想获取 link 作为变量(会根据语言而改变)
所以我有一个这样的翻译 yaml :
form:
register:
title: Registration
username: Username
email: Email
password: Password
repeat_password: Repeat your password
accept_terms: Accept %terms%
在我的 formType
文件中,我无法注入翻译,因为我找不到任何东西可以提供 %terms%
参数。
->add(
'termsAccepted',
CheckboxType::class,
[
'mapped' => false,
'constraints' => new IsTrue(),
'label' => 'form.register.accept_terms',
]
)
在 twig 文件中我无法更改 form_label 因为...在 Symfony documentation 中它说 form_label
不适用于复选框(和单选按钮)
{{ form_label(form.termsAccepted, 'that custom label is ignored...') }}
知道如何在复选框元素 (Bootstrap 4) 上发送翻译(带参数)吗?
解决方案
在我的表单中:
{% form_theme form.termsAccepted _self %}
{% block checkbox_label -%}
... here is a copy/paste of the orignial code from https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig ...
{{ widget|raw }}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{% set terms_link = '<a href="http://url.com">'~'form.register.terms_link'|trans()~'</a>' %}
{{- label is not same as(false) ? (translation_domain is same as(false) ? label|raw : label|trans({'%terms%': terms_link}, translation_domain)|raw) -}}
{{- form_errors(form) -}}
</label>
{%- endif -%} {%- endblock checkbox_label %}
在您看来,您可以在顶部插入这个
{% form_theme form.termsAccepted _self %}
如文档中所写 https://symfony.com/doc/current/form/form_customization.html#child-forms
然后你可以用你需要的覆盖 bootstrap 4 表单主题中的 checkbox_label。随便写
{% block checkbox_label %}
Copy and paste here the same block from bootstrap 4 form them and edit as your needs. See https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig
{% endblock %}
此自定义块将仅用于 form.termsAccepted。
如果您想要一个更通用的解决方案,您可以创建自己的类型(例如扩展 CheckboxType
的 CheckboxlinkType
,在 configureOptions
中添加所需的选项(linkUri 和 linkText)并通过它们的值到 buildView
中的视图。然后你可以编写你需要呈现你的类型的特定块,称为 checkboxlink_label
使用 bootstrap 4 形式主题中的一个作为基础并使用你的变量. 如果您需要这方面的帮助,请告诉我。
通过表单主题自定义模板的另一种解决方案是翻译表单类型中的标签并禁用模板中执行的翻译:
->add('termsAccepted', CheckboxType::class, [
'mapped' => false,
'constraints' => new IsTrue(),
'label' => $this->translator->trans('form.register.accept_terms', [
'%terms%' => 'foo',
],
'translation_domain' => false,
])
您必须向表单类型的构造函数添加一个 TranslatorInterface
参数,将其注册为服务并在此处注入 translator
服务。
在 Symfony 4 项目中,我使用 Bootstrap 主题来渲染我的 HTML 内容。
我有一个表单,其中包含一个用于接受以下条款的复选框:
☑ I accept terms
我想获取 link 作为变量(会根据语言而改变)
所以我有一个这样的翻译 yaml :
form:
register:
title: Registration
username: Username
email: Email
password: Password
repeat_password: Repeat your password
accept_terms: Accept %terms%
在我的 formType
文件中,我无法注入翻译,因为我找不到任何东西可以提供 %terms%
参数。
->add(
'termsAccepted',
CheckboxType::class,
[
'mapped' => false,
'constraints' => new IsTrue(),
'label' => 'form.register.accept_terms',
]
)
在 twig 文件中我无法更改 form_label 因为...在 Symfony documentation 中它说 form_label
不适用于复选框(和单选按钮)
{{ form_label(form.termsAccepted, 'that custom label is ignored...') }}
知道如何在复选框元素 (Bootstrap 4) 上发送翻译(带参数)吗?
解决方案
在我的表单中:
{% form_theme form.termsAccepted _self %}
{% block checkbox_label -%}
... here is a copy/paste of the orignial code from https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig ...
{{ widget|raw }}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{% set terms_link = '<a href="http://url.com">'~'form.register.terms_link'|trans()~'</a>' %}
{{- label is not same as(false) ? (translation_domain is same as(false) ? label|raw : label|trans({'%terms%': terms_link}, translation_domain)|raw) -}}
{{- form_errors(form) -}}
</label>
{%- endif -%} {%- endblock checkbox_label %}
在您看来,您可以在顶部插入这个
{% form_theme form.termsAccepted _self %}
如文档中所写 https://symfony.com/doc/current/form/form_customization.html#child-forms
然后你可以用你需要的覆盖 bootstrap 4 表单主题中的 checkbox_label。随便写
{% block checkbox_label %}
Copy and paste here the same block from bootstrap 4 form them and edit as your needs. See https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig
{% endblock %}
此自定义块将仅用于 form.termsAccepted。
如果您想要一个更通用的解决方案,您可以创建自己的类型(例如扩展 CheckboxType
的 CheckboxlinkType
,在 configureOptions
中添加所需的选项(linkUri 和 linkText)并通过它们的值到 buildView
中的视图。然后你可以编写你需要呈现你的类型的特定块,称为 checkboxlink_label
使用 bootstrap 4 形式主题中的一个作为基础并使用你的变量. 如果您需要这方面的帮助,请告诉我。
通过表单主题自定义模板的另一种解决方案是翻译表单类型中的标签并禁用模板中执行的翻译:
->add('termsAccepted', CheckboxType::class, [
'mapped' => false,
'constraints' => new IsTrue(),
'label' => $this->translator->trans('form.register.accept_terms', [
'%terms%' => 'foo',
],
'translation_domain' => false,
])
您必须向表单类型的构造函数添加一个 TranslatorInterface
参数,将其注册为服务并在此处注入 translator
服务。