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
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. 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: TestServlet.javapackage test; import java.io.IOException; import java.io.PrintWriter; 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 { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("accept-language header: <em>" + request.getHeader("accept-language") + "</em>"); out.println("<hr/>"); Locale locale = request.getLocale(); out.println("Preferred Locale:<br/>"); out.println(displayLocaleInfo(locale)); out.println("<hr/>"); ResourceBundle bundle = ResourceBundle.getBundle("TestBundle", locale); out.println("Question: " + bundle.getString("my.question")); out.println("<hr/>"); out.println("Acceptable Locales:<br/>"); Enumeration<?> locales = request.getLocales(); while (locales.hasMoreElements()) { Locale loc = (Locale) locales.nextElement(); out.println(displayLocaleInfo(loc)); } } 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()); sb.append("</em><br/>"); return sb.toString(); } } The contents of the three properties files are shown below: TestBundle.propertiesmy.hello=Hello (default) my.goodbye=Bye (default) my.question=Do you speak English? (default) TestBundle_en_US.propertiesmy.hello=Hello my.goodbye=Bye my.question=Do you speak English? TestBundle_sv_SE.propertiesmy.hello=Hejsan my.goodbye=Hejd? my.question=Pratar du engelska? (Continued on page 2) |