Home » xwork-2.1.5 » com.opensymphony » xwork2 » util » [javadoc | source]
com.opensymphony.xwork2.util
public class: LocalizedTextUtil [javadoc | source]
java.lang.Object
   com.opensymphony.xwork2.util.LocalizedTextUtil
Provides support for localization in XWork.

Resource bundles are searched in the following order:

  1. ActionClass.properties
  2. Interface.properties (every interface and sub-interface)
  3. BaseClass.properties (all the way to Object.properties)
  4. ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
  5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
  6. search up the i18n message key hierarchy itself
  7. global resource properties

To clarify #5, while traversing the package hierarchy, Struts 2 will look for a file package.properties:

com/
  acme/
    package.properties
    actions/
      package.properties
      FooAction.java
      FooAction.properties

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 programatically, as well as the locale.

Nested Class Summary:
static class  LocalizedTextUtil.MessageFormatKey   
static class  LocalizedTextUtil.GetDefaultMessageReturnArg   
Method from com.opensymphony.xwork2.util.LocalizedTextUtil Summary:
addDefaultResourceBundle,   clearBundle,   clearDefaultResourceBundles,   findDefaultText,   findDefaultText,   findResourceBundle,   findText,   findText,   findText,   findText,   findText,   findText,   localeFromString,   reset,   setDelegatedClassLoader,   setReloadBundles
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.opensymphony.xwork2.util.LocalizedTextUtil Detail:
 public static  void addDefaultResourceBundle(String resourceBundleName) 
    Add's the bundle to the internal list of default bundles.

    If the bundle already exists in the list it will be readded.

 public static  void clearBundle(String bundleName) 
    Removes the bundle from any cached "misses"
 public static  void clearDefaultResourceBundles() 
    Clears the internal list of resource bundles.
 public static String findDefaultText(String aTextName,
    Locale locale) 
    Returns a localized message for the specified key, aTextName. Neither the key nor the message is evaluated.
 public 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. Neither the key nor the message is evaluated.
 public static ResourceBundle findResourceBundle(String aBundleName,
    Locale locale) 
    Finds the given resorce bundle by it's name.

    Will use Thread.currentThread().getContextClassLoader() as the classloader. If #delegatedClassLoader is defined and the bundle cannot be found the current classloader it will delegate to that.

 public static String findText(Class aClass,
    String aTextName,
    Locale locale) 
 public 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.

 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:

    1. Look for message in aClass' class hierarchy.
      1. Look for the message in a resource bundle for aClass
      2. If not found, look for the message in a resource bundle for any implemented interface
      3. If not found, traverse up the Class' hierarchy and repeat from the first sub-step
    2. If not found and aClass is a ModelDriven Action, then look for message in the model's class hierarchy (repeat sub-steps listed above).
    3. If not found, look for message in child property. This is determined by evaluating the message key as an OGNL expression. For example, if the key is user.address.state, then it will attempt to see if "user" can be resolved into an object. If so, repeat the entire process fromthe beginning with the object's class as aClass and "address.state" as the message key.
    4. If not found, look for the message in aClass' package hierarchy.
    5. If still not found, look for the message in the default resource bundles.
    6. Return defaultMessage

    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.

 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.

 public 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:

    1. Look for message in aClass' class hierarchy.
      1. Look for the message in a resource bundle for aClass
      2. If not found, look for the message in a resource bundle for any implemented interface
      3. If not found, traverse up the Class' hierarchy and repeat from the first sub-step
    2. If not found and aClass is a ModelDriven Action, then look for message in the model's class hierarchy (repeat sub-steps listed above).
    3. If not found, look for message in child property. This is determined by evaluating the message key as an OGNL expression. For example, if the key is user.address.state, then it will attempt to see if "user" can be resolved into an object. If so, repeat the entire process fromthe beginning with the object's class as aClass and "address.state" as the message key.
    4. If not found, look for the message in aClass' package hierarchy.
    5. If still not found, look for the message in the default resource bundles.
    6. Return defaultMessage

    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.

 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.

 public static Locale localeFromString(String localeStr,
    Locale defaultLocale) 
 public static  void reset() 
    Clears all the internal lists.
 public static  void setDelegatedClassLoader(ClassLoader classLoader) 
    Sets a ClassLoader to look up the bundle from if none can be found on the current thread's classloader
 public static  void setReloadBundles(boolean reloadBundles) 
    Should resorce bundles be reloaded.