background image

Loading a Resource Bundle

<< Using Localized Data | Referencing Localized Static Data >>
<< Using Localized Data | Referencing Localized Static Data >>

Loading a Resource Bundle

localize dynamic data. If you are not familiar with the basics of localizing web applications, see
Chapter 15, "Internationalizing and Localizing Web Applications."
All data in the Duke's Bookstore application have been localized for Spanish, French, German,
and American English. The image map on the first page allows you to select your preferred
locale. See
Chapter 13, "Creating Custom UI Components"
for information on how the image
map custom component was created.
All the localized data is stored in resource bundles, which are represented as either
ResourceBundle
classes or text files, usually with the extension .properties. For more
information about resource bundles, see
http://java.sun.com/docs/books/tutorial/i18n/index.html
.
After the application developer has produced a resource bundle, the application architect puts it
in the same directory as the application classes. The static text data for the Duke's Bookstore
application is stored in a ResourceBundle class called BookstoreMessages. The error messages
are stored in another resource bundle called ApplicationMessages. After the resource bundles
have been created and before their data can be accessed, they must be made available to the
application, as explained in the following section.
Loading a Resource Bundle
To reference error messages or static data from the page, you first need to make available the
resource bundle containing the data.
To make available resource bundles that contain static data, you need to do one of two things:
Register the resource bundle with the application in the configuration file using the
resource-bundle
element, as explained in
"Registering Custom Localized Static Text" on
page 449
.
Load the resource bundle into the current view using the loadBundle tag.
Here is an example loadBundle tag from bookstore.jsp:
<f:loadBundle var=
"bundle"
basename=
"messages.BookstoreMessages" />
The basename attribute value specifies the fully-qualified class name of the ResourceBundle
class, which in this case is located in the messages package of the bookstore application.
The var attribute is an alias to the ResourceBundle class. This alias can be used by other tags in
the page in order to access the localized messages.
In the case of resource bundles that contain error messages, you need to register the resource
bundle with the application in the configuration file using the message-bundle element, as
explained in
"Registering Custom Error Messages" on page 448
. One exception is if you are
Using Localized Data
Chapter 11 · Using JavaServer Faces Technology in JSP Pages
351