Quantcast

i18n Database

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

i18n Database

Gregg Bolinger-8
I'm completely stuck on this right now.  Could someone please take a look?  I don't care if you answer it here or there.  I just really need to get this resolved.


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.

Here is what I have:

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;
 
}
}

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.

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)

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.

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.

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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: i18n Database

Graeme Rocher
Administrator
Best thing todo might be to implement a BeanFactoryPostProcessor that
gets the Grails version of the "messageSource" bean then passes it to
your DatabaseMessageSource which can then delegate.

inside resources.groovy you can call:


addBeanFactoryPostProcessor(new DatabaseMessageSourcePostProcessor())

and inside the void
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
method you can get the existing bean then override it by calling
"registerSingleton(name, bean)"

Cheers

On Tue, Nov 15, 2011 at 5:18 PM, Gregg Bolinger
<[hidden email]> wrote:

> I'm completely stuck on this right now.  Could someone please take a look?
>  I don't care if you answer it here or there.  I just really need to get
> this resolved.
> http://stackoverflow.com/questions/8100312/grails-i18n-from-database-but-default-back-to-file
>
> 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.
>
> Here is what I have:
>
> 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;
>
>   }
>
> }
>
> 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.
>
> 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)
>
> 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.
>
> 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.
>
> 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.



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: i18n Database

Gregg Bolinger-8
Thanks, Graeme. I'll see what I can do.

On Tue, Nov 15, 2011 at 10:31 AM, Graeme Rocher <[hidden email]> wrote:
Best thing todo might be to implement a BeanFactoryPostProcessor that
gets the Grails version of the "messageSource" bean then passes it to
your DatabaseMessageSource which can then delegate.

inside resources.groovy you can call:


addBeanFactoryPostProcessor(new DatabaseMessageSourcePostProcessor())

and inside the void
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
method you can get the existing bean then override it by calling
"registerSingleton(name, bean)"

Cheers

On Tue, Nov 15, 2011 at 5:18 PM, Gregg Bolinger
<[hidden email]> wrote:
> I'm completely stuck on this right now.  Could someone please take a look?
>  I don't care if you answer it here or there.  I just really need to get
> this resolved.
> http://stackoverflow.com/questions/8100312/grails-i18n-from-database-but-default-back-to-file
>
> 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.
>
> Here is what I have:
>
> 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;
>
>   }
>
> }
>
> 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.
>
> 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)
>
> 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.
>
> 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.
>
> 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.



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: i18n Database

marcopas
Did you get this working? I am trying to achieve the opposite. Let the DB be my backup of the properties.
Loading...