Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

Performing Localization

For this release, all data and messages in the cardemo application have been completely localized for French, German, Latin-American Spanish, and American English.

The image map on the first page allows you to select your preferred locale. See Creating Custom UI Components (page 903) for information on how the image map custom component was created.

This section explains how to localize static and dynamic data and messages for JavaServer Faces applications. If you are not familiar with the basics of localizing Web applications, see Internationalizing and Localizing Web Applications.

Localizing Static Data

Static data can be localized using the JSTL Internationalization tags by following these steps:

  1. After you declare the html_basic and jsf-core tag libraries in your JavaServer Faces page, add a declaration for the JSTL fmt tag library:
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
        prefix="fmt" %>

  3. Create a Properties file containing the localized messages.
  4. Add an fmt:setBundle tag:
  5. <fmt:setBundle
        basename="cardemo.Resources"
        scope="session" var="carDemoBundle"/>

    The basename attribute value refers to the Properties file, located in the cardemo package. Make sure the basename attribute specifies the fully qualified class name of your Resources file. This file contains the localized messages.

    The scope attribute indicates the scope--either application, session, or page--for which this bundle can be used.

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

    Add a key attribute to a component tag to access the particular localized message and add the bundle attribute to refer to the file containing the localized message. The bundle attribute must exactly match the var attribute in the fmt:setBundle tag. Here is an example from more.jsp:
    <h:output_text
      key="OptionsPackages" bundle="carDemoBundle" />

For more information on using the JSTL Internationalization functionality, please refer to Internationalization Tags (page 703).

Localizing Dynamic Data

The cardemo application has some data that is set dynamically in JavaBeans classes. Because of this, the beans must load the localized data themselves; the data can't be loaded from the page.

One example of dynamically-loaded data includes the data associated with a UISelectOne component. Another example is the car description that appears on the more.jsp page. This description corresponds to the car the user chose from the Storefront.jsp page. Since the chosen car is not known to the application prior to startup time, the localized description cannot be loaded from the page. Instead, the CurrentOptionServer bean must load the localized car description.

In the CurrentOptionServer bean, the localized car title and description is loaded with the setCarId(int) method, which is called when the user selects a car from Storefront.jsp. Here is a piece of the setCarId(int) method:

public void setCarId(int id) {
  try { 
    ResourceBundle rb; 
    switch (id) {
      case 1:
        // load car 1 data
        String optionsOne = "cardemo/CarOptions1";
        rb = ResourceBundle.getBundle(
          optionsOne, 
          (FacesContext.getCurrentInstance().getLocale()));
        setCarImage("/200x168_Jalopy.jpg");
        break;
  ...
  this.setCarTitle((String)rb.getObject("CarTitle"));
  this.setCarDesc((String)rb.getObject("CarDesc"));
  this.setCarBasePrice((String)rb.getObject("CarBasePrice"));
  this.setCarCurrentPrice((String)rb.getObject(
    "CarCurrentPrice"));
  loadOptions();
} 

This method loads the localized data for the chosen car from the ResourceBundle associated with the car by calling ResourceBundle.getBundle, passing in the path to the resource file and the current locale, which is retrieved from the FacesContext. This method then calls the appropriate setter methods of the CurrentOptionServer, passing the locale-specific object representing the localized data associated with the given key.

The localized data for the UISelectOne components is loaded with the loadOptions method, which is called when the CurrentOptionServer is initialized and at the end of the setCarId(int) method. Here is a piece of the loadOptions method:

public void loadOptions() {
  ResourceBundle rb =
    ResourceBundle.getBundle("cardemo/Resources",
    (FacesContext.getCurrentInstance().getLocale()));
  brakes = new String[2];
  brakes[0] = (String)rb.getObject("Disc");
  brakes[1] = (String)rb.getObject("Drum");
  ...
  brakeOption = new ArrayList(brakes.length);
  ...
  for (i = 0; i < brakes.length; i++) {
    brakeOption.add(new SelectItem(brakes[i], brakes[i],
      brakes[i]));
}  

Just like in setCarId(int), the loadOptions method loads the localized data from the ResourceBundle. As shown in the code snippet, the localized data for the brakes component is loaded into an array. This array is used to create a Collection of SelectItem instances.

Localizing Messages

The JavaServer Faces API provides a set of classes for associating a set of localized messages with a component. The Message class corresponds to a single message. A set of Message instances compose a MessageResources, which is analogous to a ResourceBundle. A MessageResourceFactory creates and returns MessageResources instances.

MessageResources instances will most commonly comprise a list of validation error messages. Performing Validation includes an example of registering and using a MessageResources for validation error messages.

To make a MessageResources bundle available to an application, you need to register the MessageResources instance with the application. This is explained in Register the Error Messages.

After registering the MessageResources, you can access the messages from your application (as explained in Implement the Validator Interface) by:

  1. Calling the getMessageResources(String) method, passing in the MessageResources identifier
  2. Calling getMessage on the MessageResources instance, passing in the FacesContext, the message identifier, and the substitution parameters. The substitution parameters are usually used to embed the Validator properties' values in the message. For example, the custom validator described in Implement the Validator Interface will substitute the format pattern for the {0} in this error message:
  3. Input must match one of the following patterns {0} 
    
Divider
Download
FAQ
History
PrevHomeNext 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.