Skip to main content

Best Practice: Developing Internationalized Applications for WebSphere V5 using JSTL

Nigel Morton (nmorton1@uk.ibm.com), Software Developer, IBM
Nigel Morton is a Software Engineer in the Web Services department at IBM Hursley Park in the United Kingdom.

Summary:  Developing Web applications for multiple languages is a complex process that can result in multiple copies of the same content in different languages. 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.

Date:  09 Dec 2003
Level:  Intermediate
Activity:  232 views

Introduction

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)

Recommendation

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
Default Web application in WebSphere Studio

Code samples

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
Output results

Alternative

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.


Resources

About the author

Nigel Morton is a Software Engineer in the Web Services department at IBM Hursley Park in the United Kingdom.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere, Rational
ArticleID=13603
ArticleTitle=Best Practice: Developing Internationalized Applications for WebSphere V5 using JSTL
publish-date=12092003
author1-email=nmorton1@uk.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers