Download
FAQ
History
PrevHomeNext 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 applications follow 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"}, 

Establishing the Locale

To get the correct strings for a given user, a Web application either retrieves the locale (set by a browser language preference) from the request using the getLocale method, or allows the user to explicitly select the locale.

The JSTL versions of Duke's Bookstore automatically retrieve the locale from the request and store it in a localization context (see Internationalization Tag Library). It is also possible for a component to explicitly set the locale via the fmt:setLocale tag.

The JavaServer Faces version of Duke's Bookstore allows the user to explicitly select the locale. The user selection triggers a method that stores the locale in the FacesContext object. The locale is then used in resource bundle selection and is available for localizing dynamic data and messages (see Localizing Dynamic Data):

<h:commandLink id="NAmerica" action="storeFront"
actionListener="#{localeBean.chooseLocaleFromLink}">
  <h:outputText value="#{bundle.english}" />
</h:commandLink> 
public void chooseLocaleFromLink(ActionEvent event) {
  String current = event.getComponent().getId();
  FacesContext context = FacesContext.getCurrentInstance();
  context.getViewRoot().setLocale((Locale)
    locales.get(current));
} 

Setting the Resource Bundle

After the locale is set, the controller of a Web application typically retrieves the resource bundle for that locale and saves it as a session attribute (see Associating Objects with a Session) for use by other components:

  messages = ResourceBundle.
    getBundle("messages.BookstoreMessages", locale); 
  session.setAttribute("messages", messages); 

The resource bundle base name for the JSTL versions of Duke's Bookstore is set at deployment time through a context parameter. When a session is initiated, the resource bundle for the user's locale is stored in the localization context. It is also possible to override the resource bundle at runtime for a given scope using the fmt:setBundle tag and for a tag body using the fmt:bundle tag.

In the JavaServer Faces version of Duke's Bookstore, the JSP pages set the resource bundle using the f:loadBundle tag. This tag loads the correct resource bundle according to the locale stored in FacesContext.

<f:loadBundle basename="messages.BookstoreMessages"
  var="bundle"/> 

For information on this tag, see Referencing a ResourceBundle from a Page.

Retrieving Localized Messages

A Web component written in the Java programming language retrieves the resource bundle from the session:

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

Then it looks up the string associated with the key Talk as follows:

messages.getString("Talk"); 

The JSP versions of the Duke's Bookstore application uses the fmt:message tag to provide localized strings for messages, HTML link text, button labels, and error messages:

<fmt:message key="Talk"/> 

For information on the JSTL messaging tags, see Messaging Tags.

The JavaServer Faces version of Duke's Bookstore retrieves messages from the bundle variable (created in the preceding section) by using the following tag:

<h:outputText value="#{bundle.Talk}"/> 

For information on creating localized messages in JavaServer Faces, see Referencing a Localized Message.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.