|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object mazz.i18n.Msg
public class Msg
Retrieves messages from a message ResourceBundle
and formats them as appropriate using
MessageFormat
. Instances of this object are mostly read-only - the only thing you can change
is the locale. Changing the locale will attempt to convert the message to that new locale's format.
I18N Msg
objects are serializable and will be re-translated once it is deserialized. This means
that a message could be localized in the English language, serialized and sent over the wire to another JVM with
a German locale and when the deserialized message is retrieved again, it will be in German. This feature assumes
a resource bundle with the same base bundle name exists in the JVM that deserialized the Msg
. If it
does not, then the original message will be used (in the previous example, it would mean the English message
would be retrieved in the JVM, even though its locale is German).
There is a method to the madness to some of the methods' parameter ordering. Anytime you want to specify a
localized message, you always specify the base bundle name first, followed by the locale, the bundle key and the
variable list of arguments that go with the keyed message (all in that order). Bundle name and locale are both
optional. This is consistent with the way localized messages are specified in constructors for
LocalizedException
and LocalizedRuntimeException
. When you need to specify a
Throwable
with your localized message, it is specified before those parameters. Again, this is consistent
both in this class and the localized exception classes (see
createMsg(mazz.i18n.Msg.BundleBaseName, Locale, String, Object[])
and
LocalizedException.LocalizedException(Throwable, mazz.i18n.Msg.BundleBaseName, Locale, String, Object[])
as examples).
LocalizedException
,
LocalizedRuntimeException
,
Serialized FormNested Class Summary | |
---|---|
static class |
Msg.BundleBaseName
The purpose of this class is to offer a strongly typed object (more strongly than String) so we can pass bundle base names to our vararg methods and not have this be confused with a key or arg parameter. |
Constructor Summary | |
---|---|
Msg()
Initializes the message repository using the default, localized resource bundle. |
|
Msg(java.util.Locale locale)
Initializes the message repository using the default resource bundle and given locale. |
|
Msg(Msg.BundleBaseName basename)
Initializes the message repository using the default locale and given resource bundle base name. |
|
Msg(Msg.BundleBaseName basename,
java.util.Locale locale)
Initializes the message repository with the appropriate resource bundle. |
Method Summary | |
---|---|
static Msg |
createMsg(java.util.Locale locale,
java.lang.String key,
java.lang.Object... varargs)
Creates a message object and automatically looks up the given resource bundle message. |
static Msg |
createMsg(Msg.BundleBaseName basename,
java.util.Locale locale,
java.lang.String key,
java.lang.Object... varargs)
Creates a message object and automatically looks up the given resource bundle message. |
static Msg |
createMsg(Msg.BundleBaseName basename,
java.lang.String key,
java.lang.Object... varargs)
Creates a message object and automatically looks up the given resource bundle message. |
static Msg |
createMsg(java.lang.String key,
java.lang.Object... varargs)
Creates a message object and automatically looks up the given resource bundle message. |
protected static java.lang.Object |
deserialize(byte[] serializedData)
Deserializes the given serialization data and returns the object. |
Msg.BundleBaseName |
getBundleBaseName()
Returns the base name of the resource bundles to be used when looking for messages. |
static Msg.BundleBaseName |
getBundleBaseNameDefault()
Returns the default bundle base name that all instances of this class will use when no basename is provided. |
java.lang.String |
getLastMessage()
Returns the last message that this instance read from a resource bundle. |
java.util.Locale |
getLocale()
Sets a new locale. |
java.lang.String |
getMsg(java.lang.String key,
java.lang.Object... varargs)
Returns the message string identified with the given key. |
protected java.util.ResourceBundle |
getResourceBundle()
Gets the resource bundle to use, based on the current values of the base name
and the locale . |
protected static byte[] |
serialize(java.io.Serializable object)
Given a serializable object, this will return the object's serialized byte array representation. |
static void |
setBundleBaseNameDefault(Msg.BundleBaseName newDefault)
Sets the default bundle base name that all instances of this class will use when no basename is provided. |
void |
setLocale(java.util.Locale locale)
Sets a new locale. |
java.lang.String |
toString()
Same as getLastMessage() . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Msg(Msg.BundleBaseName basename, java.util.Locale locale)
basename
- resource bundle to use, if null
uses the
default
locale
- locale used to determine proper resource bundle to use, if null
uses the default
locale of the JVMpublic Msg(java.util.Locale locale)
locale
- locale used to determine proper resource bundle to useMsg(mazz.i18n.Msg.BundleBaseName, Locale)
public Msg(Msg.BundleBaseName basename)
basename
- resource bundle to useMsg(mazz.i18n.Msg.BundleBaseName, Locale)
public Msg()
Method Detail |
---|
public static Msg createMsg(Msg.BundleBaseName basename, java.util.Locale locale, java.lang.String key, java.lang.Object... varargs)
message
object and automatically looks up the given resource bundle message. The caller
need only call toString()
to get the resource bundle message after this method returns.
basename
- the base name of the resource bundlelocale
- the locale to determine what bundle to usekey
- the resource bundle key namevarargs
- arguments to help fill in the resource bundle message
null
Msg object is returnedpublic static Msg createMsg(java.util.Locale locale, java.lang.String key, java.lang.Object... varargs)
message
object and automatically looks up the given resource bundle message. The caller
need only call toString()
to get the resource bundle message after this method returns. A default
basename is used along with the given locale.
locale
- the locale to determine what bundle to usekey
- the resource bundle key namevarargs
- arguments to help fill in the resource bundle message
null
Msg object is returnedpublic static Msg createMsg(Msg.BundleBaseName basename, java.lang.String key, java.lang.Object... varargs)
message
object and automatically looks up the given resource bundle message. The caller
need only call toString()
to get the resource bundle message after this method returns.
basename
- the base name of the resource bundlekey
- the resource bundle key namevarargs
- arguments to help fill in the resource bundle message
null
Msg object is returnedpublic static Msg createMsg(java.lang.String key, java.lang.Object... varargs)
message
object and automatically looks up the given resource bundle message. The caller
need only call toString()
to get the resource bundle message after this method returns. A default
basename and the default locale is used to determine what resource bundle to use.
key
- the resource bundle key namevarargs
- arguments to help fill in the resource bundle message
null
Msg object is returnedpublic static Msg.BundleBaseName getBundleBaseNameDefault()
public static void setBundleBaseNameDefault(Msg.BundleBaseName newDefault)
newDefault
- the new bundle base name default (e.g. "com.abc.messages")public java.util.Locale getLocale()
public void setLocale(java.util.Locale locale)
getLastMessage()
is
called, it may return a different string if the locale was set to a different locale than what it was before.
locale
- the new locale to set, if null
, the default locale will be setpublic Msg.BundleBaseName getBundleBaseName()
public java.lang.String getLastMessage()
This object can change the messages it finds by simply setting the locale
. When
you change the local, the message is retrieved again when this method is called and is stored as the last
message.
public java.lang.String getMsg(java.lang.String key, java.lang.Object... varargs)
getLastMessage()
when it returns.
key
- identifies the message to be retrievedvarargs
- arguments to replace placeholders in message
MessageFormat
public java.lang.String toString()
getLastMessage()
.
toString
in class java.lang.Object
Object.toString()
protected java.util.ResourceBundle getResourceBundle()
the base name
and the locale
.
protected static byte[] serialize(java.io.Serializable object) throws java.lang.Exception
object
- the object to serialize
java.lang.Exception
- if failed to serialize the objectprotected static java.lang.Object deserialize(byte[] serializedData) throws java.lang.Exception
serializedData
- the serialized data as a byte array
java.lang.Exception
- if failed to deserialize the object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |