Grails i18n 来自数据库但默认返回到文件 [英] Grails i18n From Database but Default Back To File

查看:16
本文介绍了Grails i18n 来自数据库但默认返回到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照这篇博客文章我启用我的应用程序从数据库加载 i18n 消息.它工作得很好.但是,我不想管理数据库中的所有消息.所以我想说如果我在数据库中没有找到代码,然后使用默认机制加载它.

Following this blog article I enabled my application to load i18n messages from the database. It works great. However, I don't want to manage all messages in the database. So I'd like to be able to say if I don't find the code in the database, then load it using the default mechanism.

这是我所拥有的:

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 的实现来查看源.

I tried calling super.resolveCode(code, locale) but that resulted in compile errors. And I'm having a hard time tracking down the implementation of AbstractMessageSource that Grails is using by default to look at the source.

更新:感谢 doelleri,我现在意识到我需要做的是扩展 ResourceBundleMessageSource 之类的事情.不幸的是,这种方法有几个问题.我的 resources.groovy 文件中有以下内容:

UPDATE: Thanks to doelleri I now realize what I need to do is something like extending the ResourceBundleMessageSource. Unfortunately, there are several issues with this approach. I have the following in my resources.groovy file:

messageSource(DatabaseMessageSource)

首先,如果我只是简单地扩展 ResourceBundleMessageSource 并覆盖 resolveCode 方法,则永远不会调用该方法.所以在我的 else 块中,调用 super.resolveCode 是没有意义的.

First of all, if I simply extend ResourceBundleMessageSource and override the resolveCode method, that method never gets called. So in my else block, calling super.resolveCode is moot.

然后我尝试使用 ResourceBundleMessageSource 中的所有代码实现我的 DatabaseMessageSource 类,但我显然在 resources.groovy 中遗漏了一些东西,因为默认包没有连接起来.

I then attempted to just implement my DatabaseMessageSource class with all the code from ResourceBundleMessageSource but I'm apparently missing something in resources.groovy because the default bundles aren't getting wired up.

所以在这一点上,我仍然迷失在我需要做的事情上.我想先检查一下数据库.如果代码不存在,则恢复到与 ResourceBundleMessageSource 相同的默认行为.

So at this point, I'm still lost on what I need to do. I want to first check the database. If the code doesn't exist, revert to the same default behavior as ResourceBundleMessageSource.

推荐答案

我建议在新 bean 中保留一个 bundle-message-source 并将其注入到您的 DatabaseMessageSource 中.

I would propose to keep one bundle-message-source in a new bean and inject it into your DatabaseMessageSource.

resources.groovy:

// Place your Spring DSL code here
beans = {
    messageSource(DatabaseMessageSource) {
        messageBundleMessageSource = ref("messageBundleMessageSource")
    }    
    messageBundleMessageSource(org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource) {
        basenames = "WEB-INF/grails-app/i18n/messages"
    }
}

DatabaseMessageSource.groovy:

class DatabaseMessageSource extends AbstractMessageSource {

    def messageBundleMessageSource

    protected MessageFormat resolveCode(String code, Locale locale) {
         Message msg = Message.findByCodeAndLocale(code, locale)
         def format
         if(msg) {
             format = new MessageFormat(msg.text, msg.locale)
         }
         else {
             format = messageBundleMessageSource.resolveCode(code, locale)
         }
         return format;
    }
}

这样,在后备解决方案中,只需从一个资源包消息源请求消息,即可从相应的 messages_*.properties 文件中读取消息.请注意,您应该使用 PluginAwareResourceBundleMessageSource,否则您可能会错过插件中的一些重要消息.

This way, in fallback solution, the message will be read from the appropriate messages_*.properties file, by just requesting it from one resource bundle message source. Note that you should use the PluginAwareResourceBundleMessageSource, otherwise you could miss some important messages from your plugins.

这篇关于Grails i18n 来自数据库但默认返回到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆