com.endeca.portlet.util
Class LanguageUtils

java.lang.Object
  extended by com.endeca.portlet.util.LanguageUtils

public class LanguageUtils
extends java.lang.Object

This class provides portlet's access to messages stored in resource bundles. Three main uses are available for these methods:

Author:
sshusteff

Field Summary
static java.lang.String REQUEST_ATTRIBUTE_PORTLET_CONFIG
          Name of request attribute that holds the PortletConfig class.
 
Constructor Summary
LanguageUtils(javax.portlet.PortletRequest request)
          Constructor for using an instance of LanguageUtils without passing the request to each static method call.
 
Method Summary
 java.util.Map<java.lang.String,java.lang.String> getAllPortletMessages()
          Invokes getAllPortletMessages(PortletRequest).
static java.util.Map<java.lang.String,java.lang.String> getAllPortletMessages(javax.portlet.PortletRequest request)
          Retrieves all messages available in a resource bundle for the invoking portlet.
static java.lang.String getMessage(javax.portlet.PortletRequest request, java.lang.String messageName)
          Retrieves a message with no parameter subtitution.
static java.lang.String getMessage(javax.portlet.PortletRequest request, java.lang.String messageName, java.lang.Object[] params)
          Retrieves a message from a resource bundle, substituting the specified parameters for tokens in the message string.
 java.lang.String getMessage(java.lang.String messageName)
          Convenience instance-specific wrapper of getMessage(PortletRequest, String).
 java.lang.String getMessage(java.lang.String messageName, java.lang.Object[] params)
          Convenience instance-specific wrapper of getMessage(PortletRequest, String, Object[]).
static java.lang.String replaceMessageTokens(java.lang.String message, java.lang.Object[] params)
          Substitutes parameters for tokens in the message string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_ATTRIBUTE_PORTLET_CONFIG

public static final java.lang.String REQUEST_ATTRIBUTE_PORTLET_CONFIG
Name of request attribute that holds the PortletConfig class. JSR 286 spec requires the portlet container to make the portlet config available in the PortletRequest under this attribute name.

See Also:
Constant Field Values
Constructor Detail

LanguageUtils

public LanguageUtils(javax.portlet.PortletRequest request)
Constructor for using an instance of LanguageUtils without passing the request to each static method call.

Parameters:
request - PortletRequest for the portlet whose resource bundle will be accessed.
Method Detail

getMessage

public java.lang.String getMessage(java.lang.String messageName)
Convenience instance-specific wrapper of getMessage(PortletRequest, String). Uses the instance's PortletRequest as the first argument.

Parameters:
messageName - Name of message to retrieve from resource bundle.
Returns:
Returns localized message from a resource bundle, or the specified default value.

getMessage

public java.lang.String getMessage(java.lang.String messageName,
                                   java.lang.Object[] params)
Convenience instance-specific wrapper of getMessage(PortletRequest, String, Object[]). Uses the instance's PortletRequest as the first argument.

Parameters:
messageName - Name of message to retrieve from resource bundle.
params - Array of parameters to substitute for tokens in the message string.
Returns:
Returns localized message from a resource bundle, or the specified default value.

getAllPortletMessages

public java.util.Map<java.lang.String,java.lang.String> getAllPortletMessages()
Invokes getAllPortletMessages(PortletRequest). Uses the instance's PortletRequest as the first argument.

Returns:
Returns a map of messages available in the portlet's resource bundle.

getMessage

public static java.lang.String getMessage(javax.portlet.PortletRequest request,
                                          java.lang.String messageName)
Retrieves a message with no parameter subtitution. Invokes getMessage(PortletRequest, String, Object[]) with a null params argument.

Parameters:
request - PortletRequest for the portlet whose resource bundle will be accessed.
messageName - Name of message to retrieve from resource bundle.
Returns:
Returns localized message from a resource bundle, or the specified default value.

getMessage

public static java.lang.String getMessage(javax.portlet.PortletRequest request,
                                          java.lang.String messageName,
                                          java.lang.Object[] params)
Retrieves a message from a resource bundle, substituting the specified parameters for tokens in the message string. If the request or messageName are null, if an error occurs, or if no message is retrieved for the given messageName, the defaultValue is used. If params are passed in, this method attempts to substitute the values for both a successfully retrieved message from a resource bundle as well as for the default value. This means that, if parameters are used, the defaultValue should be passed in with tokens that support the params. See MessageFormat class for details about parameter substitution.

Parameters:
request - PortletRequest for the portlet whose resource bundle will be accessed.
messageName - Name of message to retrieve from resource bundle.
params - Parameters to substitute for tokens in the message.
Returns:
Returns localized message from a resource bundle, or the specified default value.
See Also:
MessageFormat.format(String, Object...)

getAllPortletMessages

public static java.util.Map<java.lang.String,java.lang.String> getAllPortletMessages(javax.portlet.PortletRequest request)
Retrieves all messages available in a resource bundle for the invoking portlet. messages are returned in a map, keyed by message name. Messages that contain tokens are returned in their raw form. To substitute parameters for tokens in these strings, see replaceMessageTokens(String, Object[]). This method may be used to load (and potentially cache) messages for a portlet and avoid repeated calls to load the same strings from the resource bundle.

Parameters:
request - PortletRequest for portlet whose resource bundle will be loaded.
Returns:
Returns a map of messages available in the portlet's resource bundle.

replaceMessageTokens

public static java.lang.String replaceMessageTokens(java.lang.String message,
                                                    java.lang.Object[] params)
Substitutes parameters for tokens in the message string. See MessageFormat class for details about parameter substitution.

Parameters:
message - Message with tokens to replace.
params - Parameters to substitute for tokens in the message.
Returns:
Returns the message with tokens replaced by the passed in parameters, or the original string, if params are null.
See Also:
MessageFormat.format(String, Object...)