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 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, page 192). 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, page 403):

   <h:commandLink  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, page 89) for use by other components:

   messages = ResourceBundle.      getBundle("com.sun.bookstore.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.

The JavaServer Faces version of Duke's Bookstore uses two methods for setting the resource bundle. One method is letting 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 Loading a Resource Bundle (page 356).

Another way a JavaServer Faces application sets the resource bundle is by configuring it in the application configuration file. There are two XML elements that you can use to set the resource bundle: message-bundle and resource-bundle.

If the error messages are queued onto a component as a result of a converter or validator being registered on the component, then these messages are automatically displayed on the page using the message or messages tag. These messages must be registered with the application using the message-bundle tag:

   <message-bundle>      resources.ApplicationMessages    </message-bundle>


For more information on using this element, see Registering Custom Error Messages (page 470).

Resource bundles containing messages that are explicitly referenced from a JavaServer Faces tag attribute using a value expression must be registered using the resource-bundle element of the configuration file:

   <resource-bundle>      <base-    name>com.sun.bookstore6.resources.CustomMessages</base-name>      <var>customMessages</var>    </resource-bundle>


For more information on using this element, see Registering Custom Localized Static Text (page 471)

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 (page 193).

The JavaServer Faces version of Duke's Bookstore retrieves messages using either the message or messages tag, or by referencing the message from a tag attribute using a value expression.

You can only use a message or messages tag to display messages that are queued onto a component as a result of a converter or validator being registered on the component. The following example shows a message tag that displays the error message queued on the userNo input component if the validator registered on the component fails to validate the value the user enters into the component.

   <h:inputText  value="#{UserNumberBean.userNumber}">      <f:validateLongRange minimum="0" maximum="10" />      ...    <h:message      style="color: red;      text-decoration: overline"  for="userNo"/>


For more information on using the message or messages tags, see Displaying Error Messages with the message and messages Tags (page 354).

Messages that are not queued on a component and are therefore not loaded automatically are referenced using a value expression. You can reference a localized message from almost any JavaServer Faces tag attribute.

The value expression that references a message has the same notation whether you loaded the resource bundle with the loadBundle tag or registered it with the resource-bundle element in the configuration file.

The value expression notation is var.message, in which var matches the var attribute of the loadBundle tag or the var element defined in the resource-bundle element of the configuration file, and message matches the key of the message contained in the resource bundle, 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 resource bundle.

For information on using localized messages in JavaServer Faces, see Rendering Components for Selecting Multiple Values (page 349).



The JavaT EE 5 Tutorial
The JavaT EE 5 Tutorial
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 309

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net