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.

Providing Localized Messages and Labels<br />

Providing Localized Messages and Labels<br />

466<br />

Messages and labels should be tailored according to the conventions of a user’s language and<br />

region. <strong>The</strong>re are two approaches to providing localized messages and labels in a web<br />

application:<br />

■ Provide a version of the JSP page in each of the target locales and have a controller servlet<br />

dispatch the request to the appropriate page depending on the requested locale. This<br />

approach is useful if large amounts of data on a page or an entire web application need to be<br />

internationalized.<br />

■ Isolate any locale-sensitive data on a page into resource bundles, and access the data so that<br />

the corresponding translated message is fetched automatically and inserted into the page.<br />

Thus, instead of creating strings directly in your code, you create a resource bundle that<br />

contains translations and read the translations from that bundle using the corresponding<br />

key.<br />

<strong>The</strong> Duke’s Bookstore applications follow the second approach. Here are a few lines from the<br />

default resource bundle messages.BookstoreMessages.java:<br />

{"TitleCashier", "Cashier"},<br />

{"TitleBookDescription", "Book Description"},<br />

{"Visitor", "You are visitor number "},<br />

{"What", "What We’re Reading"},<br />

{"Talk", " talks about how Web components can transform the way you develop<br />

applications for the Web. This is a must read for any self respecting Web developer!"},<br />

{"Start", "Start Shopping"},<br />

Establishing the Locale<br />

To get the correct strings for a given user, a web application either retrieves the locale (set by a<br />

browser language preference) from the request using the getLocale method, or allows the user<br />

to explicitly select the locale.<br />

<strong>The</strong> JSTL versions of Duke’s Bookstore automatically retrieve the locale from the request and<br />

store it in a localization context (see “Internationalization Tag Library” on page 213). It is also<br />

possible for a component to explicitly set the locale by using the fmt:setLocale tag.<br />

<strong>The</strong> <strong>Java</strong>Server Faces version of Duke’s Bookstore allows the user to explicitly select the locale.<br />

<strong>The</strong> user selection triggers a method that stores the locale in the FacesContext object. <strong>The</strong><br />

locale is then used in resource bundle selection and is available for localizing dynamic data and<br />

messages (see “Localizing Dynamic Data” on page 388):<br />

<br />

<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!