Grails: internationalization in the service

Today I spent some time by creation of localized messages in a service. Here is a small tutorial.

1. Create your service

grails create-service Local

2. Add a messageSource variable to your new service

class LocalService {
  def messageSource

3. Use it in a service method

// Initialize parameters
Object[] testArgs = {}
def msg = messageSource.resolveCode("", new java.util.Locale("EN")).format(testArgs)

4. Finally edit the grails-app/i18n/ works!!!


  1. Hi Roman,

    is it necessary to add the code to resources.groovy? Shouldn’t the messageSource automatically be injected into the service by name?



  2. Personally I would rather get the application taglib from the app context and use it like in the rest of the application.
    Like having this on a tools-class

    def getApplicationTagLib(){
    def ctx = ApplicationHolder.getApplication().getMainContext()
    return ctx.getBean(‘org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib’)

    And then you can do in your service method

    def g = xyzTools.getApplicationTagLib()
    def msg = g.message(code:’’)

    That way you wouldn’t have to worry about passing the locale etc.

Comments are closed.