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 >

In another tutorial, we saw how we could internationalize a JavaSW application with resource bundles and locales. This tutorial will show how to internationalize a web application using the same technologies in conjunction with the locale specified by a browser request. The demonstration project consists of one servletW, TestServlet. It has three properties files located at the root of the src directory.

'tomcat-demo' project

The TestServlet class begins by displaying the value of the "accept-language" header, which is a list of the locales that a particular browser would like to see. Following this, gets a Locale object from request.getLocale(), which represents the browser's preferred locale. Information about the Locale is displayed. The resource bundle (set of properties from the corresponding properties file) is obtained for the preferred locale by the call to

ResourceBundle bundle = ResourceBundle.getBundle("TestBundle", locale);

The value of "my.question" is pulled from the resource bundle and displayed via

out.println("Question: " + bundle.getString("my.question"));

Following this, the TestServlet class also gets an Enumeration to all of the browser's accepted locales via the call to request.getLocales(). It displays information about these locales. The TestServlet class is shown below:

package test;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		out.println("accept-language header: <em>" + request.getHeader("accept-language") + "</em>");


		Locale locale = request.getLocale();
		out.println("Preferred Locale:<br/>");


		ResourceBundle bundle = ResourceBundle.getBundle("TestBundle", locale);
		out.println("Question: " + bundle.getString("my.question"));


		out.println("Acceptable Locales:<br/>");
		Enumeration<?> locales = request.getLocales();
		while (locales.hasMoreElements()) {
			Locale loc = (Locale) locales.nextElement();


	private String displayLocaleInfo(Locale locale) {
		StringBuffer sb = new StringBuffer();
		sb.append("Locale: <em>" + locale.toString());
		sb.append("</em>, Language: <em>" + locale.getLanguage());
		sb.append("</em>, Country: <em>" + locale.getCountry());
		sb.append("</em>, Display name: <em>" + locale.getDisplayName());
		return sb.toString();


The contents of the three properties files are shown below:

my.hello=Hello (default)
my.goodbye=Bye (default)
my.question=Do you speak English? (default)

my.question=Do you speak English?

my.question=Pratar du engelska?

(Continued on page 2)

Page:    1 2 >