Grails:如何从控制器或服务访问 i18n? [英] Grails: How to access i18n from controller or service?

查看:15
本文介绍了Grails:如何从控制器或服务访问 i18n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了这个控制器,应该用来发送电子邮件.我需要访问 i18n 才能发送本地化的电子邮件.

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...
    }
}

然后是 i18n 文件(位于 i18n 文件夹中):

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:没有这样的属性:messageSource类:org.codehaus.groovy.grails.support.MockApplicationContext

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).

推荐答案

如果您使用的是 grails 1.3.7,则需要将消息源声明为:

If you are using grails 1.3.7, you need to declare message source as :

def messageSource

这样它就会被注入到你的控制器中.我认为在 2.0 中您可以按照您发布的方式进行操作,但即便如此,我认为还是值得一试,如上所述进行声明.

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屋!

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