|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.crimson.util.MessageCatalog
This class provides support for multi-language string lookup, as needed to localize messages from applications supporting multiple languages at the same time. One class of such applications is network services, such as HTTP servers, which talk to clients who may not be from the same locale as the server. This class supports a form of negotiation for the language used in presenting a message from some package, where both user (client) preferences and application (server) support are accounted for when choosing locales and formatting messages.
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.
Locale
,
ListResourceBundle
,
MessageFormat
Field Summary | |
private String |
bundleName
|
private Hashtable |
cache
|
Constructor Summary | |
protected |
MessageCatalog(Class packageMember)
Create a message catalog for use by classes in the same package as the specified class. |
private |
MessageCatalog(Class packageMember,
String bundle)
Create a message catalog for use by classes in the same package as the specified class. |
Method Summary | |
private String[] |
canonicalize(String[] languages)
|
Locale |
chooseLocale(String[] languages)
Chooses a client locale to use, using the first language specified in the list that is supported by this catalog. |
private Locale |
getLocale(String localeName)
|
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. |
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. |
boolean |
isLocaleSupported(String localeName)
Returns true iff the specified locale has explicit language support. |
private String |
packagePrefix(String messageId)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private String bundleName
private Hashtable cache
Constructor Detail |
protected MessageCatalog(Class packageMember)
packageMember
- Class whose package has localized messagesprivate MessageCatalog(Class packageMember, String bundle)
packageMember
- Class whose package has localized messagesbundle
- Name of a group of resource bundlesMethod Detail |
public String getMessage(Locale locale, String messageId)
locale
- The locale of the message to use. If this is null,
the default locale will be used.messageId
- The ID of the message to use.
private String packagePrefix(String messageId)
public String getMessage(Locale locale, String messageId, Object[] parameters)
locale
- The locale of the message to use. If this is null,
the default locale will be used.messageId
- The ID of the message format to use.parameters
- 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).
MessageFormat
public Locale chooseLocale(String[] languages)
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.
languages
- Array of language specifiers, ordered with the most
preferable one at the front. For example, "en-ca" then "fr-ca",
followed by "zh_CN".
Locale
private String[] canonicalize(String[] languages)
private Locale getLocale(String localeName)
public boolean isLocaleSupported(String localeName)
This method is used to bypass part of the search path mechanism
of the ResourceBundle
class, specifically the parts which
force use of default locales and bundles. Such bypassing is required
in order to enable use of a client's preferred languages. Following
the above example, if a client prefers "zh_TW" but can also accept
"ja", this method would be used to detect that there are no "zh_TW"
resource bundles and hence that "ja" messages should be used. This
bypasses the ResourceBundle mechanism which will return messages in
some other locale (picking some hard-to-anticipate default) instead
of reporting an error and letting the client choose another locale.
localeName
- A standard Java locale name, using two character
language codes optionally suffixed by country codes.
Locale
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |