Grails:如何从控制器或服务访问i18n? [英] Grails: How to access i18n from controller or service?
问题描述
我制作了这个控制器,它应该用来发送电子邮件。我需要访问i18n才能发送本地化的电子邮件。
class MailController {
MessageSource message $
static transactional = false
public void sendEmail(){
String name =some name ...
String subject = message(code:somemessagekey,args:[name])
//在这里做一些花哨的东西...
然后是i18n文件(位于i18n文件夹中):
文件名:messages.properties
内容: somemessagekey = Blabla {0} - blablabla
运行这个之后,它会抛出(在集成测试中) :
groovy.lang.MissingPropertyException:没有这样的属性:class的messageSource:org.codehaus.groovy.grails.support.MockApplicationContext
我不知道如何处理控制器中的本地化(我也在服务中尝试过,但是这更复杂)。
def messageSource
这样它就会被注入到你的控制器中。我认为在2.0版中,您可以按照您发布的方式进行操作,但即使如此,我认为还是值得尝试按照上述说明。
I have made this controller, which should be used to send emails. I need to access i18n in order to send localized emails.
class MailController {
MessageSource messageSource
static transactional = false
public void sendEmail() {
String name = "some name..."
String subject = message(code:"somemessagekey", args:[name])
// do some fancy stuff here...
}
}
There is the i18n file then (located in i18n folder):
file name: messages.properties
content: somemessagekey = Blabla {0} - blablabla
After I run this, it throws (in an integration test):
groovy.lang.MissingPropertyException: No such property: messageSource for class: org.codehaus.groovy.grails.support.MockApplicationContext
I am out of ideas how to handle that localization in controller (I also tried it in a service, but that is even more complicated).
If you are using grails 1.3.7, you need to declare message source as :
def messageSource
This way it will be injected into your controller. I think in 2.0 you can do it the way you posted, but it even so, I think it is worth a try to declare it as stated above.
这篇关于Grails:如何从控制器或服务访问i18n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!