|
Download
FAQ History |
|
API
Search Feedback |
Performing Localization
For this release, all data and messages in the
cardemoapplication 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 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:
- After you declare the
html_basicandjsf-coretag libraries in your JavaServer Faces page, add a declaration for the JSTLfmttag library:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt" %>- Create a
Propertiesfile containing the localized messages.- Add an
fmt:setBundletag:
<fmt:setBundle
basename="cardemo.Resources"
scope="session" var="cardemoBundle"/>The
basenameattribute value refers to thePropertiesfile, located in thecardemopackage. Make sure thebasenameattribute specifies the fully qualified class name of your Resources file. This file contains the localized messages.The
scopeattribute indicates the scope--either application, session, or page--for which this bundle can be used.The
varattribute is an alias to theResourcesfile. This alias can be used by other tags in the page in order to access the localized messages.Add a
keyattribute to a component tag to access the particular localized message and add thebundleattribute to refer to the file containing the localized message. Thebundleattribute must exactly match thevarattribute in thefmt:setBundletag. Here is an example frommore.jsp:<h:output_text
key="OptionsPackages" bundle="carDemoBundle" />For more information on using the JSTL Internationalization functionality, please refer to Internationalization Tags.
Localizing Dynamic Data
The
cardemoapplication 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
UISelectOnecomponent. Another example is the car description that appears on themore.jsppage. This description corresponds to the car the user chose from theStorefront.jsppage. Since the chosen car is not known to the application prior to startup time, the localized description cannot be loaded from the page. Instead, theCurrentOptionServerbean must load the localized car description.In the
CurrentOptionServerbean, the localized car title and description is loaded with thesetCarId(int)method, which is called when the user selects a car fromStorefront.jsp. Here is a piece of thesetCarId(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
ResourceBundleassociated with the car by callingResourceBundle.getBundle, passing in the path to the resource file and the current locale, which is retrieved from theFacesContext. This method then calls the appropriate setter methods of theCurrentOptionServer, passing the locale-specific object representing the localized data associated with the given key.The localized data for the
UISelectOnecomponents is loaded with theloadOptionsmethod, which is called when theCurrentOptionServeris initialized and at the end of thesetCarId(int)method. Here is a piece of theloadOptionsmethod: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), theloadOptionsmethod loads the localized data from theResourceBundle. As shown in the code snippet, the localized data for thebrakescomponent 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
Messageclass corresponds to a single message. A set ofMessageinstances compose aMessageResources, which is analogous to aResourceBundle. AMessageResourceFactorycreates and returnsMessageResourcesinstances.
MessageResourcesinstances will most commonly comprise a list of validation error messages. Performing Validation includes an example of registering and using aMessageResourcesfor validation error messages.To make a
MessageResourcesbundle available to an application, you need to register theMessageResourcesinstance 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:
- Calling the
getMessageResources(String)method, passing in theMessageResourcesidentifier- Calling
getMessageon theMessageResourcesinstance, passing in theFacesContext, the message identifier, and the substitution parameters. The substitution parameters are usually used to embed theValidatorproperties' 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:
|
Download
FAQ History |
|
API
Search Feedback |
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.