Java Reference
In-Depth Information
Setting the Resource Bundle
The resource bundle is set with the
resource-bundle
element in the configuration
file. The setting for Duke's Tutoring looks like this:
<resource-bundle>
<base-name>dukestutoring.web.messages.Messages</base-name>
<var>bundle</var>
</resource-bundle>
After the locale is set, the controller of a web application could retrieve the resource
bundle for that locale and save it as a session attribute (see “Associating Objects with
a Session” in
The Java EE 6 Tutorial: Basic Concepts
) for use by other components or
simply to return a text string appropriate for the selected locale:
public String toString(Locale locale) {
ResourceBundle res = ResourceBundle.getBundle(
"dukestutoring.web.messages.Messages", locale);
return res.getString(name() + ".string");
}
Alternatively, an application could use the
f:loadBundle
tag to set the resource
bundle. This tag loads the correct resource bundle according to the locale stored in
FacesContext
.
<f:loadBundle basename="dukestutoring.web.messages.Messages"
var="bundle"/>
Resource bundles containing messages that are explicitly referenced from a JavaServer
Faces tag attribute using a value expression must be registered using the
resource-
bundle
element of the configuration file.
For more information on using this element, see “
Registering Application Messages
”
on
page
155
.
Retrieving Localized Messages
A web component written in the Java programming language retrieves the resource bundle
from the session: