php 如何使用messages.en.yml 以symfony2 形式翻译标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21236605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to translate labels in symfony2 forms with messages.en.yml?
提问by k0pernikus
I am trying to get translation of form fields to work. I have messages.en.ymlin my Bundle's Ressource folder.
我正在尝试使表单字段的翻译工作。我messages.en.yml在我的 Bundle 的 Ressource 文件夹中。
test: it works
form:
description: Add a description.
I want to build a form in an EntityTypewhich translates the labels of the form fields accordingly.
我想在 an 中构建一个表单EntityType,相应地转换表单字段的标签。
$builder->add(
'description',
null,
array('label' => 'form.description', 'required' => false)
);
Yet only the literal string 'form.description'gets shown, not the expected translation of Add a description.
然而只显示了文字字符串'form.description',而不是预期的翻译Add a description.
The translation service and the messages.en.ymlare loaded correctly as I can call
翻译服务和messages.en.yml我可以调用的正确加载
var_dump($this->get('translator')->trans('test'));
var_dump($this->get('translator')->trans('test'));
in a controller and get the translated result.
在控制器中并获得翻译结果。
What am I missing when I want to treat the string 'form.description'as something that should be translated?
当我想将字符串'form.description'视为应该翻译的东西时,我错过了什么?
I suppose I have to call the trans function on them anyhow, yet how can I make it in one go automatically?
我想无论如何我都必须对它们调用 trans 函数,但是我怎么能一次性自动完成呢?
回答by john Smith
i had to explicitly set the translation domain
我必须明确设置翻译域
e.g
例如
->add('description','hidden',
array(
"label"=>"form.description",
"required"=>true,
'translation_domain' => 'fooo'
)
);
in case of
的情况下
fooo.de.xlf
回答by Micha? Zwierzyński
In symfony 3.1 You don't need to pass translator to form type constructor. It will be translated automatically by form field name like this:
在 symfony 3.1 中,您不需要通过翻译器来形成类型构造函数。它将通过表单字段名称自动翻译,如下所示:
Controller action:
控制器动作:
$form = $this->createForm(LoginType::class);
FormType class:
表单类型类:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username',
TextType::class, ['label_format' => '%name%',]
);
}
And your translations file (yml):
以及您的翻译文件 (yml):
username: Your translation for username field
Hope it will help someone:)
希望它会帮助某人:)
回答by Moris Finkel
Add translation in form for is best idea - because found if in twig much more easy
在表单中添加翻译是最好的主意 - 因为如果在树枝中找到更容易
{% trans_default_domain 'YourNameBundle' %}
{{'shot.tag.for.transle'|trans }}
also you must remember about translation file in your bundle, if you extend bundle from another (example - you extends FosUserBundle) you can take translate from this bundle or use func transwith params
你还必须记住你的包中的翻译文件,如果你从另一个扩展包(例如 - 你扩展了 FosUserBundle),你可以从这个包中进行翻译或使用 func trans和 params
{{'shot.tag.for.transle'|trans({}, 'TranslationNameBundle') }}
Have a nice localisation.
有一个很好的本地化。
回答by mapmalith
Let say you want to translate into French. Then you have filename.en.xlfand filename.fr.xlf.
假设您想翻译成法语。然后你有filename.en.xlf和filename.fr.xlf。
now for the following form element you want to make descriptiontranslatable.
现在对于以下表单元素,您要使描述可翻译。
$builder->add(
'description',
null,
array('label' => 'form.description', 'required' => false)
);
so in the filename.en.xlf
所以在文件名.en.xlf
<trans-unit id="17">
<source>description</source>
<target>description</target>
</trans-unit>
and for French, so in the filename.fr.xlfas below
对于法语,因此在filename.fr.xlf 中,如下所示
<trans-unit id="17">
<source>description</source>
<target>fr descrip</target>
</trans-unit>
hope this will help for someone
希望这会对某人有所帮助

