Home TOC |
![]() ![]() ![]() |
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.
JSTL defines two sets of tags:
- Messaging tags assist page authors with creating messages that can be adapted to any locale available in the JSP container
- Formatting tags allow various data elements such as numbers, currencies, dates and times to be formatted and parsed in a locale-sensitive or customized manner.
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.
The
locale
tag is used to override the client-specified locale for a page.Specifying a Bundle
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.i18n.basename
in the Web application deployment descriptor. Here is the declaration from the Duke's Bookstore descriptor:<context-param> <param-name> javax.servlet.jsp.jstl.i18n.basename </param-name> <param-value>messages.BookstoreMessages</param-value> </context-param>Message Tags
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
messageFormat
tag performs parametric replacement on a given pattern string, using the runtime's default locale. The pattern string may be specified via thevalue
attribute; if missing, it is read from the tag's body content.The
messageArg
tag provides a single argument (for parametric replacement) to the compound message or pattern in its parentmessage
ormessageFormat
tag, respectively. OnemessageArg
tag must be specified for each variable in the compound message or pattern. Parametric replacement takes place in the order of themessageArg
tags.Formatting Tags
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 phony, because the 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.
Home TOC |
![]() ![]() ![]() |