如何在 EntityType 中翻译占位符?
How to have the placeholder translated in an EntityType?
我正在构建一个包含 EntityType 元素的表单,我遇到的问题是无法翻译占位符。
这是我的代码:
$builder
->add('Products', EntityType::class, [
'mapped' => false,
'expanded' => true,
'multiple' => false,
'required' => false,
'class' => Product::class,
'choices' => $options['step']->getProducts(),
'placeholder' => 'form.mat.alreadyOwned',
'label' => 'form.mat.alreadyOwned',
'translation_domain' => 'messages'
])
当我使用 form.mat.alreadyOwned
翻译作为标签时,它工作正常,但我想在占位符中使用它。我错过了什么?
期待您提供任何提示或技巧!
[更新]
正如@gp_sflover 所指出的,我不是要替换一般占位符,而是替换空值的占位符。仅当您将 required
设置为 false
时才会出现此选项。
经过研究和思考,实际使用placeholder
的地方非常有限(应该如此)。然而,具体的占位符翻译并不是特例(遗憾)。
对于 ChoiceType
中的每个选择,都会添加一个 ChoiceView
。也用于占位符 a ChoiceView
is added, that inherits the options of the form (which is a somewhat sensible choice for the ChoiceType
), including the translation_domain
parameter, which indicates that the choices shall be translated (all of them). There's also a reference in some twig template that specifically manages the translation in the twig bridge for non-expanded choice types。但是,这些并没有就如何专门处理 ChoiceType
.
中占位符的翻译提供具体的最佳实践答案。
对于 EntityType,这不会改变。
所以有一些方法,其中一些可能完全没有实用性...
在构建表单时翻译那里的占位符
尽管这在概念上不是最漂亮的选择,恕我直言,它仍然是最实用的选择。本质上,在 Symfony 4 中 forms can receive dependencies in their constructor via auto-wiring, so injecting a TranslatorInterface
将开放使用请求区域设置翻译占位符的能力(这是通过事件侦听器自动为翻译器设置的):
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
然后在您的 buildForm 中,您可以使用它来翻译占位符
$builder
->add('Products', EntityType::class, [
'mapped' => false,
'expanded' => true,
'multiple' => false,
'required' => false,
'class' => Product::class,
'choices' => $options['step']->getProducts(),
'placeholder' => $this->translator->trans('form.mat.alreadyOwned'), // <-- change
'label' => 'form.mat.alreadyOwned',
'translation_domain' => 'messages'
])
不要保留其他选项,在我看来这些选项几乎都是矫枉过正...
为 所有 个条目设置一个 choice_translation_domain
,包括占位符
显然,如果有一个实体的选择标签与翻译器中的键相匹配,那么这可能而且很可能会导致问题……并且它不打算用于翻译。但它会用完全相同的 translation_domain
...
翻译占位符
调整表单呈现并检查占位符
这是有问题的,因为您必须将表单 theme/form 呈现分配给所有相关表单。占位符确实有一个唯一的名称('placeholder'
,谁会想到)所以它可以很好地用于实现该目标。但是,设置 不同的翻译域可能非常具有挑战性。 (如果你尝试这样做,会有点麻烦)
编写您自己的实体类型(可选择添加自己的表单呈现)
理论上,您可以创建自己的 EntityType
并在那里正确处理占位符。就像...向选择视图和子表单添加翻译域等等。对于 inspiration/reference,请参考 ChoiceType
, EntityType
and DoctrineType
(父类型)。
我正在构建一个包含 EntityType 元素的表单,我遇到的问题是无法翻译占位符。
这是我的代码:
$builder
->add('Products', EntityType::class, [
'mapped' => false,
'expanded' => true,
'multiple' => false,
'required' => false,
'class' => Product::class,
'choices' => $options['step']->getProducts(),
'placeholder' => 'form.mat.alreadyOwned',
'label' => 'form.mat.alreadyOwned',
'translation_domain' => 'messages'
])
当我使用 form.mat.alreadyOwned
翻译作为标签时,它工作正常,但我想在占位符中使用它。我错过了什么?
期待您提供任何提示或技巧!
[更新]
正如@gp_sflover 所指出的,我不是要替换一般占位符,而是替换空值的占位符。仅当您将 required
设置为 false
时才会出现此选项。
经过研究和思考,实际使用placeholder
的地方非常有限(应该如此)。然而,具体的占位符翻译并不是特例(遗憾)。
对于 ChoiceType
中的每个选择,都会添加一个 ChoiceView
。也用于占位符 a ChoiceView
is added, that inherits the options of the form (which is a somewhat sensible choice for the ChoiceType
), including the translation_domain
parameter, which indicates that the choices shall be translated (all of them). There's also a reference in some twig template that specifically manages the translation in the twig bridge for non-expanded choice types。但是,这些并没有就如何专门处理 ChoiceType
.
对于 EntityType,这不会改变。
所以有一些方法,其中一些可能完全没有实用性...
在构建表单时翻译那里的占位符
尽管这在概念上不是最漂亮的选择,恕我直言,它仍然是最实用的选择。本质上,在 Symfony 4 中 forms can receive dependencies in their constructor via auto-wiring, so injecting a TranslatorInterface
将开放使用请求区域设置翻译占位符的能力(这是通过事件侦听器自动为翻译器设置的):
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
然后在您的 buildForm 中,您可以使用它来翻译占位符
$builder
->add('Products', EntityType::class, [
'mapped' => false,
'expanded' => true,
'multiple' => false,
'required' => false,
'class' => Product::class,
'choices' => $options['step']->getProducts(),
'placeholder' => $this->translator->trans('form.mat.alreadyOwned'), // <-- change
'label' => 'form.mat.alreadyOwned',
'translation_domain' => 'messages'
])
不要保留其他选项,在我看来这些选项几乎都是矫枉过正...
为 所有 个条目设置一个 choice_translation_domain
,包括占位符
显然,如果有一个实体的选择标签与翻译器中的键相匹配,那么这可能而且很可能会导致问题……并且它不打算用于翻译。但它会用完全相同的 translation_domain
...
调整表单呈现并检查占位符
这是有问题的,因为您必须将表单 theme/form 呈现分配给所有相关表单。占位符确实有一个唯一的名称('placeholder'
,谁会想到)所以它可以很好地用于实现该目标。但是,设置 不同的翻译域可能非常具有挑战性。 (如果你尝试这样做,会有点麻烦)
编写您自己的实体类型(可选择添加自己的表单呈现)
理论上,您可以创建自己的 EntityType
并在那里正确处理占位符。就像...向选择视图和子表单添加翻译域等等。对于 inspiration/reference,请参考 ChoiceType
, EntityType
and DoctrineType
(父类型)。