public class StrutsLocalizedTextProvider extends Object
Modifier and Type | Field and Description |
---|---|
protected ConcurrentMap<String,ResourceBundle> |
bundlesMap |
protected boolean |
devMode |
protected boolean |
reloadBundles |
protected boolean |
searchDefaultBundlesFirst |
static String |
STRUTS_MESSAGES_BUNDLE |
static String |
XWORK_MESSAGES_BUNDLE |
Constructor and Description |
---|
StrutsLocalizedTextProvider() |
Modifier and Type | Method and Description |
---|---|
void |
addDefaultResourceBundle(String resourceBundleName)
Adds the bundle to the internal list of default bundles.
|
protected MessageFormat |
buildMessageFormat(String pattern,
Locale locale) |
void |
clearBundle(String bundleName)
Deprecated.
A "no-op" since 6.0.0. Use
clearBundle(java.lang.String, java.util.Locale) instead. |
protected void |
clearBundle(String bundleName,
Locale locale)
Clear a specific bundle + locale combination from the
bundlesMap . |
static void |
clearDefaultResourceBundles()
Deprecated.
used only in tests
|
protected void |
clearMissingBundlesCache()
Clears the
missingBundles contents. |
String |
findDefaultText(String aTextName,
Locale locale)
Returns a localized message for the specified key, aTextName.
|
String |
findDefaultText(String aTextName,
Locale locale,
Object[] params)
Returns a localized message for the specified key, aTextName, substituting variables from the
array of params into the message.
|
protected String |
findMessage(Class clazz,
String key,
String indexedKey,
Locale locale,
Object[] args,
Set<String> checked,
ValueStack valueStack)
Traverse up class hierarchy looking for message.
|
ResourceBundle |
findResourceBundle(String aBundleName,
Locale locale)
Finds the given resource bundle by it's name.
|
String |
findText(Class aClass,
String aTextName,
Locale locale)
Calls
findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)
with aTextName as the default message. |
String |
findText(Class aClass,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args)
Finds a localized text message for the given key, aTextName.
|
String |
findText(Class aClass,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args,
ValueStack valueStack)
Finds a localized text message for the given key, aTextName.
|
String |
findText(ResourceBundle bundle,
String aTextName,
Locale locale)
Finds a localized text message for the given key, aTextName, in the specified resource bundle
with aTextName as the default message.
|
String |
findText(ResourceBundle bundle,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args)
Finds a localized text message for the given key, aTextName, in the specified resource
bundle.
|
String |
findText(ResourceBundle bundle,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args,
ValueStack valueStack)
Finds a localized text message for the given key, aTextName, in the specified resource
bundle.
|
protected String |
formatWithNullDetection(MessageFormat mf,
Object[] args) |
protected List<String> |
getCurrentBundleNames() |
protected ClassLoader |
getCurrentThreadContextClassLoader() |
protected com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg |
getDefaultMessage(String key,
Locale locale,
ValueStack valueStack,
Object[] args,
String defaultMessage) |
protected com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg |
getDefaultMessageWithAlternateKey(String key,
String alternateKey,
Locale locale,
ValueStack valueStack,
Object[] args,
String defaultMessage)
A helper method that can be used by descendant classes to perform some common two-stage message lookup operations
against the default resource bundles.
|
protected String |
getMessage(String bundleName,
Locale locale,
String key,
ValueStack valueStack,
Object[] args) |
static Locale |
localeFromString(String localeStr,
Locale defaultLocale)
Deprecated.
please use
LocaleUtils.toLocale(String) |
protected void |
reloadBundles() |
protected void |
reloadBundles(Map<String,Object> context) |
void |
reset()
Deprecated.
used only in tests
|
void |
setCustomI18NResources(String bundles) |
void |
setDelegatedClassLoader(ClassLoader classLoader) |
void |
setDevMode(String devMode) |
void |
setReloadBundles(String reloadBundles) |
void |
setSearchDefaultBundlesFirst(String searchDefaultBundlesFirst)
Set the
searchDefaultBundlesFirst flag state. |
protected boolean |
unableToFindTextForKey(com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg result)
Determines if we found the text in the bundles.
|
public static final String XWORK_MESSAGES_BUNDLE
public static final String STRUTS_MESSAGES_BUNDLE
protected final ConcurrentMap<String,ResourceBundle> bundlesMap
protected boolean devMode
protected boolean reloadBundles
protected boolean searchDefaultBundlesFirst
@Deprecated public static void clearDefaultResourceBundles()
@Deprecated public static Locale localeFromString(String localeStr, Locale defaultLocale)
LocaleUtils.toLocale(String)
Locale
from a String of the form en_US_foo into a Locale
with language "en", country "US" and variant "foo". This will parse the output of
Locale.toString()
.localeStr
- The locale String to parse.defaultLocale
- The locale to use if localeStr is null.public String findText(Class aClass, String aTextName, Locale locale)
findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)
with aTextName as the default message.aClass
- class nameaTextName
- text namelocale
- the localefindText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)
public String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)
Finds a localized text message for the given key, aTextName. Both the key and the message itself is evaluated as required. The following algorithm is used to find the requested message:
searchDefaultBundlesFirst
is true
, look for the message in the default resource bundles first.ModelDriven
Action, then look for message in
the model's class hierarchy (repeat sub-steps listed above).searchDefaultBundlesFirst
was true
).When looking for the message, if the key indexes a collection (e.g. user.phone[0]) and a message for that specific key cannot be found, the general form will also be looked up (i.e. user.phone[*]).
If a message is found, it will also be interpolated. Anything within ${...}
will be treated as an OGNL expression and evaluated as such.
aClass
- the class whose name to use as the start point for the searchaTextName
- the key to find the text message forlocale
- the locale the message should be fordefaultMessage
- the message to be returned if no text message can be found in any
resource bundleargs
- arguments
resource bundlepublic String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack)
Finds a localized text message for the given key, aTextName. Both the key and the message itself is evaluated as required. The following algorithm is used to find the requested message:
searchDefaultBundlesFirst
is true
, look for the message in the default resource bundles first.ModelDriven
Action, then look for message in
the model's class hierarchy (repeat sub-steps listed above).searchDefaultBundlesFirst
was true
).When looking for the message, if the key indexes a collection (e.g. user.phone[0]) and a message for that specific key cannot be found, the general form will also be looked up (i.e. user.phone[*]).
If a message is found, it will also be interpolated. Anything within ${...}
will be treated as an OGNL expression and evaluated as such.
If a message is not found a DEBUG level log warning will be logged.
aClass
- the class whose name to use as the start point for the searchaTextName
- the key to find the text message forlocale
- the locale the message should be fordefaultMessage
- the message to be returned if no text message can be found in any
resource bundleargs
- argumentsvalueStack
- the value stack to use to evaluate expressions instead of the
one in the ActionContext ThreadLocalpublic String findText(ResourceBundle bundle, String aTextName, Locale locale)
Finds a localized text message for the given key, aTextName, in the specified resource bundle with aTextName as the default message.
If a message is found, it will also be interpolated. Anything within ${...}
will be treated as an OGNL expression and evaluated as such.
bundle
- a resource bundle nameaTextName
- text namelocale
- the localefindText(java.util.ResourceBundle, String, java.util.Locale, String, Object[])
public String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args)
Finds a localized text message for the given key, aTextName, in the specified resource bundle.
If a message is found, it will also be interpolated. Anything within ${...}
will be treated as an OGNL expression and evaluated as such.
If a message is not found a WARN log will be logged.
bundle
- the bundleaTextName
- the keylocale
- the localedefaultMessage
- the default message to use if no message was found in the bundleargs
- arguments for the message formatter.public void addDefaultResourceBundle(String resourceBundleName)
addDefaultResourceBundle
in interface LocalizedTextProvider
resourceBundleName
- the name of the bundle to add.protected ClassLoader getCurrentThreadContextClassLoader()
public void setCustomI18NResources(String bundles)
public String findDefaultText(String aTextName, Locale locale)
findDefaultText
in interface LocalizedTextProvider
aTextName
- the message keylocale
- the locale the message should be forpublic String findDefaultText(String aTextName, Locale locale, Object[] params)
findDefaultText
in interface LocalizedTextProvider
aTextName
- the message keylocale
- the locale the message should be forparams
- an array of objects to be substituted into the message textpublic String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack)
Finds a localized text message for the given key, aTextName, in the specified resource bundle.
If a message is found, it will also be interpolated. Anything within ${...}
will be treated as an OGNL expression and evaluated as such.
If a message is not found a WARN log will be logged.
findText
in interface LocalizedTextProvider
bundle
- the bundleaTextName
- the keylocale
- the localedefaultMessage
- the default message to use if no message was found in the bundleargs
- arguments for the message formatter.valueStack
- the OGNL value stack.public void setDelegatedClassLoader(ClassLoader classLoader)
classLoader
- a ClassLoader
to look up the bundle from if none can be found on the current thread's classloaderpublic void clearBundle(String bundleName)
clearBundle(java.lang.String, java.util.Locale)
instead.bundlesMap
.
Warning: This method is now a "no-op". It was ineffective due
to the way the bundlesMap
is used in combination with locale.
Descendants should use the method clearBundle(java.lang.String, java.util.Locale)
instead.bundleName
- The bundle to remove from the bundle mapprotected void clearBundle(String bundleName, Locale locale)
bundlesMap
.
Intended for descendants to use clear a bundle + locale combination.bundleName
- The bundle (combined with locale) to remove from the bundle maplocale
- Provides the locale to combine with the bundle to get the keyprotected void clearMissingBundlesCache()
missingBundles
contents. This allows descendants to
clear the "missing bundles cache" when desired (or needed).
Note: This method may be used when the bundlesMap
state has changed
in such a way that bundles that were previously "missing" may now be available
(e.g. after calling addDefaultResourceBundle(java.lang.String)
when the
AbstractLocalizedTextProvider
has already been used for failed bundle
lookups of a given key, or some transitory state made a bundle lookup fail.protected void reloadBundles()
protected MessageFormat buildMessageFormat(String pattern, Locale locale)
protected String formatWithNullDetection(MessageFormat mf, Object[] args)
public void setReloadBundles(String reloadBundles)
public void setDevMode(String devMode)
public void setSearchDefaultBundlesFirst(String searchDefaultBundlesFirst)
searchDefaultBundlesFirst
flag state. This flag may be used by descendant TextProvider
implementations to determine if default bundles should be searched for messages first (before the standard
flow of the LocalizedTextProvider
implementation the descendant provides).searchDefaultBundlesFirst
- provide String
"true" or "false" to set the flag state accordingly.public ResourceBundle findResourceBundle(String aBundleName, Locale locale)
Will use Thread.currentThread().getContextClassLoader()
as the classloader.
findResourceBundle
in interface LocalizedTextProvider
aBundleName
- the name of the bundle (usually it's FQN classname).locale
- the locale.@Deprecated public void reset()
protected boolean unableToFindTextForKey(com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg result)
result
- the result so farprotected com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg getDefaultMessage(String key, Locale locale, ValueStack valueStack, Object[] args, String defaultMessage)
protected com.opensymphony.xwork2.util.AbstractLocalizedTextProvider.GetDefaultMessageReturnArg getDefaultMessageWithAlternateKey(String key, String alternateKey, Locale locale, ValueStack valueStack, Object[] args, String defaultMessage)
null
) if both
key lookup operations fail.
A known use case is when a key indexes a collection (e.g. user.phone[0]) for which some specific keys may exist, but not all, along with a general key (e.g. user.phone[*]). In such cases the specific key would be passed in the key parameter and the general key would be passed in the alternateKey parameter.
key
- the initial key to search for a value within the default resource bundles.alternateKey
- the alternate (fall-back) key to search for a value within the default resource bundles, if the initial key lookup fails.locale
- the Locale
to be used for the default resource bundle lookup.valueStack
- the ValueStack
associated with the operation.args
- the argument array for parameterized messages (may be null
).defaultMessage
- the default message String
to use if both key lookup operations fail.GetDefaultMessageReturnArg
result containing the processed message lookup (by key first, then alternateKey if key's lookup fails).
If both key lookup operations fail, defaultMessage is used for processing.
If defaultMessage is null
then the return result may be null
.protected String getMessage(String bundleName, Locale locale, String key, ValueStack valueStack, Object[] args)
Copyright © 2000–2023 Apache Software Foundation. All rights reserved.