Book: JavaServer Pages™, 2nd Edition
Section: Chapter 22.  Integrating Custom Code with JSTL



22.5 Integrating Custom I18N Actions

The default locale, resource bundle, and time zone for the JSTL I18N actions can be specified through four configuration settings, described in Tables Table 22-1 through Table 22-4.

Table 22-1. Locale configuration setting

Variable name:

javax.servlet.jsp.jstl.fmt.locale

Java constant:

Config.FMT_LOCALE

Java type:

String or java.util.Locale

Set by:

<fmt:setLocale>, context parameter, or custom code

Used by:

<fmt:bundle>, <fmt:setBundle>, <fmt:message>, <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate>

Table 22-2. Fallback locale configuration setting

Variable name:

javax.servlet.jsp.jstl.fmt.fallbackLocale

Java constant:

Config.FMT_FALLBACK_LOCALE

Java type:

String or java.util.Locale

Setby:

Context parameter or custom code

Used by:

<fmt:bundle>, <fmt:setBundle>, <fmt:message>, <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate>

Table 22-3. Localization context configuration setting

Variable name:

javax.servlet.jsp.jstl.fmt.localizationContext

Java constant:

Config.FMT_LOCALIZATION_CONTEXT

Java type:

String or javax.servlet.jsp.jstl.fmt.LocalizationContext

Set by:

<fmt:setBundle>, context parameter, or custom code

Used by:

<fmt:message>, <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate>

Table 22-4. Time zone configuration setting

Variable name:

javax.servlet.jsp.jstl.fmt.timeZone

Java constant:

Config.FMT_TIME_ZONE

Java type:

String or java.util.TimeZone

Set by:

<fmt:setTimeZone>, context parameter, or custom code

Used by:

<fmt:formatDate> and <fmt:parseDate>

Setting the locale variable disables the lookup of a locale based on user preferences (passed through the Accept-Language header value), while setting the fallback locale variable preserves this feature and provides a default value that is used only if none of the preferred locales are available. When String values are used to set these two variables, they must be specified as a two-letter lowercase ISO-639 language code, optionally followed by a two-letter uppercase ISO-3166 country code, separated by a hyphen or an underscore character.

The localization context variable can be set to a String value containing the name of the default resource bundle base name. The formatting actions then locate the locale-specific version of this bundle. The <fmt:setBundle> sets the variable to an instance of the LocalizationContext class, which contains references to both a locale and a resource bundle for a locale.

String values for the time-zone setting must be of the type defined for the java.util.TimeZone class: an abbreviation, a full name, or a GMT offset.

The configuration variables can be set by the calling the Config.set( ) methods, as described earlier in this chapter. It can be used to simplify the JSP pages in an application with a Controller implemented in Java and JSP only for the View part. For instance, the Controller may pick up the preferred locale and time zone from a database when a user logs in and sets the corresponding configuration settings.

If you develop custom actions that need to produce localized text, you can piggyback on the locale-lookup logic provided by JSTL by using the javax.servlet.jsp.jstl.fmt.LocaleSupport class. It provides the following methods:

public static String getLocalizedMessage(PageContext pc, String key)
public static String getLocalizedMessage(PageContext pc, String key,
    String basename)
  
public static String getLocalizedMessage(PageContext pc, String key,
    Object[] args)
public static String getLocalizedMessage(PageContext pc, String key,
    Object[] args, String basename)

There are two sets of methods. The first is for simple messages and the second for parameterized messages. In each set, one method has a basename parameter and one doesn't. The versions without the basename parameter rely on the localization context configuration setting to find the resource bundle to get the message from, and the others locate the locale-specific resource bundle for the specified base name. Both sets return the localized message for the specified key.

    [http://safari.oreilly.com/059600317X/jserverpages2-CHP-22-SECT-5]


    Copyright © 2002 O'Reilly & Associates, Inc. All rights reserved.
    1005 Gravenstein Highway North
    Sebastopol, CA 95472