Class CmsMessages
- java.lang.Object
-
- org.opencms.gwt.client.util.CmsMessages
-
public class CmsMessages extends java.lang.Object
Reads localized resource Strings from ajava.util.ResourceBundle
and provides convenience methods to access the Strings from a template.- Since:
- 8.0.0
- See Also:
CmsMessages
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
KEY_SHORT_SUFFIX
The suffix of a "short" localized key name.static java.lang.String
UNKNOWN_KEY_EXTENSION
Prefix / Suffix for unknown keys.
-
Constructor Summary
Constructors Constructor Description CmsMessages(java.lang.String bundleName)
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
formatMessage(java.lang.String result, java.lang.Object... args)
Helper method for formatting message parameters.static java.lang.String
formatUnknownKey(java.lang.String keyName)
Formats an unknown key.CmsMessages
getBundle()
Returns the localized message bundle wrapped in this instance.java.lang.String
getBundleName()
Returns the name of the resource bundle.java.lang.String
getDate(long time)
Returns a formated date String from a timestamp value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.java.lang.String
getDate(java.util.Date date)
Returns a formated date String from a Date value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.java.lang.String
getDate(java.util.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.java.lang.String
getDateTime(long time)
Returns a formated date and time String from a timestamp value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.java.lang.String
getDateTime(java.util.Date date)
Returns a formated date and time String from a Date value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.java.lang.String
getDateTime(java.util.Date date, CmsDateTimeUtil.Format format)
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.com.google.gwt.i18n.client.Dictionary
getDictionary()
Returns the internal dictionary.static boolean
isUnknownKey(java.lang.String value)
Returnstrue
if the provided value matches the scheme"??? " + keyName + " ???"
, that is the value appears to be an unknown key.java.lang.String
key(java.lang.String keyName)
Returns the localized resource string for a given message key.java.lang.String
key(java.lang.String keyName, boolean allowNull)
Returns the localized resource string for a given message key.java.lang.String
key(java.lang.String key, java.lang.Object... args)
Returns the selected localized message for the initialized resource bundle and locale.java.lang.String
keyDefault(java.lang.String keyName, java.lang.String defaultValue)
Returns the localized resource string for a given message key.java.lang.String
keyWithParams(java.lang.String keyName)
Returns the localized resource string for a given message key, treating all values appended with "|" as replacement parameters.
-
-
-
Field Detail
-
KEY_SHORT_SUFFIX
public static final java.lang.String KEY_SHORT_SUFFIX
The suffix of a "short" localized key name.- See Also:
- Constant Field Values
-
UNKNOWN_KEY_EXTENSION
public static final java.lang.String UNKNOWN_KEY_EXTENSION
Prefix / Suffix for unknown keys.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CmsMessages
public CmsMessages(java.lang.String bundleName)
Default constructor.- Parameters:
bundleName
- the localized bundle name
-
-
Method Detail
-
formatMessage
public static java.lang.String formatMessage(java.lang.String result, java.lang.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 java.lang.String formatUnknownKey(java.lang.String keyName)
Formats an unknown key.- Parameters:
keyName
- the key to format- Returns:
- the formatted unknown key
- See Also:
isUnknownKey(String)
-
isUnknownKey
public static boolean isUnknownKey(java.lang.String value)
Returnstrue
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 isnull
.- Parameters:
value
- the value to check- Returns:
- true if the value is matches the scheme for unknown keys
- See Also:
formatUnknownKey(String)
-
getBundle
public CmsMessages 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
public java.lang.String getBundleName()
Returns the name of the resource bundle.- Returns:
- the name of the resource bundle
-
getDate
public java.lang.String getDate(java.util.Date date)
Returns a formated date String from a Date value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.- Parameters:
date
- the Date object to format as String- Returns:
- the formatted date
-
getDate
public java.lang.String getDate(java.util.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 Stringformat
- the format to use, seeCmsDateTimeUtil.Format
for possible values- Returns:
- the formatted date
-
getDate
public java.lang.String getDate(long time)
Returns a formated date String from a timestamp value, the format beingCmsDateTimeUtil.Format.SHORT
and the locale based on this instance.- Parameters:
time
- the time value to format as date- Returns:
- the formatted date
-
getDateTime
public java.lang.String getDateTime(java.util.Date date)
Returns a formated date and time String from a Date value, the format beingCmsDateTimeUtil.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
public java.lang.String getDateTime(java.util.Date date, CmsDateTimeUtil.Format format)
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 Stringformat
- the format to use, seeCmsDateTimeUtil.Format
for possible values- Returns:
- the formatted date and time
-
getDateTime
public java.lang.String getDateTime(long time)
Returns a formated date and time String from a timestamp value, the format beingCmsDateTimeUtil.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 java.lang.String key(java.lang.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 java.lang.String key(java.lang.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 stringallowNull
- if true, 'null' is an allowed return value- Returns:
- the resource string for the given key
-
key
public java.lang.String key(java.lang.String key, java.lang.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
using the provided parameters.MessageFormat
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 keyargs
- the message arguments- Returns:
- the selected localized message for the initialized resource bundle and locale
-
keyDefault
public java.lang.String keyDefault(java.lang.String keyName, java.lang.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 stringdefaultValue
- 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 java.lang.String keyWithParams(java.lang.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
using the provided parameters. The parameters have to be appended to the key separated by a "|". For example, the keyNameMessageFormat
error.message|First|Second
would use the keyerror.message
with the parametersFirst
andSecond
. 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:
key(String, Object[])
,key(String)
-
-