com.opensymphony.xwork.util
Class LocalizedTextUtil

java.lang.Object
  extended bycom.opensymphony.xwork.util.LocalizedTextUtil

public class LocalizedTextUtil
extends Object

Provides support for localization in XWork.

Author:
Jason Carreira, Mark Woon

Constructor Summary
LocalizedTextUtil()
           
 
Method Summary
static void addDefaultResourceBundle(String resourceBundleName)
           
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)
           
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, OgnlValueStack 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, OgnlValueStack valueStack)
           
static void setReloadBundles(boolean reloadBundles)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalizedTextUtil

public LocalizedTextUtil()
Method Detail

setReloadBundles

public static void setReloadBundles(boolean reloadBundles)

addDefaultResourceBundle

public static void addDefaultResourceBundle(String resourceBundleName)

findDefaultText

public static String findDefaultText(String aTextName,
                                     Locale locale)
                              throws MissingResourceException
Returns a localized message for the specified key, aTextName. Neither the key nor the message is evaluated.

Parameters:
aTextName - the message key
locale - the locale the message should be for
Returns:
a localized message based on the specified key
Throws:
MissingResourceException - if no message can be found for the specified key

findDefaultText

public static String findDefaultText(String aTextName,
                                     Locale locale,
                                     Object[] params)
                              throws MissingResourceException
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.

Parameters:
aTextName - the message key
locale - the locale the message should be for
params - an array of objects to be substituted into the message text
Returns:
A formatted message based on the specified key
Throws:
MissingResourceException - if no message can be found for the specified key

findResourceBundle

public static ResourceBundle findResourceBundle(String aBundleName,
                                                Locale locale)

findText

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

See Also:
findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)

findText

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.

Parameters:
aClass - the class whose name to use as the start point for the search
aTextName - the key to find the text message for
locale - the locale the message should be for
defaultMessage - the message to be returned if no text message can be found in any resource bundle
Returns:
the localized text, or null if none can be found and no defaultMessage is provided

findText

public static String findText(Class aClass,
                              String aTextName,
                              Locale locale,
                              String defaultMessage,
                              Object[] args,
                              OgnlValueStack 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.

Parameters:
aClass - the class whose name to use as the start point for the search
aTextName - the key to find the text message for
locale - the locale the message should be for
defaultMessage - the message to be returned if no text message can be found in any resource bundle
valueStack - the value stack to use to evaluate expressions instead of the one in the ActionContext ThreadLocal
Returns:
the localized text, or null if none can be found and no defaultMessage is provided

findText

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.

See Also:
findText(java.util.ResourceBundle, java.lang.String, java.util.Locale, java.lang.String, java.lang.Object[])

findText

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.


findText

public static String findText(ResourceBundle bundle,
                              String aTextName,
                              Locale locale,
                              String defaultMessage,
                              Object[] args,
                              OgnlValueStack valueStack)

XWork Project Page