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.
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.
packageapp.i18nimport app.Messageimport org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSourceimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.context.support.AbstractMessageSourceimport java.text.MessageFormatclass JdbcMessageSource extends AbstractMessageSource { @Autowired PluginAwareResourceBundleMessageSource messageBundleMessageSource @Override protected MessageFormat resolveCode(String code, Locale l) { Message message = Message.findByKeyAndLocale(code, l) MessageFormat format if (message) { format = new MessageFormat(message.value, message.locale) } else { format = messageBundleMessageSource.resolveCode(code, l) if (!format) { format = new MessageFormat("[$code]", l) } } return format }}
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.