我如何使用Grails中的所有i18n消息创建一张地图 [英] How can I create a map with all i18n-messages in Grails
本文介绍了我如何使用Grails中的所有i18n消息创建一张地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类MessageController {
def index = {
def messageMap = listAlli18nMessages()//问题
render(contentType:text / xml){
messageMap {key ,消息 - >
..
}
}
}
}
最后,我找到了一个答案 - 覆盖默认的Grails消息源:
class ExtendedPluginAwareResourceBundleMessageSource extends PluginAwareResourceBundleMessageSource {
Map< String,String> listMessageCodes(Locale locale){
Properties properties = getMergedProperties(locale).properties
属性pluginProperties = getMergedPluginProperties(locale).properties
返回properties.plus(pluginProperties)
}
在grails-app / conf / spring / resources.groovy中:
beans = {
messageSource(ExtendedPluginAwareResourceBundleMessageSource){
basenames =WEB-INF / grails-app / i18n / messages
$ / code $ / pre
$ b $ p 相应的控制器代码:
class MessageController {
def messageSource
def index = {
def messageMap = messageSource.listMessageCodes(request .locale)
render(contentType:text / xml){
messageMap {key,message - >
..
}
}
}
}
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 ->
..
}
}
}
}
解决方案 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 ->
..
}
}
}
}
这篇关于我如何使用Grails中的所有i18n消息创建一张地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文