如何在 Grails 中创建包含所有 i18n 消息的地图 [英] How can I create a map with all i18n-messages in Grails

查看:17
本文介绍了如何在 Grails 中创建包含所有 i18n 消息的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要它在控制器中渲染它的一部分,例如:

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

推荐答案

终于找到了答案 - 覆盖默认的 Grails 消息来源:

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

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

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

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

对应的控制器代码:

class MessageController {
    def messageSource

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

        render (contentType: "text/xml") {
            messageMap {key, message ->
                ..
            }
        }
    }
}

这篇关于如何在 Grails 中创建包含所有 i18n 消息的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆