Java Reference
In-Depth Information
Establishing the Locale
To get the correct strings for a given user, a web application either retrieves the locale (set
by a browser language preference) from the request using the
getLocale
method, or
allows the user to explicitly select the locale.
A component can explicitly set the locale by using the
fmt:setLocale
tag.
The
locale-config
element in the configuration file registers the default locale and
other supported locales. This element in Duke's Tutoring registers English as the default
locale and indicates that German, Spanish, Portuguese, and Chinese are supported locales.
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
<supported-locale>es</supported-locale>
<supported-locale>pt</supported-locale>
<supported-locale>zh</supported-locale>
</locale-config>
The Status Manager in the Duke's Tutoring application uses the
getLocale
method to
retrieve the locale and a
toString
method to return a localized translation of a student's
status based on the locale.
public class StatusManager {
private FacesContext ctx = FacesContext.getCurrentInstance();
private Locale locale;
/** Creates a new instance of StatusManager */
public StatusManager() {
locale = ctx.getViewRoot().getLocale();
}
public String getLocalizedStatus(StatusType status) {
return status.toString(locale);
}
}