问题描述
我正在尝试翻译表单域.我的 Bundle 的 Ressource 文件夹中有 messages.en.yml
.
I am trying to get translation of form fields to work. I have messages.en.yml
in my Bundle's Ressource folder.
test: it works
form:
description: Add a description.
我想在 EntityType
中构建一个表单,它相应地转换表单字段的标签.
I want to build a form in an EntityType
which translates the labels of the form fields accordingly.
$builder->add(
'description',
null,
array('label' => 'form.description', 'required' => false)
);
然而只有文字字符串 'form.description'
被显示,而不是 Add a description 的预期翻译.
Yet only the literal string 'form.description'
gets shown, not the expected translation of Add a description.
翻译服务和 messages.en.yml
已正确加载,因为我可以调用
The translation service and the messages.en.yml
are loaded correctly as I can call
var_dump($this->get('translator')->trans('test'));
在控制器中并获得翻译结果.
in a controller and get the translated result.
当我想将字符串 'form.description'
视为应该翻译的内容时,我错过了什么?
What am I missing when I want to treat the string 'form.description'
as something that should be translated?
我想无论如何我都必须对它们调用 trans 函数,但我怎么能一次性自动完成它呢?
I suppose I have to call the trans function on them anyhow, yet how can I make it in one go automatically?
推荐答案
我必须明确设置翻译域
例如
->add('description','hidden',
array(
"label"=>"form.description",
"required"=>true,
'translation_domain' => 'fooo'
)
);
在
fooo.de.xlf
这篇关于如何使用messages.en.yml 以symfony2 形式翻译标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!