Barebones Spring MVC part 8: Externalization and internationalization

August 29, 2010

Message externalization in the Web view layer digs the various text out of view templates and keeps them centralized and manageable. It also provides a convenient launchpad for site internationalization.

Once again, this capability is a snap with Spring MVC. This example builds upon part 1: core to introduce message externalization and internationalization into the view layer.

The following changes are required:

  1. Add a ResourceBundleMessageSource bean to the Spring context.
  2. Move messages from the JSP files into
  3. Translate messages from into British English in
  4. Add LocaleChangeInterceptor and SessionLocaleResolver beans to the Spring context.

Spring needs to know where it will find externalized messages. This is done with Java's resource bundle functionality, encapsulated in a Spring ResourceBundleMessageSource.


<bean id="messageSource"
    <property name="basename" value="messages" />

There isn't much in the way of messages in this example, but the little that is there is moved into a properties file named in the above ResourceBundleMessageSource.

value1=Value 1:
value2=Value 2:
value3=Value 3:
check1=Check 1:
check2=Check 2:
check3=Check 3:

This is also done in British English (or a reasonable facsimile thereof).

value1=Valueue 1:
value2=Valueue 2:
value3=Valueue 3:
check1=Cheque 1:
check2=Cheque 2:
check3=Cheque 3:

Finally, a couple of beans are added to the Spring context to allow detection of a user's desire to switch languages, and the ability to store that preference in the user's session.


<bean id="localeChangeInterceptor"
    <property name="paramName" value="lang" />

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

A user simply includes the HTTP request parameter lang=en_UK to change the language to British English.