public final class Messages extends Object
static Messages x = new Messages("org.package.MyMessages");
Later the message is typically generated this way if there are no
substitution arguments:
String msg = x.createMessage(org.package.MyMessages.KEY_ONE, null);
If there are arguments substitutions then something like this:
String filename = ...;
String directory = ...;
String msg = x.createMessage(org.package.MyMessages.KEY_TWO,
new Object[] {filename, directory) );
The constructor of an instance of this class must be given
the class name of a class that extends java.util.ListResourceBundle
("org.package.MyMessages" in the example above).
The name should not have any language suffix
which will be added automatically by this utility class.
The message class ("org.package.MyMessages")
must define the abstract method getContents() that is
declared in its base class, for example:
public Object[][] getContents() {return contents;}
It is suggested that the message class expose its
message keys like this:
public static final String KEY_ONE = "KEY1";
public static final String KEY_TWO = "KEY2";
. . .
and used through their names (KEY_ONE ...) rather than
their values ("KEY1" ...).
The field contents (returned by getContents()
should be initialized something like this:
public static final Object[][] contents = {
{ KEY_ONE, "Something has gone wrong!" },
{ KEY_TWO, "The file ''{0}'' does not exist in directory ''{1}''." },
. . .
{ KEY_N, "Message N" } }
Where that section of code with the KEY to Message mappings
(where the message classes 'contents' field is initialized)
can have the Message strings translated in an alternate language
in a errorResourceClass with a language suffix.
More sophisticated use of this class would be to pass null
when contructing it, but then call loadResourceBundle()
before creating any messages.
This class is not a public API, it is only public because it is
used in org.apache.xml.serializer.Modifier and Type | Method and Description |
---|---|
String |
createMessage(String msgKey,
Object[] args)
Creates a message from the specified key and replacement
arguments, localized to the given locale.
|
public final String createMessage(String msgKey, Object[] args)
msgKey
- The key for the message text.args
- The arguments to be used as replacement text
in the message created.Copyright © 2019 JBoss by Red Hat. All rights reserved.