Download
FAQ History |
API
Search Feedback |
Performing Localization
As explained in Using Localized Messages, all data and messages in the Duke's Bookstore application have been completely localized for French, German, Spanish, and American English.
This section explains how to produce the localized messages as well as how to localize dynamic data and messages.
Using Localized Messages describes how page authors access localized data from the page using the
loadBundle
tag.If you are not familiar with the basics of localizing Web applications, see Chapter 22.
Creating a Resource Bundle
A
ResourceBundle
contains a set of localized messages. To learn how to create aResourceBundle
, seeAfter you create the
ResourceBundle
, put it in the same directory as your classes. Much of the data for the Duke's Bookstore application is stored in aResourceBundle
calledBookstoreMessages,
located in<INSTALL>
/j2eetutorial14/examples/web/bookstore/src/messages/
.Localizing Dynamic Data
The Duke's Bookstore application has some data that is set dynamically in backing beans. Because of this, the beans must load the localized data themselves; the data can't be loaded from the page.
The
message
method inbookstore6/src/backing/AbstractBean
is a general-purpose method that looks up localized messages used in the backing beans:protected void message(String clientId, String key) { // Look up the requested message text String text = null; try { ResourceBundle bundle = ResourceBundle.getBundle("messages.BookstoreMessages", context().getViewRoot().getLocale()); text = bundle.getString(key); } catch (Exception e) { text = "???" + key + "???"; } // Construct and add a FacesMessage containing it context().addMessage(clientId, new FacesMessage(text)); }This method gets the current locale from the
UIViewRoot
of the current request and loads the localized data for the messages using thegetBundle
method, passing in the path to theResourceBundle
and the current locale.The other backing beans call this method by using the key to the message that they are trying to retrieve from the resource bundle. Here is a call to the
message
method frombookstore6/src/backing/ShowCartBean
:Localizing Messages
The JavaServer Faces API provides two ways to create messages from a
ResourceBundle
:
- You can register the
ResourceBundle
with the application configuration resource file and use a message factory pattern to examine theResouceBundle
and to generate localizedFacesMessage
instances, which represent single localized messages. The message factory pattern is required to access messages that are registered with theApplication
instance. Instead of writing your own message factory pattern, you can use the one included with the Duke's Bookstore application. It is calledMessageFactory
and is located in<INSTALL>
/j2eetutorial14/examples/web/bookstore6/src/util/
.- You can use the
FacesMessage
class, which represents a single localized message, to get the localized string directly from theResourceBundle
.Registering Messages includes an example of registering a
ResourceBundle
in the application configuration resource file.Creating a Message with a Message Factory
To use a message factory to create a message, follow these steps:
- Register the
ResourceBundle
with the application. This is explained in Registering Messages.- Create a message factory implementation. You can simply copy the
MessageFactory
class included with the Duke's Bookstore application to your application.- Access a message from your application by calling the
getMessage(FacesContext, String, Object)
of theMessageFactory
class. TheMessageFactory
class uses theFacesContext
to access theApplication
instance on which the messages are registered. TheString
argument is the key that corresponds to the message in theResourceBundle
. TheObject
typically contains the substitution parameters that are embedded 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:
Input must match one of the following patterns {0}
Implement the Validator Interface gives an example of accessing messages.
Using FacesMessage to Create a Message
Instead of registering messages in the application configuration resource file, you can access the
ResourceBundle
directly from the code. ThevalidateEmail
method from the Coffee Break example does this:... String message = ""; ... message = CoffeeBreakBean.loadErrorMessage(context, CoffeeBreakBean.CB_RESOURCE_BUNDLE_NAME, "EMailError"); context.addMessage(toValidate.getClientId(context), new FacesMessage(message)); ...These lines also call the
loadErrorMessage
to get the message from theResourceBundle
. Here is theloadErrorMessage
method fromCoffeeBreakBean
:
Download
FAQ History |
API
Search Feedback |
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.