Reputation: 3660
I am trying to add some additional values into the i18n messageSource in Grails.
The default message.properties work fine but I need some additional values loaded from a remote resource (I know it is not good, but you know, managements... ).
I am trying to load these variables through the Boostrap.groovy example:
def messageSource = new StaticMessageSource()
messageSource.addMessage("key1", new Locale("en"), "English Value")
messageSource.addMessage("key2", new Locale("de"), "Other Language Value")
When I try to access them in any GSP via the
<g:message code="key1" />
only the key is returned, as if the values where not set at all at the StaticMessageSource. Apparently I am not having much luck with the documentation.
There is nothing on the Grails website and almost nothing on the SpringFramework. Will appreciate any suggestions.
Anyother way to add the a set of messages to the messageSource Session!?
Upvotes: 2
Views: 1613
Reputation: 639
I have solved a similar issue by following:
messageSource(MyMessageSource) { bean -> bean.autowire = "byName" //-THIS IS IMPORTANT basenames = "WEB-INF/grails-app/i18n/messages" }
Upvotes: 2
Reputation:
The messageSource
bean is declared in Grails with the PluginAwareResourceBundleMessageSource
class. When I need more funcionality in this, I create a grails plugin and configure the context to load my class intead of the Grails one.
Something like:
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
}
}
The plugin descriptor -> 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
}
}
Upvotes: 4