background image

Performing Localization

<< Properties Bound to Converters | Localizing Messages >>
<< Properties Bound to Converters | Localizing Messages >>

Performing Localization

Performing Localization
As mentioned in
"Rendering Components for Selecting Multiple Values" on page 345
, data and
messages in the Duke's Bookstore application have been localized for French, German, Spanish,
and American English.
This section explains how to produce the localized error messages as well as how to localize
dynamic data and messages.
"Rendering Components for Selecting Multiple Values" on page 345
describes how page
authors access localized data from the page.
If you are not familiar with the basics of localizing web applications, see
Chapter 15,
"Internationalizing and Localizing Web Applications."
Creating a Resource Bundle
A ResourceBundle contains a set of localized messages. To learn how to create a
ResourceBundle
, see
http://java.sun.com/docs/books/tutorial/i18n/index.html
.
After 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 a ResourceBundle called
BookstoreMessages,
located in tut-install/javaeetutorial5/examples/web/bookstore/
src/com/sun/bookstore/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 in
tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/backing/AbstractBean.java
is a general-purpose method that looks up localized data 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) {
Performing Localization
The Java EE 5 Tutorial · September 2007
390