|
Download
FAQ History |
|
API
Search Feedback |
Internationalization Tags
Chapter 23 covers how to design Web applications so that they conform to the language and formatting conventions of client locales. This section describes tags that support the internationalization of JSP pages.
JSTL defines tags for: setting the locale for a page, creating locale-sensitive messages, and formatting and parsing data elements such as numbers, currencies, dates, and times in a locale-sensitive or customized manner. Table 17-6 lists the tags.
JSTL i18n tags use a localization context to localize their data. A localization context contains a locale and a resource bundle instance. To specify the localization context, you define the context parameter
javax.servlet.jsp.jstl.fmt.localizationContext, whose value can be ajavax.servlet.jsp.jstl.fmt.LocalizationContextor aString. AStringcontext parameter is interpreted as the name of a resource bundle basename. For the Duke's Bookstore application, the context parameter is theStringmessages.BookstoreMessages, which is set withdeploytoolin the Context tab of the WAR inspector. This setting can be overridden in a JSP page by using the JSTLfmt:setBundletag. When a request is received, JSTL automatically sets the locale based on the value retrieved from the request header and chooses the correct resource bundle using the basename specified in the context parameter.Setting the Locale
The
setLocaletag is used to override the client-specified locale for a page. TherequestEncodingtag is used to set the request's character encoding, in order to be able to correctly decode request parameter values whose encoding is different fromISO-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
bundletag 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.localizationContextin the Web application deployment descriptor.message Tag
The
messagetag is used to output localized strings. The following tag frombookcatalog.jspis used to output a string inviting customers to choose a book from the catalog.
The
paramsubtag provides a single argument (for parametric replacement) to the compound message or pattern in its parentmessagetag. Oneparamtag must be specified for each variable in the compound message or pattern. Parametric replacement takes place in the order of theparamtags.Formatting Tags
JSTL provides a set of tags for parsing and formatting locale-sensitive numbers and dates.
The
formatNumbertag is used to output localized numbers. The following tag frombookshowcart.jspis 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
formatNumbertag 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. ThetimeZonetag establishes the time zone (specified via thevalueattribute) to be used by any nestedformatDatetags.In
bookreceipt.jsp, a "pretend" ship date is created and then formatted with theformatDatetag:
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.