Grails i18n From Database but Default Back To File

后端 未结 5 1297
抹茶落季
抹茶落季 2020-12-08 03:49

Following this blog article I enabled my application to load i18n messages from the database. It works great. However, I don\'t want to manage all messages in the database

5条回答
  •  暖寄归人
    2020-12-08 03:56

    You may extend ReloadableResourceBundleMessageSource (the original grails message bundle, which does not seem to be final) instead and then apply this code:

    class DatabaseMessageSource extends ReloadableResourceBundleMessageSource {
      protected MessageFormat resolveCode(String code, Locale locale) {
        Message msg = Message.findByCodeAndLocale(code, locale)
        def format = null
        if (msg) {
          format = new MessageFormat(msg.text, msg.locale)
        }else{
          format = super.resolveCode(code,locale)
        }
        return format;
      }
    }
    

    You may also reconfigure your message resources in Resources.groovy:

     beans = {
        messageSource(com.mycompany.DatabaseMessageSource) {
            basename = "WEB-INF/grails-app/i18n/messages"
        }
     }
    

提交回复
热议问题