【发布时间】:2011-11-11 21:39:09
【问题描述】:
在this blog article 之后,我启用了我的应用程序以从数据库加载 i18n 消息。它工作得很好。但是,我不想管理数据库中的所有消息。所以我想说如果我在数据库中没有找到代码,然后使用默认机制加载它。
这是我所拥有的:
class DatabaseMessageSource extends AbstractMessageSource {
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{
// What do I do here to grab it from the file
}
return format;
}
}
我尝试调用 super.resolveCode(code, locale) 但导致编译错误。而且我很难追踪 Grails 默认使用的 AbstractMessageSource 的实现来查看源代码。
更新:感谢 doelleri,我现在意识到我需要做的就是扩展 ResourceBundleMessageSource。不幸的是,这种方法存在几个问题。我的 resources.groovy 文件中有以下内容:
messageSource(DatabaseMessageSource)
首先,如果我只是扩展 ResourceBundleMessageSource 并覆盖 resolveCode 方法,则该方法永远不会被调用。所以在我的 else 块中,调用 super.resolveCode 没有实际意义。
然后我尝试使用来自 ResourceBundleMessageSource 的所有代码来实现我的 DatabaseMessageSource 类,但我显然在 resources.groovy 中遗漏了一些东西,因为默认包没有连接起来。
所以在这一点上,我仍然不知道我需要做什么。我想先检查一下数据库。如果代码不存在,则恢复为与 ResourceBundleMessageSource 相同的默认行为。
【问题讨论】:
-
你做过这个工作吗?
标签: grails internationalization