Pages

Tuesday, December 1, 2009

Internationalization using JSTL

The Java EE 5 platform is a powerful enterprise-level server development platform which in my opinion is very productive including to develop small web modules, provided you've got a suitable Application Server or Servlet Container to run it.

As far as it concerns web development and authoring, Java EE 5 has got some great APIs such as servlet's, JSP's and JSF's. The JavaServer Pages Specification (JSR 245) includes JSTL (JavaServer Pages Standard Tag Library, JSR 052), which are JSP standard tag extensions that include internationalization features.

Creating a Resource Bundle

The first thing you ought to do when internationalizing your application is creating resource bundles. Resource bundles are Java Properties file, organized for locales, whose aim is separating string literal according to the locale they're meant for.

Let's suppose you want to use three locales in your application:
  • en_US
  • it_IT
  • es_ES

Create for Java Properties files, one per locale and one as a fallback. The naming scheme is [bundle-name]_[locale].properties, so that you'll end up with:

bundle_en_US.properties
bundle_it_IT.properties
bundle_es_ES.properties

Some advances Java IDE, such as NetBeans, let you manage properties files with visual tools and perform basic operations, such as adding a locale or filling up the properties, with a fancy GUI.

Filling the Resource Bundles Up

Filling up properties files is pretty easy: such files contain records with this basic structure:

key=value

In this case you would translate every key value for the correct locale.

Using a Bundle from a JavaServer Page with a JSTL tag

JSTL lets you easily use resource bundles in a JSP. The first thing you've got to do is declaring that you're going to use the JSTL's fmt tag library:

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

The next thing is telling the library which bundle you're going to use. Let's suppose you're going to use com.domain.YourBundle:

<fmt:setBundle basename="com.domain.YourBundle" />

The last thing is using the retrieved values where you need them:

<fmt:message key="your.key" />

Determining the user locale

Maybe you're wondering how JSTL determines which is the user locale. JSTL, by default, determines the user locale by examining the headers sent by the browser and applying a best match algorithm. If your preferred language settings show (in this order)
  1. English
  2. Spanish
  3. Italian

the resulting application will show up in English. The JSTL Specification will give you further details about how to establish a default locale for a web application using JSTL.

In another post, I'll show you how I resolved the problem of locale switching for a given user in an application of ours.

Further Readings




No comments:

Post a Comment