Search code examples
springinternationalizationexternalbundleresourcebundle

How to externalize a Spring MessageSources bundle outside the WAR


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!


Solution

  • 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.