哪里找到了Grails中messageSource的声明?

时间:2012-11-21 16:37:07

标签: grails internationalization grails-2.0

背景

我们对存储在数据库中的字段标签有一些遗留国际化,所以我尝试制作一个“合并”的messageSource。如果代码存在于数据库中,则返回,如果不存在,则使用PluginAwareResourceBundleMessageSource查看i18n。

问题

由于某种原因,cachedMergedPluginProperties正在为区域设置缓存错误的文件。例如,如果我搜索en_US,我会收到pt_BR消息(Map的键是en_US,但属性是pt_BR)。

我将messageSource声明如下:

messageSource(DatabaseMessageSource) {
  messageBundleMessageSource = { org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource m ->
    basenames = "WEB-INF/grails-app/i18n/messages"
    } 
}  

内部bean是因为Grails不会让我有两个类型MessageSource的bean。

我是否宣布PluginAwareResourceBundleMessageSource与Grails的默认值不同?在哪个Grails文件中我可以看到这个bean声明?

1 个答案:

答案 0 :(得分:0)

我在I18nGrailsPlugin内找到了声明,它比我的更详细:

String baseDir = "grails-app/i18n"
String version = GrailsUtil.getGrailsVersion()
String watchedResources = "file:./${baseDir}/**/*.properties".toString()
...
 Set baseNames = []

        def messageResources
        if (application.warDeployed) {
            messageResources = parentCtx?.getResources("**/WEB-INF/${baseDir}/**/*.properties")?.toList()
        }
        else {
            messageResources = plugin.watchedResources
        }

        if (messageResources) {
            for (resource in messageResources) {
                // Extract the file path of the file's parent directory
                // that comes after "grails-app/i18n".
                String path
                if (resource instanceof ContextResource) {
                    path = StringUtils.substringAfter(resource.pathWithinContext, baseDir)
                }
                else {
                    path = StringUtils.substringAfter(resource.path, baseDir)
                }

                // look for an underscore in the file name (not the full path)
                String fileName = resource.filename
                int firstUnderscore = fileName.indexOf('_')

                if (firstUnderscore > 0) {
                    // grab everyting up to but not including
                    // the first underscore in the file name
                    int numberOfCharsToRemove = fileName.length() - firstUnderscore
                    int lastCharacterToRetain = -1 * (numberOfCharsToRemove + 1)
                    path = path[0..lastCharacterToRetain]
                }
                else {
                    // Lop off the extension - the "basenames" property in the
                    // message source cannot have entries with an extension.
                    path -= ".properties"
                }
                baseNames << "WEB-INF/" + baseDir + path
            }
        }

        LOG.debug "Creating messageSource with basenames: $baseNames"

        messageSource(PluginAwareResourceBundleMessageSource) {
            basenames = baseNames.toArray()
            fallbackToSystemLocale = false
            pluginManager = manager
            if (Environment.current.isReloadEnabled() || GrailsConfigUtils.isConfigTrue(application, GroovyPagesTemplateEngine.CONFIG_PROPERTY_GSP_ENABLE_RELOAD)) {
                def cacheSecondsSetting = application?.flatConfig?.get('grails.i18n.cache.seconds')
                if (cacheSecondsSetting != null) {
                    cacheSeconds = cacheSecondsSetting as Integer
                } else {
                    cacheSeconds = 5
                }
            }
        }

由于Grails不允许你有两个类型为MessageSource的bean,我不得不复制这段代码并适应我的“merged”messageSource。