|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.opencms.i18n.CmsMessages
public class CmsMessages
Reads localized resource Strings from a java.util.ResourceBundle
and provides convenience methods to access the Strings from a template.
This class is frequently used from JSP templates. Because of that, throwing of
exceptions related to the access of the resource bundle are suppressed
so that a template always execute. The class provides an isInitialized()
method
that can be checked to see if the instance was properly initialized.
Field Summary | |
---|---|
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 | |
---|---|
protected |
CmsMessages()
Empty constructor for subclassing. |
|
CmsMessages(java.lang.String bundleName,
java.util.Locale locale)
Constructor for the messages with an initialized java.util.Locale . |
|
CmsMessages(java.lang.String bundleName,
java.lang.String language)
Constructor for the messages with a language string. |
|
CmsMessages(java.lang.String bundleName,
java.lang.String language,
java.lang.String country)
Constructor for the messages with language and country code strings. |
|
CmsMessages(java.lang.String bundleName,
java.lang.String language,
java.lang.String country,
java.lang.String variant)
Constructor for the messages with language, country code and variant strings. |
Method Summary | |
---|---|
boolean |
equals(java.lang.Object obj)
|
static java.lang.String |
formatUnknownKey(java.lang.String keyName)
Formats an unknown key. |
protected java.lang.String |
getBundleName()
Returns the name of the resource bundle this object was initialized with. |
java.lang.String |
getDate(java.util.Date date)
Returns a formated date String from a Date value, the format being DateFormat.SHORT and the locale
based on this instance. |
java.lang.String |
getDate(java.util.Date date,
int 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 |
getDate(long time)
Returns a formated date String from a timestamp value, the format being DateFormat.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 being DateFormat.SHORT and the locale
based on this instance. |
java.lang.String |
getDateTime(java.util.Date date,
int 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. |
java.lang.String |
getDateTime(long time)
Returns a formated date and time String from a timestamp value, the format being DateFormat.SHORT and the locale
based on this instance. |
java.util.Locale |
getLocale()
Returns the locale to use for looking up this messages. |
java.util.ResourceBundle |
getResourceBundle()
Returns the resource bundle this message object was initialized with. |
java.lang.String |
getString(java.lang.String keyName)
Directly calls the getString(String) method of the wrapped ResourceBundle. |
int |
hashCode()
|
boolean |
isInitialized()
Checks if the bundle was properly initialized. |
static boolean |
isUnknownKey(java.lang.String value)
Returns true if the provided value matches the scheme
"??? |
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 arg0)
Returns the selected localized message for the initialized resource bundle and locale. |
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 |
key(java.lang.String key,
java.lang.Object arg0,
java.lang.Object arg1)
Returns the selected localized message for the initialized resource bundle and locale. |
java.lang.String |
key(java.lang.String key,
java.lang.Object arg0,
java.lang.Object arg1,
java.lang.Object arg2)
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. |
protected void |
setBundleName(java.lang.String bundleName)
Sets the bundleName. |
protected void |
setLocale(java.util.Locale locale)
Sets the locale. |
protected void |
setResourceBundle(java.util.ResourceBundle resourceBundle)
Sets the resource bundle. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String KEY_SHORT_SUFFIX
public static final java.lang.String UNKNOWN_KEY_EXTENSION
Constructor Detail |
---|
public CmsMessages(java.lang.String bundleName, java.util.Locale locale)
java.util.Locale
.
bundleName
- the base ResourceBundle namelocale
- the m_locale to use, eg. "de", "en" etc.public CmsMessages(java.lang.String bundleName, java.lang.String language)
The language
is a 2 letter language ISO code, e.g. "EN"
.
The Locale for the messages will be created like this:
new Locale(language, "", "")
.
bundleName
- the base ResourceBundle namelanguage
- ISO language indentificator for the m_locale of the bundlepublic CmsMessages(java.lang.String bundleName, java.lang.String language, java.lang.String country)
The language
is a 2 letter language ISO code, e.g. "EN"
.
The country
is a 2 letter country ISO code, e.g. "us"
.
The Locale for the messages will be created like this:
new Locale(language, country, "")
.
bundleName
- the base ResourceBundle namelanguage
- ISO language indentificator for the m_locale of the bundlecountry
- ISO 2 letter country code for the m_locale of the bundlepublic CmsMessages(java.lang.String bundleName, java.lang.String language, java.lang.String country, java.lang.String variant)
The language
is a 2 letter language ISO code, e.g. "EN"
.
The country
is a 2 letter country ISO code, e.g. "us"
.
The variant
is a vendor or browser-specific code, e.g. "POSIX"
.
The Locale for the messages will be created like this:
new Locale(language, country, variant)
.
bundleName
- the base ResourceBundle namelanguage
- language indentificator for the m_locale of the bundlecountry
- 2 letter country code for the m_locale of the bundlevariant
- a vendor or browser-specific variant codeprotected CmsMessages()
Method Detail |
---|
public static java.lang.String formatUnknownKey(java.lang.String keyName)
keyName
- the key to format
isUnknownKey(String)
public static boolean isUnknownKey(java.lang.String value)
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
.
value
- the value to check
formatUnknownKey(String)
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
Object.equals(java.lang.Object)
public java.util.ResourceBundle getResourceBundle()
public java.lang.String getDate(java.util.Date date)
DateFormat.SHORT
and the locale
based on this instance.
date
- the Date object to format as String
public java.lang.String getDate(java.util.Date date, int format)
date
- the Date object to format as Stringformat
- the format to use, see CmsMessages
for possible values
public java.lang.String getDate(long time)
DateFormat.SHORT
and the locale
based on this instance.
time
- the time value to format as date
public java.lang.String getDateTime(java.util.Date date)
DateFormat.SHORT
and the locale
based on this instance.
date
- the Date object to format as String
public java.lang.String getDateTime(java.util.Date date, int format)
date
- the Date object to format as Stringformat
- the format to use, see CmsMessages
for possible values
public java.lang.String getDateTime(long time)
DateFormat.SHORT
and the locale
based on this instance.
time
- the time value to format as date
public java.util.Locale getLocale()
public java.lang.String getString(java.lang.String keyName) throws CmsMessageException
If you use this this class on a template, you should consider using
the key(String)
method to get the value from the ResourceBundle because it
handles the exception for you in a convenient way.
keyName
- the key
CmsMessageException
- in case the key is not found or the bundle is not initializedpublic int hashCode()
hashCode
in class java.lang.Object
Object.hashCode()
public boolean isInitialized()
true
if bundle was initialized, false
otherwisepublic java.lang.String key(java.lang.String keyName)
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.
keyName
- the key for the desired string
public java.lang.String key(java.lang.String keyName, boolean allowNull)
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.
keyName
- the key for the desired stringallowNull
- if true, 'null' is an allowed return value
public java.lang.String key(java.lang.String key, java.lang.Object arg0)
Convenience method for messages with one argument.
key
- the message keyarg0
- the message argument
public java.lang.String key(java.lang.String key, java.lang.Object arg0, java.lang.Object arg1)
Convenience method for messages with two arguments.
key
- the message keyarg0
- the first message argumentarg1
- the second message argument
public java.lang.String key(java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)
Convenience method for messages with three arguments.
key
- the message keyarg0
- the first message argumentarg1
- the second message argumentarg2
- the third message argument
public java.lang.String key(java.lang.String key, java.lang.Object[] args)
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.
key
- the message keyargs
- the message arguments
public java.lang.String keyDefault(java.lang.String keyName, java.lang.String defaultValue)
If the key was not found in the bundle, the provided default value is returned.
keyName
- the key for the desired stringdefaultValue
- the default value in case the key does not exist in the bundle
public java.lang.String keyWithParams(java.lang.String keyName)
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 keyName MessageFormat
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.
keyName
- the key for the desired string, optinally containing parameters appended with a "|"
key(String, Object[])
,
key(String)
public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
protected java.lang.String getBundleName()
protected void setBundleName(java.lang.String bundleName)
bundleName
- the bundleName to setprotected void setLocale(java.util.Locale locale)
locale
- the locale to setprotected void setResourceBundle(java.util.ResourceBundle resourceBundle)
resourceBundle
- the resource bundle to set
|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |