Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: How to access i18n from controller or service?

Tags:

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

like image 641
Ondrej Kvasnovsky Avatar asked Dec 30 '11 22:12

Ondrej Kvasnovsky


1 Answers

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.

like image 92
Roberto Guerra Avatar answered Dec 26 '22 19:12

Roberto Guerra