gpt4 book ai didi

grails - Grails中的messageSource声明在哪里?

转载 作者:行者123 更新时间:2023-12-02 15:34:41 29 4
gpt4 key购买 nike

背景

对于存储在数据库中的字段标签,我们有一些传统的国际化方法,因此我尝试创建一个“合并的” 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声明?

最佳答案

我在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,因此我必须复制此代码并适应我的“合并” messageSource。

关于grails - Grails中的messageSource声明在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497835/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com