Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a map with all i18n-messages in Grails

Tags:

grails

I need this to render a part of it in a controller like:

class MessageController {

  def index = {

    def messageMap = listAlli18nMessages() // the question

    render (contentType: "text/xml") {
       messageMap {key, message ->
          ..
       }
    }
  }
}
like image 252
Adrian Avatar asked Oct 11 '12 16:10

Adrian


1 Answers

Finally I found an answer - override the default Grails messageSource:

class ExtendedPluginAwareResourceBundleMessageSource extends PluginAwareResourceBundleMessageSource {
    Map<String, String> listMessageCodes(Locale locale) {
        Properties properties = getMergedProperties(locale).properties
        Properties pluginProperties = getMergedPluginProperties(locale).properties
        return properties.plus(pluginProperties)
    }
}

In grails-app/conf/spring/resources.groovy:

beans = {
    messageSource(ExtendedPluginAwareResourceBundleMessageSource)  {
        basenames = "WEB-INF/grails-app/i18n/messages"
    }
}

Corresponding controller code:

class MessageController {
    def messageSource

    def index = {
        def messageMap = messageSource.listMessageCodes(request.locale)

        render (contentType: "text/xml") {
            messageMap {key, message ->
                ..
            }
        }
    }
}
like image 200
Adrian Avatar answered Oct 20 '22 09:10

Adrian