Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
Providing Localized Messages and Labels
Messages and labels should be tailored according to the conventions of a user's language and region. There are two approaches to providing localized messages and labels in a Web application:
- Provide a version of the JSP page in each of the target locales and have a controller servlet dispatch the request to the appropriate page depending on the requested locale. This approach is useful if large amounts of data on a page or an entire Web application need to be internationalized.
- Isolate any locale-sensitive data on a page into resource bundles, and access the data so that the corresponding translated message is fetched automatically and inserted into the page. Thus, instead of creating strings directly in your code, you create a resource bundle that contains translations and read the translations from that bundle using the corresponding key.
The Duke's Bookstore application follows the second approach. Here are a few lines from the default resource bundle
messages
.BookstoreMessages.java
:{"TitleCashier", "Cashier"}, {"TitleBookDescription", "Book Description"}, {"Visitor", "You are visitor number "}, {"What", "What We're Reading"}, {"Talk", " talks about how Web components can transform the way you develop applications for the Web. This is a must read for any self respecting Web developer!"}, {"Start", "Start Shopping"},To get the correct strings for a given user, a Web component retrieves the locale (set by a browser language preference) from the request using the
getLocale
method, opens the resource bundle for that locale, and then saves the bundle as a session attribute (see Associating Attributes with a Session, page 641):ResourceBundle messages = (ResourceBundle)session. getAttribute("messages"); if (messages == null) { Locale locale=request.getLocale(); messages = ResourceBundle. getBundle("messages.BookstoreMessages", locale); session.setAttribute("messages", messages); }A Web component retrieves the resource bundle from the session:
and looks up the string associated with the key
TitleCashier
as follows:The JSP versions of the Duke's Bookstore application uses the
fmt:message
tag to provide localized strings for introductory messages, HTML link text, button labels, and error messages. For more information on the JSTL messaging tags, see Messaging Tags (page 705).
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.