Developing Web applications for multiple languages is a complex process that can result in multiple copies of the same content in different languages. This makes the content difficult to maintain and prone to errors. By using Java® Server Tag Language (JSTL) to perform the internationalization and perform the work of detecting and outputting foreign language text on Web pages, you can display a single page in multiple languages. Because only one page is required, you can significantly reduce the time required for development and maintenance. Using this method for development, applications are internationalized and new languages are added to applications by simply providing properties files with the translated language strings.
This best practice applies to the following product, version, and plaform:
- WebSphere Application Server Advanced V5.x (all platforms)
- WebSphere Studio Application Developer V5.x (all platforms)
JSTL is a suite of predefined custom tags that allow access to back end Java classes that can perform a number of functions. JSTL allows a Web developer access to a suite of custom tags that he or she can embed into JSP pages to perform a number of functions from language setting to logic.
Using JSTL tags, you can easily set up internationalized Web pages and eliminate the need for coding the same page for each different language. JSTL lets your application read strings from a ResourceBundle file that is translated for each language that you want to support. Strings are read from the resource bundle by the JSTL code and the Web page is created with these strings displayed.
You can easily add JSTL to a JSP in WebSphere Studio Application Developer (hereafter called WebSphere Studio) at creation time. When you get to the "Tag Libraries" page in the wizard, select Add and choose the libraries you want. For internationalization, select the http://java.sun.com/jstl/fmt library. This adds the necessary libraries to your project. If you haven't already added the libraries to your project, you are prompted with a dialog asking for the JSP tag library to import. By default, this is standard.jar. However, if standard.jar is not the jar file listed, then browse to the directory containing standard.jar and select it. You then select the http://java.sun.com/jstl/fmt library.
To speed up development of your Web application, set up a default template that contains the required JSTL declarations and any other formatting and style sheets that you want in your application. Use the template for each new JSP, customizing the page where necessary. Do not add any plain text that needs to be displayed on the Web page. Instead, use a convention, such as ORDER.ADDRESS, for an order page. Keep sections of strings together in the resource bundle file so they are easy to locate. Examples of a JSP template and resource bundle file are shown in "Code samples" below.
When using WebSphere Studio for development, the resource bundle file for the language strings must be in the Java Source folder of the Web project so that it gets built and correctly added to the Web project. The files for a default Web application project in WebSphere Studio are shown below.
Figure 1: Default Web application in WebSphere Studio
List any code samples.
template.jsp
<doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<fmt:setBundle basename="Messages" scope="session" />
<head>
<TITLE><fmt:message key="TITLE" /><TITLE>
<link rel="stylesheet" href="theme/Master.css">
</head>
<fmt:message key="TEMPLATE.STRING1" /><BR>
<fmt:message key="TEMPLATE.STRING2" /><BR>
<fmt:message key="TEMPLATE.STRING3" /><BR>
<html> |
Messages_en.properties (ResourceBundle for English)
TITLE=My JSP title
TEMPLATE.STRING1=Message string 1
TEMPLATE.STRING2=Message string 2
TEMPLATE.STRING3=Message string 3
|
Messages_es.properties (ResourceBundle for Spanish)
TITLE= Mi título de JSP
TEMPLATE.STRING1=Secuencia del mensaje 1
TEMPLATE.STRING2= Secuencia del mensaje 2
TEMPLATE.STRING3= Secuencia del mensaje 3 |
The result of the different outputs are shown below.
Figure 2: Output results
The standard alternative to using JSTL for internationalization requires users to select their language when presented with the initial page (often by clicking on their countries flag or language). This redirects the user to the set of Web pages that have been translated into that language. This becomes difficult to manage when multiple languages are required for an application.
Another common alternative to JSTL is to use the Java Format classes. This is a complex process and requires embedding of Java code within the JSP pages. Using JSTL tags removes the need for embedding Java into the JSP pages because the formatting function is now encapsulated within these easy-to-use tags.
Comments (Undergoing maintenance)





