Download PDF Home History |  | API Search Feedback FAQ

> ## 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). 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.

Download PDF Home History |  | API Search Feedback FAQ

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.