Home
TOC Index |
Internationalization Tags
In Internationalizing and Localizing Web Applications we discussed the how to adapt Web applications to the language and formatting conventions of client locales. This section describes tags that support the internationalization of JSP pages.
- Setting the locale for a page
- Creating locale-sensitive messages
- Formatting and parsing data elements such as numbers, currencies, dates, and times in a locale-sensitive or customized manner
Setting the Locale
The
setLocale
tag is used to override the client-specified locale for a page. TherequestEncoding
tag is used to set the request's character encoding, in order to be able to correctly decode request parameter values whose encoding is different from ISO-8859-1.Messaging Tags
By default, browser-sensing capabilities for locales are enabled. This means that the client determines (via its browser settings) which locale to use, and allows page authors to cater to the language preferences of their clients.
bundle Tag
You use the
bundle
tag to specify a resource bundle for a page.To define a resource bundle for a Web application you specify the context parameter
javax.servlet.jsp.jstl.fmt.localizationContext
in the Web application deployment descriptor. Here is the declaration from the Duke's Bookstore descriptor:<context-param> <param-name> javax.servlet.jsp.jstl.fmt.localizationContext </param-name> <param-value>messages.BookstoreMessages</param-value> </context-param>message Tag
The
message
tag is used to output localized strings. The following tag fromcatalog.jsp
<h3><fmt:message key="Choose"/></h3>is used to output a string inviting customers to choose a book from the catalog.
The
param
subtag provides a single argument (for parametric replacement) to the compound message or pattern in its parentmessage
tag. Oneparam
tag must be specified for each variable in the compound message or pattern. Parametric replacement takes place in the order of theparam
tags.Formatting Tags
JSTL provides a set of tags for parsing and formatting locale-sensitive numbers and dates.
The
formatNumber
tag is used to output localized numbers. The following tag fromshowcart.jsp
<fmt:formatNumber value="${book.price}" type="currency"/>is used to display a localized price for a book. Note that since the price is maintained in the database in dollars, the localization is somewhat simplistic, because the
formatNumber
tag is unaware of exchange rates. The tag formats currencies but does not convert them.Analogous tags for formatting dates (
formatDate
), and parsing numbers and dates (parseNumber
,parseDate
) are also available. ThetimeZone
tag establishes the time zone (specified via thevalue
attribute) to be used by any nestedformatDate
tags.In
receipt.jsp
, a "pretend" ship date is created and then formatted with theformatDate
tag:<jsp:useBean id="now" class="java.util.Date" /> <jsp:setProperty name="now" property="time" value="<%= now.getTime() + 432000000 %>" /> <fmt:message key="ShipDate"/> <fmt:formatDate value="${now}" type="date" dateStyle="full"/>.
Home
TOC Index |
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.