正在關注this blog article我啓用了我的應用程序從數據庫加載i18n消息。它效果很好。但是,我不想管理數據庫中的所有消息。所以我想能夠說,如果我沒有找到數據庫中的代碼,然後使用默認機制加載它。Grails 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(代碼區域),但導致編譯錯誤。而且我很難跟蹤Grails在默認情況下使用的AbstractMessageSource的實現來查看源代碼。
更新:感謝doelleri我現在意識到我需要做的就像擴展ResourceBundleMessageSource。不幸的是,這種方法存在幾個問題。我在我的resources.groovy文件中的以下內容:
所有的messageSource(DatabaseMessageSource)
首先,如果我只是延長ResourceBundleMessageSource會並覆蓋resolveCode方法,該方法不會被調用。所以在我的else塊中,調用super.resolveCode是沒有意義的。
然後我試圖用ResourceBundleMessageSource中的所有代碼實現我的DatabaseMessageSource類,但我顯然缺少resources.groovy中的某些內容,因爲缺省的bundle沒有連接。
所以在這一點上,我仍然失去了我需要做的事情。我想先檢查數據庫。如果代碼不存在,請恢復爲與ResourceBundleMessageSource相同的默認行爲。
你有沒有這個工作? – Marco