Class CmsMessages

java.lang.Object
org.opencms.gwt.client.util.CmsMessages

public class CmsMessages extends Object
Reads localized resource Strings from a java.util.ResourceBundle and provides convenience methods to access the Strings from a template.

Since:
8.0.0
See Also:
  • CmsMessages
  • Field Details

  • Constructor Details

    • CmsMessages

      public CmsMessages(String bundleName)
      Default constructor.

      Parameters:
      bundleName - the localized bundle name
  • Method Details

    • formatMessage

      public static String formatMessage(String result, Object... args)
      Helper method for formatting message parameters.

      Parameters:
      result - the raw message containing placeholders like {0}
      args - the parameters to insert into the placeholders
      Returns:
      the formatted message
    • formatUnknownKey

      public static String formatUnknownKey(String keyName)
      Formats an unknown key.

      Parameters:
      keyName - the key to format
      Returns:
      the formatted unknown key
      See Also:
    • isUnknownKey

      public static boolean isUnknownKey(String value)
      Returns true if the provided value matches the scheme "??? " + keyName + " ???", that is the value appears to be an unknown key.

      Also returns true if the given value is null.

      Parameters:
      value - the value to check
      Returns:
      true if the value is matches the scheme for unknown keys
      See Also:
    • getBundle

      Returns the localized message bundle wrapped in this instance.

      Mainly for API compatibility with the core localization methods.

      Returns:
      the localized message bundle wrapped in this instance
    • getBundleName

      Returns the name of the resource bundle.

      Returns:
      the name of the resource bundle
    • getDate

      public String getDate(Date date)
      Returns a formated date String from a Date value, the format being CmsDateTimeUtil.Format.SHORT and the locale based on this instance.

      Parameters:
      date - the Date object to format as String
      Returns:
      the formatted date
    • getDate

      public String getDate(Date date, CmsDateTimeUtil.Format format)
      Returns a formated date String from a Date value, the formatting based on the provided option and the locale based on this instance.

      Parameters:
      date - the Date object to format as String
      format - the format to use, see CmsDateTimeUtil.Format for possible values
      Returns:
      the formatted date
    • getDate

      public String getDate(long time)
      Returns a formated date String from a timestamp value, the format being CmsDateTimeUtil.Format.SHORT and the locale based on this instance.

      Parameters:
      time - the time value to format as date
      Returns:
      the formatted date
    • getDateTime

      public String getDateTime(Date date)
      Returns a formated date and time String from a Date value, the format being CmsDateTimeUtil.Format.SHORT and the locale based on this instance.

      Parameters:
      date - the Date object to format as String
      Returns:
      the formatted date and time
    • getDateTime

      Returns a formated date and time String from a Date value, the formatting based on the provided option and the locale based on this instance.

      Parameters:
      date - the Date object to format as String
      format - the format to use, see CmsDateTimeUtil.Format for possible values
      Returns:
      the formatted date and time
    • getDateTime

      public String getDateTime(long time)
      Returns a formated date and time String from a timestamp value, the format being CmsDateTimeUtil.Format.SHORT and the locale based on this instance.

      Parameters:
      time - the time value to format as date
      Returns:
      the formatted date and time
    • getDictionary

      public com.google.gwt.i18n.client.Dictionary getDictionary()
      Returns the internal dictionary.

      Returns:
      the internal dictionary
    • key

      public String key(String keyName)
      Returns the localized resource string for a given message key.

      If the key was not found in the bundle, the return value is "??? " + keyName + " ???". This will also be returned if the bundle was not properly initialized first.

      Parameters:
      keyName - the key for the desired string
      Returns:
      the resource string for the given key
    • key

      public String key(String keyName, boolean allowNull)
      Returns the localized resource string for a given message key.

      If the key was not found in the bundle, the return value depends on the setting of the allowNull parameter. If set to false, the return value is always a String in the format "??? " + keyName + " ???". If set to true, null is returned if the key is not found. This will also be returned if the bundle was not properly initialized first.

      Parameters:
      keyName - the key for the desired string
      allowNull - if true, 'null' is an allowed return value
      Returns:
      the resource string for the given key
    • key

      public String key(String key, Object... args)
      Returns the selected localized message for the initialized resource bundle and locale.

      If the key was found in the bundle, it will be formatted using a MessageFormat using the provided parameters.

      If the key was not found in the bundle, the return value is "??? " + keyName + " ???". This will also be returned if the bundle was not properly initialized first.

      Parameters:
      key - the message key
      args - the message arguments
      Returns:
      the selected localized message for the initialized resource bundle and locale
    • keyDefault

      public String keyDefault(String keyName, String defaultValue)
      Returns the localized resource string for a given message key.

      If the key was not found in the bundle, the provided default value is returned.

      Parameters:
      keyName - the key for the desired string
      defaultValue - the default value in case the key does not exist in the bundle
      Returns:
      the resource string for the given key it it exists, or the given default if not
    • keyWithParams

      public String keyWithParams(String keyName)
      Returns the localized resource string for a given message key, treating all values appended with "|" as replacement parameters.

      If the key was found in the bundle, it will be formatted using a MessageFormat using the provided parameters. The parameters have to be appended to the key separated by a "|". For example, the keyName error.message|First|Second would use the key error.message with the parameters First and Second. This would be the same as calling key(String, Object[]).

      If no parameters are appended with "|", this is the same as calling key(String).

      If the key was not found in the bundle, the return value is "??? " + keyName + " ???". This will also be returned if the bundle was not properly initialized first.

      Parameters:
      keyName - the key for the desired string, optionally containing parameters appended with a "|"
      Returns:
      the resource string for the given key
      See Also: