Package org.apache.struts.util
Class PropertyMessageResources
java.lang.Object
org.apache.struts.util.MessageResources
org.apache.struts.util.PropertyMessageResources
- All Implemented Interfaces:
Serializable
Concrete subclass of
MessageResources
that reads message keys
and corresponding strings from named property resources in a similar manner
(see modes below) that java.util.PropertyResourceBundle
does. The
base
property defines the base property resource name, and
must be specified. IMPLEMENTATION NOTE - This class trades memory for speed by caching all messages located via generalizing the Locale under the original locale as well. This results in specific messages being stored in the message cache more than once, but improves response time on subsequent requests for the same locale + key combination.
Operating Modes
This implementation can be configured to operate in one of three modes:- 1. Default - default, backwardly compatible, Struts behaviour (i.e. the way its always worked).
- 2. JSTL - compatible with how JSTL finds messages (fix for STR-2925)
- 3. Resource - compatible with how Java's
PropertyResourceBundle
finds messages (fix for STR-2077)
1. Default Mode
Default mode is the way this implementation has always operated. It searches for a message key for property resources in the following sequence:base + "_" + localeLanguage + "_" + localeCountry + "_" + localeVariant base + "_" + localeLanguage + "_" + localeCountry base + "_" + localeLanguage base + "_" + default locale base
This mode is the default and requires no additional configuration.
2. JSTL Mode
JSTL mode is compatible with how JSTL operates and the default Locale is not used when looking for a message key. JSTL mode searches for a message key for property resources in the following sequence:base + "_" + localeLanguage + "_" + localeCountry + "_" + localeVariant base + "_" + localeLanguage + "_" + localeCountry base + "_" + localeLanguage base
Configure PropertyMessageResources
to operate in this mode by
specifying a value of JSTL
for the mode
key in your struts-config.xml
:
<message-resources parameter="mypackage.MyMessageResources"> <set-property key="mode" value="JSTL"/> </message-resources>
3. Resource Mode
Resource mode is compatible with how Java'sPropertyResourceBundle
operates. Resource mode searches first through the specified Locale's language,
country and variant, then through the default Locale's language,
country and variant and finally using just the base
:
base + "_" + localeLanguage + "_" + localeCountry + "_" + localeVariant base + "_" + localeLanguage + "_" + localeCountry base + "_" + localeLanguage base + "_" + defaultLanguage + "_" + defaultCountry + "_" + defaultVariant base + "_" + defaultLanguage + "_" + defaultCountry base + "_" + defaultLanguage base
Configure PropertyMessageResources
to operate in this mode by
specifying a value of resource
for the mode
key in your struts-config.xml
:
<message-resources parameter="mypackage.MyMessageResources"> <set-property key="mode" value="resource"/> </message-resources>
- Version:
- $Rev$ $Date$
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionThe set of locale keys for which we have already loaded messages, keyed by the value calculated inlocaleKey()
.The cache of messages we have accumulated over time, keyed by the value calculated inmessageKey()
.Fields inherited from class org.apache.struts.util.MessageResources
config, defaultFactory, defaultLocale, factory, formats, returnNull
-
Constructor Summary
ConstructorDescriptionPropertyMessageResources
(MessageResourcesFactory factory, String config) Construct a new PropertyMessageResources according to the specified parameters.PropertyMessageResources
(MessageResourcesFactory factory, String config, boolean returnNull) Construct a new PropertyMessageResources according to the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptiongetMessage
(Locale locale, String key) Returns a text message for the specified key, for the specified or default Locale.protected void
loadLocale
(String localeKey) Load the messages associated with the specified Locale key.void
Set the compatibility mode this implementation uses for message lookup.Methods inherited from class org.apache.struts.util.MessageResources
escape, getConfig, getFactory, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessageResources, getReturnNull, isEscape, isPresent, isPresent, localeKey, log, log, messageKey, messageKey, setEscape, setReturnNull
-
Field Details
-
locales
The set of locale keys for which we have already loaded messages, keyed by the value calculated inlocaleKey()
. -
messages
The cache of messages we have accumulated over time, keyed by the value calculated inmessageKey()
.
-
-
Constructor Details
-
PropertyMessageResources
Construct a new PropertyMessageResources according to the specified parameters.- Parameters:
factory
- The MessageResourcesFactory that created usconfig
- The configuration parameter for this MessageResources
-
PropertyMessageResources
Construct a new PropertyMessageResources according to the specified parameters.- Parameters:
factory
- The MessageResourcesFactory that created usconfig
- The configuration parameter for this MessageResourcesreturnNull
- The returnNull property we should initialize with
-
-
Method Details
-
setMode
Set the compatibility mode this implementation uses for message lookup.- Parameters:
mode
-JSTL
for JSTL compatibility,resource
for PropertyResourceBundle compatibility ordefault
for Struts backward compatibility.
-
getMessage
Returns a text message for the specified key, for the specified or default Locale. A null string result will be returned by this method if no relevant message resource is found for this key or Locale, if thereturnNull
property is set. Otherwise, an appropriate error message will be returned.This method must be implemented by a concrete subclass.
- Specified by:
getMessage
in classMessageResources
- Parameters:
locale
- The requested message Locale, ornull
for the system default Localekey
- The message key to look up- Returns:
- text message for the specified key and locale
-
loadLocale
Load the messages associated with the specified Locale key. For this implementation, theconfig
property should contain a fully qualified package and resource name, separated by periods, of a series of property resources to be loaded from the class loader that created this PropertyMessageResources instance. This is exactly the same name format you would use when utilizing thejava.util.PropertyResourceBundle
class.- Parameters:
localeKey
- Locale key for the messages to be retrieved
-