Friday, February 24, 2012

Agile content deployment with Spring

Perhaps because I am not a native English speaker I have been always so concern about localized content (Internationalization). I have seen many applications that are not thought for other than English audiences from the beginning and later down the road have a painful path to get translated to other languages. With Spring all you need to do is to keep your messages in properties files and then configure a ReloadableResourceBundleMessageSource bean that will take care of loading the files following certain schedule. Below is an example that will load the properties every minute.
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!--  <property name="basename" value="classpath:messages" />  -->
        <property name="basenames">
            <value>/WEB-INF/i18n/messages</value>
        </property> 
        <property name="cacheSeconds">
            <value>60</value>
        </property>
        <property name="fallbackToSystemLocale" value="false" />
    </bean>
Of course you use then taglibs for your content. You never hardcode a string in your application. That will allow you to ship to operations that concern.
...
 <spring:message code="welcome.loggedIn" />
...
Your properties file will need to live for example in a file like WEB-INF/i18n/messages.properties for the default language or WEB-INF/i18n/messages_de.properties for German in case that is not the default for you. The content is simple key values as we all know:
...
welcome.loggedIn=You are logged in as
...
A word of advice about Unicode: It can get complicated. For one java Property does not accept unicode. You can find a workaround for that issue as well as several hints to effectively handle Unicode in your app from a previous post I made three years ago.

Now your deployment script just needs to be able to drop the file from your repository into the exploded webapp and your new content will show up in the website. Easy, simple, agile.

No comments:

Followers