Internationalization Tag Library - The Java EE 5 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
Flow Control Tags in the Core Tag Library
Further Information about JSTL
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
Internationalization Tag Library
Chapter 15, Internationalizing and Localizing Web Applications 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 7-6 lists the tags.
Table 7-6 Internationalization Tags
Area | Function | Tags | Prefix |
---|---|---|---|
I18N | Setting Locale | setLocale requestEncoding | fmt |
Messaging | bundle message param setBundle | ||
Number and Date Formatting | formatNumber formatDate parseDate parseNumber setTimeZone timeZone |
JSTL I18N tags use a localization context to localize their data. A localization contextcontains a locale and a resource bundle instance. To specify the localization context at deployment time, you define the context parameter javax.servlet.jsp.jstl.fmt.localizationContext, whose value can be a javax.servlet.jsp.jstl.fmt.LocalizationContext or a String. A String context parameter is interpreted as a resource bundle base name. For the Duke’s Bookstore application, the context parameter is the String messages.BookstoreMessages. 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 base name specified in the context parameter.
Setting the Locale
The setLocale tag is used to override the client-specified locale for a page. The requestEncoding 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, the capability to sense the browser locale setting is enabled in JSTL. This means that the client determines (through its browser setting) which locale to use, and allows page authors to cater to the language preferences of their clients.
The setBundle and bundle Tags
You can set the resource bundle at runtime with the JSTL fmt:setBundle andfmt:bundle tags. fmt:setBundle is used to set the localization context in a variable or configuration variable for a specified scope. fmt:bundle is used to set the resource bundle for a given tag body.
The message Tag
The message tag is used to output localized strings. The following tag from tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookcatalog.jsp 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 parent message tag. One param tag must be specified for each variable in the compound message or pattern. Parametric replacement takes place in the order of the param 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 from_tut-install_/javaeetutorial5/examples/web/bookstore4/web/books/bookshowcart.jsp is used to display a localized price for a book.
<fmt:formatNumber value="${book.price}" type="currency"/>
Note that because 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 for parsing numbers and dates (parseNumber, parseDate) are also available. The timeZone tag establishes the time zone (specified with the valueattribute) to be used by any nested formatDate tags.
In tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookreceipt.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.time + 432000000}" /> <fmt:message key="ShipDate"/> <fmt:formatDate value="${now}" type="date" dateStyle="full"/>.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices