Grails: How do I get an i18 message using the current location?

advertisements

I'm coding in Grails (2.2.4) + groovy application, and I need to get i18 message, what are the possible ways to return this kind of value?


Check the official documentation - http://grails.org/doc/2.2.x/guide/i18n.html You can find there all interesting information about how to set and read i18n messages.

In essence:

Put your i18n messages in grails-app/i18n/messages_[lang_code].properties where [lang_code] represents language code you are interested in. This file is simple ini-like file, so it contains key=values e.g.

site_title=Your site internationalized site name

Then you can use this message. If you would like to display it in your gsp view, use simple taglig:

<g:message code="site_title" />

As you can see, "code" attributes is associated with the key of your message in the properties file. If you would like to get your i18n message in the controller, then you will have to inject "messageSource" bean:

def messageSource

And after that you can receive your message in the controller action by using this method:

messageSource.getMessage('site_title', null, request.locale, 'here you can put some default value if i18n label is not found')

It will return the message associated with 'site_title' key.

One more thing - Grails app uses your browser locale. If you would like to change them, pass the lang parameter in your url request e.g. http://yourappdomain.com?lang=en