如何在Grails中创建包含所有i18n消息的地图

Adr*_*ian 8 grails

我需要这个在控制器中呈现它的一部分,如:

class MessageController {

  def index = {

    def messageMap = listAlli18nMessages() // the question

    render (contentType: "text/xml") {
       messageMap {key, message ->
          ..
       }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*ian 11

最后我找到了答案 - 覆盖默认的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)
    }
}
Run Code Online (Sandbox Code Playgroud)

在grails-app/conf/spring/resources.groovy中:

beans = {
    messageSource(ExtendedPluginAwareResourceBundleMessageSource)  {
        basenames = "WEB-INF/grails-app/i18n/messages"
    }
}
Run Code Online (Sandbox Code Playgroud)

对应的控制器代码:

class MessageController {
    def messageSource

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

        render (contentType: "text/xml") {
            messageMap {key, message ->
                ..
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)