来自属性文件的Grails i18n由数据库备份? [英] Grails i18n from property files backed up by a DB?

查看:65
本文介绍了来自属性文件的Grails i18n由数据库备份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一种情况,我可以使用与数据库一起备份的i18n属性文件?

因此,对于一些标准的东西,我想使用属性文件,但有些字段必须由最终用户编辑,所以我打算在数据库中使用i18n为了那个原因。所以真正的组合会很棒。如果在属性文件中找不到i18n代码,请在数据库中查找。



任何想法如何解决这个问题?我已经看过这篇文章 Grails i18n来自数据库,但默认返回文件



但是这个问题没有真正的答案,有关于如何解决这个问题的其他建议?

  class Message {
字符串代码
语言环境区域
字符串文本
}

将以下行添加到 resources.groovy

  //将您的Spring DSL代码放在这里
beans = {
messageSource(DatabaseMessageSource){
messageBundleMessageSource = ref(messageBundleMessageSource)
}
messageBundleMessageSource(org.codehaus.groovy .grails.context.support.PluginAwareResourceBundleMessageSource){
basenames =WEB-INF / grails-app / i18n / messages
}
}

将以下类添加到 src / groovy 文件夹:

  class DatabaseMessageSource extends AbstractMessageSource {

def messageBundleMessageSource

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


$ / code>

现在,grails将尝试解析消息中的消息束。如果它不可用,它将从数据库中查找它。您可以添加一些错误处理,但是如果所有消息至少在一个地方都可用,则此版本可行。



请参阅 http://graemerocher.blogspot.com/2010/04/reading-i18n-messages-from-database .html 了解更多详细信息。






有关在 resources.groovy



在这个文件中,你可以定义可注入的groovy类,可以通过定义一个名称与在 resources.groovy 中定义。例如。在这个文件中,有 messageSource messageBundleMessageSource ,您可以将其包含在任何控制器或服务文件中。如果定义了这个变量,就会创建一个括号中类的实例。



在这种情况下,我们覆盖了一般的 messageSource 来使用我们的自定义实现 DatabaseMessageSource 。因此,I18n函数 message 现在将使用我们的自定义实现。

由于我们的自定义实现需要检查 message.properties - 文件,我们保留原始的

第二个bean中的消息源。通过在我们的自定义实现中定义这个实例,我们仍然可以使用旧的实现(并因此以通常的方式查找消息)。


i am trying to get a situation where i can use i18n property files which are backed up with a database?

So for some standard stuff i would like to use the property files, but some fields must be editable by the end-user so i was planning to use i18n in the database for that. So a real combination would be great. If the i18n code cannot be found in the property files then do a lookup in the DB.

Any idea how i can tackle this? I have seen the post Grails i18n From Database but Default Back To File

But there is no real answer to the problem, any other suggestions on how to tackle this?

解决方案

Put a new domain class into your project:

class Message {
    String code
    Locale locale
    String text
}

Add the following lines to your 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"
    }
}

And add the following class to your src/groovy folder:

class DatabaseMessageSource extends AbstractMessageSource {

    def messageBundleMessageSource

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

Now grails will try to resolve the message from the message bundle. If it is not available, it will look it up from database. You could add some error-handling, but this version works, if all messages are available at least in one place.

See http://graemerocher.blogspot.com/2010/04/reading-i18n-messages-from-database.html for some more details.


Some details on the changes done in resources.groovy:

In this file you can define injectable groovy classes, which can be included by just defining a variable having the same name as defined in the resources.groovy. E.g. in this file, there are messageSource and messageBundleMessageSource, which you can be include in any controller or service files. If this variable is defined, an instance of the class in the brackets is created.

In this case, we overwrite the general messageSource to use our custom implementation DatabaseMessageSource. So the I18n function message will now use our custom implementation.

Since our custom implementation requires to check the message.properties-files we keep the original message source in the second bean. By defining this instance in our custom implementation, we can still use the old implementation (and therefore looking up messages the usual way).

这篇关于来自属性文件的Grails i18n由数据库备份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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