lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



Spring MVC Internationalization


Text labels and headings can be externalized into resource bundles, allowing Spring's internationalization features to automatically display web pages in the appropriate language.

Tip
Tip - as an alternative, check out the Google website translator gadget, which can dynamically translate web pages into 51 languages (note that, in Google's own words, "it's not a perfect substitute for the art of professional translation").

Resource Bundles

We set up three resource bundle files called messages_en.properties (English), messages_fr.properties (French) and messages.properties (the default if no other file is found).

The files contain key-value pairs representing text for the web pages in different languages.

messages_en.properties
application.name=hello world

#navigation.back=Back
#falls back to messages.properties

country.plural=Countries
country.details=Country Details
country.name=Name
country.area=Area
country.population=Population
country.updatedOn=Last Updated
country.currency=Currency

messages_fr.properties
application.name=bonjour monde

navigation.back=Retournez

country.plural=Pays
country.details=Détails de pays
country.name=Nom
country.area=Taille
country.population=Population
country.updatedOn=Dernier mis à jour
country.currency=Devise

messages.properties
application.name=hello world

navigation.back=Back

country.plural=Countries
country.details=Country Details
country.name=Name
country.area=Area
country.population=Population
country.updatedOn=Last Updated
country.currency=Currency
We tell Spring how to locate these resource bundles in the configuration section.

JSP Pages

Next, we convert our JSP pages to use the <spring:message> tag from the Spring tag library to display static text, and to use the JSTL <fmt:formatNumber> and <fmt:formatDate> tags to display numbers and dates.
<table>
  <tr>
    <td><spring:message code="country.name"/></td>
    <td>${country.name}</td>
  </tr>
  <tr>
    <td><spring:message code="country.area"/></td>
    <td><fmt:formatNumber type="number" value="${country.area}" /></td>
  </tr>
  <tr>
    <td><spring:message code="country.population"/></td>
    <td><fmt:formatNumber type="number" value="${country.population}" /></td>
  </tr>
  <tr>
    <td><spring:message code="country.updatedOn"/></td>
    <td><fmt:formatDate value="${country.populationLastUpdated}"/></td>
  </tr>
  <tr>
    <td><spring:message code="country.currency"/></td>
    <td>${country.currency}</td>
  </tr>
</table>

Locale

The appropriate resource bundle (and therefore, the appropriate set of messages to be displayed) is automatically selected by Spring, based on the end user's locale. If the display language setting is English, then the messages_en.properties file will be used. If the language is French then the messages_fr.properties file will be used, and so on.

If a file is not available for a language, or a message within a file cannot be found (for example, navigation.back in messages_en.properties has been commented out), then Spring will fall back to using the entries in messages.properties.

The same JSP page can now be displayed in Engish or French.

English
Hello World Application in English

French
Hello World Application in French


Tip
Tip - change the language preferences in your browser to access the different resource bundles for the application.

Formatting

In addition to Spring's <spring:message> tag, we also use the JSTL <fmt:formatNumber> and <fmt:formatDate> tags to display numbers and dates.

A number that is displayed as 999,999,999 in English is displayed as 999 999 999 in French, and dates displayed as Mon dd, yyyy in English are displayed as dd mon. yyyy in French. Month names and their abbreviations are also converted to the appropriate language.
»