Home > Articles > Programming > Java

Using JSTL for Internationalization (I18N)

  • Print
  • + Share This
Por favor Conexión: Creating a Web site that supports more than one language can be a challenging task. Jeff Heaton shows you how to use JSP Standard Tag Library (JSTL) to construct a multilingual Web site that interacts with users in English, Spanish and Chinese.
From the author of

This article will show you how to create a multilingual website using the JSP Standard Tag Library (JSTL). You will create a simple login page that can display itself in English, Spanish, and Chinese. These languages allow us to test with two Latin-based languages (Spanish and English) and one non-Latin-based language (Chinese).

Constructing the Login Page

First we will examine the login page, which is saved as a simple JSP file named index.jsp. You can see the complete contents of this file in Listing 1 at the end of this article. At the top of the file you will notice the JSP page directive used to set the content type for the page.

<%@ page contentType="text/html; charset=UTF-8" %>

This tag specifies that UTF-8 encoding be used for this page. If you do not include this directive, you cannot display multibyte character sets properly. For this example, we are using Chinese for one of the languages. Chinese does require a multibyte character set.

Next you must declare which of the JSTL tag libraries we are going to use. The following lines of code do this:

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

Here you can see that we are using the format (fmt) and core (c) tag libraries. The format tag library contains tags that are necessary for formatting text. This formatting also includes internationalization and multilingual support. The core tag library contains very basic functionality such as "if" statements.

Next you should check to see whether the resource bundle is loaded or not. The resource bundle contains all the text strings that will be displayed by the application. By organizing your strings into a resource bundle, you can simply use a different resource bundle when you want to switch to another language. The following lines of code ensure that the resource bundle is loaded:

<c:if test="${lang==null}">
 <fmt:setBundle basename="com.heaton.bundles.Forum" var="lang" scope="session"/>

After you are sure that the resource bundle is loaded, you can switch the language being used. The next thing that the login page will do is check to see whether the user has changed the language that the application is using. When you examine the login screen, you will see that there are three hyperlinks that allow you to select the appropriate language.

Finally, the login.jsp page now contains the regular HTML that is used to provide the form. Text is not put directly into the JSP page, however. Any place text might appear, a formatting tag appears instead. This formatting tag maps to the resource bundle and specifies which string should be displayed instead of the JSTL tag. For example, the following tag displays the text "Please Login" in the appropriate language.

<fmt:message key="login.pleaselogin" bundle="${lang}"/>

If you try to bring up the site without the resource bundle loaded, you will get errors for all of your strings. Bringing up the website in this state will produce output similar to Figure 1.

Figure 1Figure 1 The login screen with no language.

  • + Share This
  • 🔖 Save To Your Account