Create a new resource bundle in Grails?

百般思念 提交于 2019-12-10 15:41:43

问题


I'd like to create another resource bundle to organize my Grails app. Grails provides a 'messages' resource bundle and I need to create a 'myApp' resource bundle.

How can I create a new resource bundle and read its properties with the 'g:message' GSP tag?


回答1:


You have to create a bean in grails-app/conf/spring/resources.groovy which will override the default MessageSource.

// Place your Spring DSL code here
beans = {
      messageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource) {
        basename = "classpath:grails-app/i18n/myApp"
    }
}

Note: If you need to customize Grails, the only advise I can give you is to get familiar with the Spring framework (and specifically Spring-MVC) with the following links:

  • Customize Message source within Spring
  • Configure beans within Grails



回答2:


Grails (as of version 1.0.3) will add all property files found in the grails-app/i18n directory to the resource bundle automatically. No need to add them manually :)




回答3:


As of Grails 1.3.4, any property files added to the grails-app/i18n directory will be added to the messageSource.

In addition, if you attempt to manually add the bean (and the basenames under it), when you go into the production environment it will not load them. It will work in development mode (tested running off of IDEA 9.0.3 and the tomcat plugin v1.3.4), but not in production mode.

This happened for me after following the answer provided by @rochb (I checked the answer and implemented before trying the simple way), and I had to remove the messageSource bean configuration in order to correct it.



来源:https://stackoverflow.com/questions/1522568/create-a-new-resource-bundle-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!