Custom MessageSource
Example code is available on github.com/vaadin-on-grails/custom-messagesource.
We might want to define a custom MessageSource that will return a localized message. If a message is missing, we might want to log information about missing localization (in logs or database) or we might want to provide other way to fetch the localized labels.
In this toturial we will show way to load localization from database.
Step 1
Enable OSIV in VaadinConfig.groovy. Add or comment out the following line.
openSessionInViewFilter = 'org.springframework.orm.hibernate4.support.OpenSessionInViewFilter'Step 2
Create new domain object that will hold localization data. Run grails create-domain app.Message command.
keyholds the localization key that we will you in our application to refer a localized stringvalueis localized string, for example a label in Englishlocaledefines in what language isvaluestring
package app
class Message {
String key
String value
Locale locale
}Step 3
Create localized key-value pair for given language in BootStrap.groovy.
Step 4
Implement a new message source that will use GORM domain object, Message in our case, to load the localized messages. When a value is not found, we will return the key in brackets.
Step 4
Open grails-app/conf/spring/resources.groovy and add a new bean messageSource and messageBundleMessageSource that we use to get the localization in case the localization is not found in database.
Step 5
Now we can use standard Grails.i18 method to get localized strings.
Step 6
Run the application grails run-app and open it in the browser http://localhost:8080/custom-messagesource. Then we will see the messages in the console.

Last updated
Was this helpful?