Simplifying I18N for Java Web Applications

Today’s e-commerce sites like to cater to users across the globe. Process of enabling any application to adapt many languages based on user location is know as Internationalization or or I18N (I – eighteen letters -N) as it is often referred.

Java web applications using Spring framework or JSTL taglib can use the below steps to support I18N in their application. Once implemented this feature provides an easy way to support new languages in no time. All one needs is a language translator.
Let us now see the steps involved in making your java web application I18N enabled.
1. Spring gives us a flexibility to set message source for the application with the help of ResourceBundleMessageSource class. Below configuration can be used to set the message source. Multiple locations can be loaded as part of basenames.

<!-- MESSAGES CONFIGURATION -->
 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolve" />
 <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
 <property name="paramName" value="language" />
 </bean>
 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource”>
  <property name="basenames">
 <list>
 <value>classpath:messages</value>
 </list>
 </property>
 <property name="defaultEncodinmg" value="UTF-8"/>
 </bean>

CSS – border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;

Make sure to add localeChangeInterceptor to interceptors list.
Naming convention of the language files should be _.properties. For example in above code snippet, basename used is messages which means the properties to be named as messages_enus.properties, messages_frfr.properties, etc. If a languages specific resource is not found, then default messages.properties will be used.
If there is a frequent update to the properties files then it is recommended to use ReloadableResourceBundleMessageSource.

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource”>
  <property name="basenames">
 <list>
 <value>path/to/messages</value>
 </list>
 </property><property name="defaultEncodinmg" value="UTF-8"/>
 </bean>

CSS – border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;

This spring class gives you the flexibility to reload properties after an interval specified by cacheSeconds attribute of the class. Basically this is the timeframe the properties will be cached before an attempt is made to load them again only if the last modified timestamp is different from the previous reload.
Note : For correct working of ReloadableResourceBundleMessageSource, ensure that the resources are loaded from a different location and not the classpath. Since resources loaded from the classpath are generally cached.

2. All views should use property key for every text shown on the UI.
JSTL tag can be used to achieve this. Below is the demonstration of tag usage, where fmt:setBundle is to set the property file namely ‘myprop’.

<fmt:setBundle basename="myprop" var="mypropvalue"/>
 <fmt:setLocale value="en"/>
 <fmt:message key="mypropkey1" bundle="${mypropvalue}"/>
 <fmt:message key="mypropkey2" bundle="${mypropvalue}"/>
 <fmt:message key="mypropkey3" bundle="${mypropvalue}"/>

CSS – border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;

If you are using JSP with Spring, then the making use of the same resourcebundle as that loaded by Spring will suffice. To achieve it, add following to the viewResolver for JSTL tags to be localized.

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

CSS – border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;

In JSP, resourceBundle need not be configured and tags can be used as below :

<fmt:message key="mypropkey1"/>
 <fmt:message key="mypropkey2"/>
 <fmt:message key="mypropkey3"/>

CSS – border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;

Following are the bottlenecks one might come across during implementation.

  • Resource paths incorrectly set in tag and messageSource attribute in bean configuration.
  • Non – English resources being saved using a different format – such as ANSI instead of UTF-8.

Author

  • Shruthi D

    Shruthi is currently working as a Senior Software Engineer in Trigent. She has more than 6 years of experience in Java web development. She has expertise in Spring, iBatis, CometD, PostgreSQL and DOJO.