Search code examples
grails

Grails: How to access i18n from controller or service?


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


Solution

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