background image

Setting the Resource Bundle

<< Providing Localized Messages and Labels | Retrieving Localized Messages >>
<< Providing Localized Messages and Labels | Retrieving Localized Messages >>

Setting the Resource Bundle

</h:commandLink>
public void chooseLocaleFromLink(ActionEvent event) {
String current = event.getComponent().getId();
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale((Locale)
locales.get(current));
}
Setting the Resource Bundle
After the locale is set, the controller of a web application typically retrieves the resource bundle
for that locale and saves it as a session attribute (see
"Associating Objects with a Session" on
page 126
) for use by other components:
messages = ResourceBundle.getBundle(
"com.sun.bookstore.messages.BookstoreMessages",
locale);
session.setAttribute(
"messages", messages);
The resource bundle base name for the JSTL versions of Duke's Bookstore is set at deployment
time through a context parameter. When a session is initiated, the resource bundle for the user's
locale is stored in the localization context. It is also possible to override the resource bundle at
runtime for a given scope using the fmt:setBundle tag and for a tag body using the fmt:bundle
tag.
The JavaServer Faces version of Duke's Bookstore uses two methods for setting the resource
bundle. One method is letting the JSP pages set the resource bundle using the f:loadBundle
tag. This tag loads the correct resource bundle according to the locale stored in FacesContext.
<f:loadBundle basename=
"messages.BookstoreMessages"
var=
"bundle"/>
For information on this tag, see
"Loading a Resource Bundle" on page 351
.
Another way a JavaServer Faces application sets the resource bundle is by configuring it in the
application configuration file. There are two XML elements that you can use to set the resource
bundle: message-bundle and resource-bundle.
If the error messages are queued onto a component as a result of a converter or validator being
registered on the component, then these messages are automatically displayed on the page
using the message or messages tag. These messages must be registered with the application
using the message-bundle tag:
<message-bundle>
resources.ApplicationMessages
</message-bundle>
Providing Localized Messages and Labels
Chapter 15 · Internationalizing and Localizing Web Applications
469