public class LocalizedTextUtil extends Object
Resource bundles are searched in the following order:
To clarify #5, while traversing the package hierarchy, Struts 2 will look for a file package.properties:
com/If FooAction.properties does not exist, com/acme/action/package.properties will be searched for, if not found com/acme/package.properties, if not found com/package.properties, etc.
A global resource bundle could be specified programmatically, as well as the locale.
Constructor and Description |
---|
LocalizedTextUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
addDefaultResourceBundle(String resourceBundleName)
Add's the bundle to the internal list of default bundles.
|
static void |
clearBundle(String bundleName) |
static void |
clearDefaultResourceBundles()
Clears the internal list of resource bundles.
|
static String |
findDefaultText(String aTextName,
Locale locale)
Returns a localized message for the specified key, aTextName.
|
static 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.
|
static ResourceBundle |
findResourceBundle(String aBundleName,
Locale locale)
Finds the given resource bundle by it's name.
|
static 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. |
static String |
findText(Class aClass,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args)
Finds a localized text message for the given key, aTextName.
|
static String |
findText(Class aClass,
String aTextName,
Locale locale,
String defaultMessage,
Object[] args,
ValueStack valueStack)
Finds a localized text message for the given key, aTextName.
|
static 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.
|
static 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.
|
static 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.
|
static Locale |
localeFromString(String localeStr,
Locale defaultLocale)
Builds a
Locale from a String of the form en_US_foo into a Locale
with language "en", country "US" and variant "foo". |
static void |
reset()
Clears all the internal lists.
|
static void |
setDelegatedClassLoader(ClassLoader classLoader) |
static void |
setDevMode(boolean devMode) |
static void |
setReloadBundles(boolean reloadBundles)
Should resorce bundles be reloaded.
|
public static void clearDefaultResourceBundles()
public static void setReloadBundles(boolean reloadBundles)
reloadBundles
- reload bundles?public static void setDevMode(boolean devMode)
public static void addDefaultResourceBundle(String resourceBundleName)
If the bundle already exists in the list it will be readded.
resourceBundleName
- the name of the bundle to add.public static Locale localeFromString(String localeStr, Locale defaultLocale)
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 static String findDefaultText(String aTextName, Locale locale)
aTextName
- the message keylocale
- the locale the message should be forpublic static String findDefaultText(String aTextName, Locale locale, Object[] params)
aTextName
- the message keylocale
- the locale the message should be forparams
- an array of objects to be substituted into the message textpublic static ResourceBundle findResourceBundle(String aBundleName, Locale locale)
Will use Thread.currentThread().getContextClassLoader()
as the classloader.
aBundleName
- the name of the bundle (usually it's FQN classname).locale
- the locale.public static void setDelegatedClassLoader(ClassLoader classLoader)
classLoader
- a ClassLoader
to look up the bundle from if none can be found on the current thread's classloaderpublic static void clearBundle(String bundleName)
bundleName
- Removes the bundle from any cached "misses"public static 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 static 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:
ModelDriven
Action, then look for message in
the model's class hierarchy (repeat sub-steps listed above).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 static 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:
ModelDriven
Action, then look for message in
the model's class hierarchy (repeat sub-steps listed above).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 WARN log 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 static 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 static 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 static 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.
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 static void reset()
Copyright © 2000–2017 Apache Software Foundation. All rights reserved.