0

背景

对于存储在数据库中的字段标签,我们有一些遗留的国际化,因此我尝试创建一个“合并”消息源。如果代码存在于数据库中,则返回,如果不存在,则使用 PluginAwareResourceBundleMessageSource 在 i18n 中查找。

问题

由于某种原因,cachedMergedPluginProperties 正在为区域设置缓存错误的文件。例如,如果我搜索 en_US,我会收到 pt_BR 消息(地图的键是 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 不允许我有两个类型的 bean MessageSource

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

4

1 回答 1

1

我在里面找到了声明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 不允许您拥有两个类型的 bean,MessageSource因此我不得不复制此代码并适应我的“合并”消息源。

于 2012-11-21T16:51:20.640 回答