views:

328

answers:

2

Hi,

I have to externalize the Spring MessageSources bundle for i18n support (properties files) outside the classpath in order to modify properties more easily. How can I do that ?

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="test-messages"/>

Thanks!

A: 

We have built a message source implementation that looks up messages in the DB. What you have to do is create a MessageSource implementation that inherits from spring's AbstractMessageSource (in order to gain all features, see javadoc).

You have to implement at minimal the abstract method 'resolveCode(String, Locale)' (but implementing 'resolveCodeWithoutArguments(String, Locale)' will increase your performances), which delegates to a DAO pointing to that simple table, with a definition such as this:

table translation (
  translation_id number pk
  code varchar(20)
  locale varchar(5)
  translation varchar(100)
)

code and locale form a unique index.

And you're done. Of course, you will add some cache capabilities, and provide "locale degradation" behaviour (i.e. if "en_US" is not found, try "en"), either at dao- or MessageSource-level.

This works perfectly.

Gaetan
Thanks for the example, I did something like your solution to solve my problem.
Wickramben
A: 

Check this thread for information regarding this issue, but I think is not a good practise to have files outside the tomcat context as you never know where it is going to be deployed your application.

But in case you need, you'll find some pretty nice solutions there.

pedromarce