How do I internationalize my web application?
Author: Deron Eriksson
Description: This tutorial describes how to internationalize a web application with locales and resource bundles.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20


Page: < 1 2

(Continued from page 1)

Now, let's fire up the application and test it. First off, for this demonstration, we'll modify our browser's accepted languages. To do this in Internet Explorer 7, I brought up the Tools → Internet Options window. On the General tab, I clicked the Languages button.

IE7 Languages button

In the Language Preference window, we can see that my browser's default setting is to US English (en-US).

Language Preference

If we hit TestServlet, we can see that the accept-language header is "en-US". The preferred locale is "en_US". The question that is displayed comes from the TestBundle_en_US.properties bundle. The acceptable locales only shows US English.

Hitting test servlet with browser

Now, in my browser's Language Preference, I'll add "sv-SE", which is Swedish in Sweden. After that, I have two accepted languages, English-US and Swedish-Sweden, in that order.

Adding 'sv-SE' to Language Preference

Hitting the TestServlet with my browser, I can now see that the "accept-language" is en-US,sv-SE;q=0.5. Ths q value is a language weighting. We once again see that English-US is the preferred locale, so we again see the question is displayed in English from the TestBundle_en_US.properties bundle. We see that there are now two acceptable locales in the Locale Enumeration from request.getLocales(), with English listed first.

Hitting test servlet with browser

In the browser Language Preference window, I'll try bumping Swedish above English.

Setting Swedish as browser preferred language

If we hit TestServlet, we can see the "accept-language" header is sv-SE,en-US;q=0.5. The preferred locale is Swedish, so now the question appears in Swedish since it comes from the TestBundle_sv_SE.properties bundle. The acceptable locales Enumeration still shows two locales, but this time the Swedish locale comes first.

Hitting test servlet with browser

In this example, we've seen how we can use the request's getLocale() method to get a Locale object that we can use to get a particular ResourceBundle object for a particular language. This bundle allows us to pull language-specific content into our application for display to end users in their desired languages.

Page: < 1 2