Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

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:

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):

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:

ResourceBundle messages =
  (ResourceBundle)session.getAttribute("messages"); 

and looks up the string associated with the key TitleCashier as follows:

messages.getString("TitleCashier"); 

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.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

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.