我正在尝试向 Grails 中的 i18n messageSource 添加一些额外的值。
默认的 message.properties 工作正常,但我需要从远程资源加载一些额外的值(我知道这不好,但你知道,管理...)。
我正在尝试通过 Boostrap.groovy 加载这些变量 示例:
def messageSource = new StaticMessageSource()
messageSource.addMessage("key1", new Locale("en"), "English Value")
messageSource.addMessage("key2", new Locale("de"), "Other Language Value")
当我尝试通过
在任何 GSP 中访问它们时<g:message code="key1" />
仅返回键,就好像在 StaticMessageSource 中根本没有设置值一样。显然我对文档不太满意。
Grails 网站上什么也没有,SpringFramework 上几乎什么也没有。 将不胜感激任何建议。
还有其他方法可以将一组消息添加到 messageSource Session 中吗?
最佳答案
messageSource
bean 在 Grails 中使用 PluginAwareResourceBundleMessageSource
类声明。当我需要更多功能时,我创建了一个 grails plugin并配置上下文以加载我的类而不是 Grails 类。
类似于:
src/groovy/MyMessageSource.groovy
class MyMessageSource extends PluginAwareResourceBundleMessageSource {
String getMessage(String key, Object[] args, Locale locale) {
String message
//handle your custom keys
if(key.contains('myCoolMessagePrefix') {
} else {
//or delegate to the default of Grails
message = super.getMessage(key, args, locale)
}
return message
}
}
插件描述符 -> MyMessagesGrailsPlugin.groovy
def doWithSpring = {
//here we change the bean definition to use the customized messagesource
def beanconf = springConfig.getBeanConfig('messageSource')
def beandef = beanconf ? beanconf.beanDefinition : springConfig.getBeanDefinition('messageSource')
if (beandef) {
beandef.beanClassName = MyMessageSource.class.name
}
}
关于spring - 在 Grails 中动态设置消息 i18n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19431108/