So far in this chapter, all of the code examples have used <fmt:setLocale> to specify the locale used by the <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate> actions. But in practice, it's usually not necessary to use <fmt:setLocale> to establish a formatting locale because the formatting actions perform a rather elaborate search for a locale. This section discusses that search. Before we discuss the search for a formatting locale, you must understand the concept of a localization context. You can read about localization contexts in "Localization Context Lookup" on page 268, but in a nutshell , a localization context is a simple JavaBeans component (bean) that maintains a resource bundle and a locale. For our purposes in this chapter, the resource bundle is immaterial, but the locale stored in a localization context is often used by formatting actions. The search that formatting actions perform to locate a formatting locale proceeds as follows :
Let's discuss each of the preceding steps in more detail. 1 An Enclosing <fmt:bundle> ActionFormatting actions that are nested in a <fmt:bundle> action use the locale stored in that <fmt:bundle> action's localization context; for example: <%-- The following <fmt:bundle> action establishes a localization context that is only used in the body of the <fmt:bundle> action --%> <fmt:bundle basename='messages'> <%-- The i18n and formatting actions nested in the enclosing <fmt:bundle> action use the localization context established by the enclosing <fmt:bundle> action --%> <fmt:message key='formatting.example.number'/> ... <fmt:formatNumber value='234682.155'/> ... </fmt:bundle> In the preceding code fragment, the <fmt:bundle> action tries to locate a resource bundle whose base name is messages ; if it finds that resource bundle, it creates a localization context and stores the resource bundle and the locale that was used to locate that resource bundle in its localization context. [17] All of the <fmt:message> actions and all of the formatting actions nested in that <fmt:bundle> action use the same localization context; for example, in the preceding code fragment, the <fmt:message> action uses the resource bundle stored in the <fmt:bundle> action's localization context, and the <fmt:formatNumber> action uses the locale stored in the <fmt:bundle> action's localization context.
2 The FMT_LOCALIZATION_CONTEXT Configuration SettingIf a formatting action is not nested in a <fmt:bundle> action and the FMT_LOCALIZATION_CONTEXT configuration setting has been set, that formatting action uses the locale stored in the FMT_LOCALIZATION_CONTEXT configuration setting's localization context; for example: <%-- This <fmt:setBundle> action establishes a localization context and stores it in the FMT_LOCALIZATION_CONTEXT configuration setting --%> <fmt:setBundle basename='messages'/> <%-- Because the following <fmt:formatNumber> action is not nested in a <fmt:bundle> action, it gets its locale from the localization context established by the preceding <fmt:setBundle> action --%> <fmt:formatNumber value='234682.155'/> In the preceding code fragment, the FMT_LOCALIZATION_CONTEXT configuration setting is set by the <fmt:setBundle> action. [18] The locale stored in the FMT_LOCALIZATION_CONTEXT configuration setting is used by the <fmt:formatNumber> action.
If the localization context stored in the FMT_LOCALIZATION_CONTEXT configuration setting does not have a locale, formatting actions that do not reside in the body of a <fmt:bundle> action perform a formatting locale lookup, which is described in the next step. 3 Formatting Locale LookupIf a formatting action is not nested in a <fmt:bundle> action and the FMT_LOCALIZATION_CONTEXT configuration setting has not been set, that formatting action performs a formatting locale lookup; for example: <%-- If the FMT_LOCALIZATION_CONTEXT configuration setting has not been set, the following <fmt:formatNumber> action performs a formatting locale lookup to find a locale to use to format its value --%> <fmt:formatNumber value='234682.155'/> In the preceding code fragment, the <fmt:formatNumber> action is not nested in a <fmt:bundle> action. If the FMT_LOCALIZATION_CONTEXT configuration setting has not been set, that <fmt:formatNumber> action performs a formatting locale lookup, which is discussed in the next section. Formatting Locale LookupFormatting actions that are not nested in a <fmt:bundle> action perform a formatting locale lookup if the FMT_LOCALIZATION_CONTEXT configuration setting has not been set or if that configuration setting does not contain a locale. The formatting locale lookup tries to find an appropriate locale among a set of available locales. For <fmt:formatNumber> and <fmt:parseNumber>, the available locales are determined by a call to the getAvailableLocales method from java.text.NumberFormat . For <fmt:formatDate> and <fmt:parseDate>, the available locales are determined by a call to the getAvailableLocales method from java.text.DateFormat . The formatting locale lookup proceeds as follows:
A preferred locale (or the fallback locale) matches an available locale if:
|