Using Localized Messages

All data and messages in the Duke's Bookstore application have been localized for Spanish, French, German, and American English. Performing Localization explains how to produce the localized messages as well as how to localize dynamic data and messages.

The image map on the first page allows you to select your preferred locale. See Chapter 20 for information on how the image map custom component was created.

This section explains how to use localized static data and messages for JavaServer Faces applications. If you are not familiar with the basics of localizing web applications, see Chapter 22. Localized static data can be included in a page by using the loadBundle tag, defined in jsf_core.tld. Follow these steps:

  1. Reference a ResourceBundle from the page.
  2. Reference the localized message located within the bundle.

A ResourceBundle contains a set of localized messages. For more information about resource bundles, see 

After the application developer has produced a ResourceBundle, the application architect puts it in the same directory as the application classes. Much of the data for the Duke's Bookstore application is stored in a ResourceBundle called BookstoreMessages.

Referencing a ResourceBundle from a Page

For a page with JavaServer Faces tags to use the localized messages contained in a ResourceBundle, the page must reference the ResourceBundle using a loadBundle tag.

The loadBundle tag from bookstore.jsp is

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

The basename attribute value refers to the ResourceBundle, located in the messages package of the bookstore application. Make sure that the basename attribute specifies the fully qualified class name of the file.

The var attribute is an alias to the ResourceBundle. This alias can be used by other tags in the page in order to access the localized messages.

Referencing a Localized Message

To reference a localized message from a ResourceBundle, you use a value-binding expression from an attribute of the component tag that will display the localized data. You can reference the message from any component tag attribute that is value-binding-enabled.

The value-binding expression has the notation "var.message", in which var matches the var attribute of the loadBundle tag, and message matches the key of the message contained in the ResourceBundle referred to by the var attribute. Here is an example from bookstore.jsp:

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

Notice that bundle matches the var attribute from the loadBundle tag and that Talk matches the key in the ResourceBundle.

Another example is the graphicImage tag from chooselocale.jsp:

<h:graphicImage id="mapImage" url="/template/world.jpg" 
  usemap="#worldMap" /> 

The alt attribute is value-binding-enabled, and this means that it can use value-binding expressions. In this case, the alt attribute refers to localized text, which will be included in the alternative text of the image rendered by this tag.

See Creating the Component Tag Handler and Enabling Value-Binding of Component Properties for information on how to enable value binding on your custom component's attributes.