User's Manual

166 Chapter 13. Web Applications Tutorial
Note
If an application built on WAF relies on a service provided by another package, that package must
also be globalized and must support any languages the WAF application is intended to support.
For example, if your application relies on the places service to relate and/or present geographical
data, you must ensure that this service supports all the Locales that you wish your application to
support.
You can retrieve the Locale for the current request by calling the getLocale() method of the
current RequestContext. For example:
java.util.Locale locale =
DispatcherHelper.getRequestContext(request).getLocale();
System.out.println("The locale for the current request is: "
+ locale.toString());
Another option, character set encoding negotiation (charset negotiation) is more complicated than
Locale negotiation.
13.3. ResourceBundles and MessageCatalogs
ResourceBundles
1
are Java classes for grouping localizable resources. The class also provides
methods for finding the appropriate ResourceBundle based on a Locale and for retrieving resources
from it by performing a lookup based on a key.
A ResourceBundle has a base name and optionally an associated Locale. A base name is a names-
pace used to group resources. A ResourceBundle without an associated Locale is the default Re-
sourceBundle for that base name. If a ResourceBundle for a particular Locale hierarchy is not
found, the ResourceBundle with no associated Locale is used. These are used to store resources
that are common across all Locale instances.
The Locale hierarchy is simply the mechanism that is used to fall back from very specific Locales
to more generic ones. For example, from en_US_WIN95, the hierarchy falls back to en_US and then
to en and finally to the empty Locale.
Typically, one base name is used per application. That is, all resources for an application or service are
grouped in the same ResourceBundle. In cases where this is not convenient, different base names
can be used.
An example of the normal use of ResourceBundles is the notes application. The base name
it uses is com.arsdigita.notes.NotesResources. It groups all its localizable resources
into that ResourceBundle. On the other hand, the places service uses multiple base names,
since it maintains a large amount of data, which may not be needed by all developers. For
example, a developer might just want to translate the names of countries into a particular
language, and not require any other place data. Therefore, the places service divides its resources
into different base names, including com.arsdigita.places.CountriesResources,
com.arsdigita.places.USStatesResources and so on.
It is recommended that you use PropertyResourceBundle
2
to store static resources. Static re-
sources remain the same for the lifetime of the running server. This includes strings on WUI elements
(such as form labels, page titles, and error messages) and names of objects that do not change fre-
quently (such as country and city names).
1. http://java.sun.com/j2se/1.3/docs/api/java/util/ResourceBundle.html
2. http://java.sun.com/j2se/1.3/docs/api/java/util/PropertyResourceBundle.html