Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
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 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_basic
andjsf-core
tag libraries in your JavaServer Faces page, add a declaration for the JSTLfmt
tag library:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt" %>- Create a
Properties
file containing the localized messages.- Add an
fmt:setBundle
tag:
<fmt:setBundle
basename="cardemo.Resources"
scope="session" var="cardemoBundle"/>The
basename
attribute value refers to theProperties
file, located in thecardemo
package. Make sure thebasename
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 theResources
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 thebundle
attribute to refer to the file containing the localized message. Thebundle
attribute must exactly match thevar
attribute in thefmt:setBundle
tag. 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
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 themore.jsp
page. This description corresponds to the car the user chose from theStorefront.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, theCurrentOptionServer
bean must load the localized car description.In the
CurrentOptionServer
bean, 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
ResourceBundle
associated 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
UISelectOne
components is loaded with theloadOptions
method, which is called when theCurrentOptionServer
is initialized and at the end of thesetCarId(int)
method. Here is a piece of theloadOptions
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)
, theloadOptions
method loads the localized data from theResourceBundle
. As shown in the code snippet, the localized data for thebrakes
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 ofMessage
instances compose aMessageResources
, which is analogous to aResourceBundle
. AMessageResourceFactory
creates and returnsMessageResources
instances.
MessageResources
instances will most commonly comprise a list of validation error messages. Performing Validation includes an example of registering and using aMessageResources
for validation error messages.To make a
MessageResources
bundle available to an application, you need to register theMessageResources
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:
- Calling the
getMessageResources(String)
method, passing in theMessageResources
identifier- Calling
getMessage
on theMessageResources
instance, passing in theFacesContext
, the message identifier, and the substitution parameters. The substitution parameters are usually used to embed theValidator
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:
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.