10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Performing Localization<br />

Performing Localization<br />

388<br />

As mentioned in “Rendering Components for Selecting Multiple Values” on page 343, data and<br />

messages in the Duke’s Bookstore application have been localized for French, German, Spanish,<br />

and American English.<br />

This section explains how to produce the localized error messages as well as how to localize<br />

dynamic data and messages.<br />

“Rendering Components for Selecting Multiple Values” on page 343 describes how page<br />

authors access localized data from the page.<br />

If you are not familiar with the basics of localizing web applications, see Chapter 15,<br />

“Internationalizing and Localizing Web Applications.”<br />

Creating a Resource Bundle<br />

A ResourceBundle contains a set of localized messages. To learn how to create a<br />

ResourceBundle, see http://java.sun.com/docs/books/tutorial/i18n/index.html.<br />

After you create the ResourceBundle, put it in the same directory as your classes. Much of the<br />

data for the Duke’s Bookstore application is stored in a ResourceBundle called<br />

BookstoreMessages, located in tut-install/javaeetutorial5/examples/web/bookstore/<br />

src/com/sun/bookstore/messages/.<br />

Localizing Dynamic Data<br />

<strong>The</strong> Duke’s Bookstore application has some data that is set dynamically in backing beans.<br />

Because of this, the beans must load the localized data themselves; the data can’t be loaded from<br />

the page.<br />

<strong>The</strong> message method in<br />

tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/backing/Abstr<br />

is a general-purpose method that looks up localized data used in the backing beans:<br />

protected void message(String clientId, String key) {<br />

// Look up the requested message text<br />

String text = null;<br />

try {<br />

ResourceBundle bundle =<br />

ResourceBundle.getBundle("messages.BookstoreMessages",<br />

context().getViewRoot().getLocale());<br />

text = bundle.getString(key);<br />

} catch (Exception e) {<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!