Each package should have a singleton package-private message catalog class. This ensures that the correct class loader will always be used to access message resources, and minimizes use of memory:
package some.package;
// "foo" might be public
class foo {
...
// package private
static final Catalog messages = new Catalog ();
static final class Catalog extends MessageCatalog {
Catalog () { super (Catalog.class); }
}
...
}
Messages for a known client could be generated using code something like this:
String clientLanguages [];
Locale clientLocale;
String clientMessage;
// client languages will probably be provided by client,
// e.g. by an HTTP/1.1 "Accept-Language" header.
clientLanguages = new String [] { "en-ca", "fr-ca", "ja", "zh" };
clientLocale = foo.messages.chooseLocale (clientLanguages);
clientMessage = foo.messages.getMessage (clientLocale,
"fileCount",
new Object [] { new Integer (numberOfFiles) }
);
At this time, this class does not include functionality permitting messages to be passed around and localized after-the-fact. The consequence of this is that the locale for messages must be passed down through layers which have no normal reason to support such passdown, or else the system default locale must be used instead of the one the client needs.
getMessage call. This lets your
applications use IETF standard locale names, and avoids needless
use of system defaults.
java.util.ListResourceBundle.
The "resources" sub-package can be treated separately from the package with which it is associated. That main package may be sealed and possibly signed, preventing other software from adding classes to the package which would be able to access methods and data which are not designed to be publicly accessible. On the other hand, resources such as localized messages are often provided after initial product shipment, without a full release cycle for the product. Such files (text and class files) need to be added to some package. Since they should not be added to the main package, the "resources" subpackage is used without risking the security or integrity of that main package as distributed in its JAR file.
java.util.Locale , java.util.ListResourceBundle , java.text.MessageFormat
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private HashMap |
| Access | Constructor and Description |
|---|---|
| protected | MessageCatalog(Class<?>
Class whose package has localized messages packageMember)Create a message catalog for use by classes in the same package as the specified class. |
| private | MessageCatalog(Class<?>
Class whose package has localized messages packageMember, String Name of a group of resource bundles bundle)Create a message catalog for use by classes in the same package as the specified class. |
| Modifier and Type | Method and Description |
|---|---|
| private String[] | |
| public Locale | Returns: The most preferable supported locale, or null.Array of language specifiers, ordered with the most
preferable one at the front. For example, "en-ca" then "fr-ca",
followed by "zh_CN". languages)Chooses a client locale to use, using the first language specified in the list that is supported by this catalog. |
| private Locale | |
| public String | Returns: The message, localized as described above.The locale of the message to use. If this is null,
the default locale will be used. locale, String The ID of the message to use. messageId)Get a message localized to the specified locale, using the message ID and package name if no message is available. |
| public String | Returns: The message, localized as described above.The locale of the message to use. If this is null,
the default locale will be used. locale, String The ID of the message format to use. messageId, Object[] Used when formatting the message. Objects in
this list are turned to strings if they are not Strings, Numbers,
or Dates (that is, if MessageFormat would treat them as errors). parameters)Format a message localized to the specified locale, using the message ID with its package name if none is available. |
| public boolean | Returns: True iff the language of that locale is supported.A standard Java locale name, using two character
language codes optionally suffixed by country codes. localeName)Returns true iff the specified locale has explicit language support. |
| bundleName | back to summary |
|---|---|
| private String bundleName | |
| cache | back to summary |
|---|---|
| private HashMap<String, Boolean> cache | |
| MessageCatalog | back to summary |
|---|---|
| protected MessageCatalog(Class<?> packageMember) Create a message catalog for use by classes in the same package as the specified class. This uses Messages resource bundles in the resources sub-package of class passed as a parameter.
| |
| MessageCatalog | back to summary |
|---|---|
| private MessageCatalog(Class<?> packageMember, String bundle) Create a message catalog for use by classes in the same package as the specified class. This uses the specified resource bundle name in the resources sub-package of class passed as a parameter; for example, resources.Messages. | |
| canonicalize | back to summary |
|---|---|
| private String[] canonicalize(String[] languages) | |
| chooseLocale | back to summary |
|---|---|
| public Locale chooseLocale(String[] languages) Chooses a client locale to use, using the first language specified in the list that is supported by this catalog. If none of the specified languages is supported, a null value is returned. Such a list of languages might be provided in an HTTP/1.1 "Accept-Language" header field, or through some other content negotiation mechanism. The language specifiers recognized are RFC 1766 style ("fr" for all French, "fr-ca" for Canadian French), although only the strict ISO subset (two letter language and country specifiers) is currently supported. Java-style locale strings ("fr_CA") are also supported.
| |
| getLocale | back to summary |
|---|---|
| private Locale getLocale(String localeName) | |
| getMessage | back to summary |
|---|---|
| public String getMessage(Locale locale, String messageId) Get a message localized to the specified locale, using the message ID and package name if no message is available. The locale is normally that of the client of a service, chosen with knowledge that both the client and this server support that locale. There are two error cases: first, when the specified locale is unsupported or null, the default locale is used if possible; second, when no bundle supports that locale, the message ID and package name are used. | |
| getMessage | back to summary |
|---|---|
| public String getMessage(Locale locale, String messageId, Object[] parameters) Format a message localized to the specified locale, using the message ID with its package name if none is available. The locale is normally the client of a service, chosen with knowledge that both the client server support that locale. There are two error cases: first, if the specified locale is unsupported or null, the default locale is used if possible; second, when no bundle supports that locale, the message ID and package name are used.
| |
| isLocaleSupported | back to summary |
|---|---|
| public boolean isLocaleSupported(String localeName) Returns true iff the specified locale has explicit language support. For example, the traditional Chinese locale "zh_TW" has such support if there are message bundles suffixed with either "zh_TW" or "zh". This method is used to bypass part of the search path mechanism
of the
| |