如何使用messages.en.yml 以symfony2 形式翻译标签? [英] How to translate labels in symfony2 forms with messages.en.yml?

查看:32
本文介绍了如何使用messages.en.yml 以symfony2 形式翻译标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻译表单域.我的 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 形式翻译标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆