Chapter 117. How to register message bundles

117.1. How to create a message bundle
117.2. How to register a message bundle

Imagine that you write an extension to OGSA-DAI (e.g. a new activity or data resource) that logs a message when it has been initialised. If you log a plain message saying "Hello World!" this will only be understood if the user knows English.

OGSA-DAI provides a mechanism for you to register your own messages (a so-called message bundle). This is useful if you wish to enable internationalization for your OGSA-DAI components without editing OGSA-DAI's own message bundle or source code.

117.1. How to create a message bundle

A message bundle is a simple Java properties file. This is a list of what are termed message keys which map to strings in whatever language you want. For example, a message bundle my_messages.properties may contain the following message keys and messages.

com.example.HELLO_WORLD_MESSAGE=Hello World!
com.example.MISSING_INPUT_ERROR=Missing user input
com.example.PARAMETER_VALUE_OUT_OF_BOUNDS_ERROR=Value {0} exceeds permitted range of this parameter
[Note]Note
Note that the file extension must be .properties or it will not be picked up by Java.

Each string which a message key maps to may contain placeholders for arguments. Arguments for these placeholders are provided in-code whenever messages are logged (or, for messages associated with errors, whenever exceptions are logged). The placeholders in a single string must range in order from {0} to {N}. The third entry of the example above contains an example of a message with placeholders.

Your message bundles, such as my_messages.properties must be available in the CLASSPATH of your OGSA-DAI server.

This will be typically in

Alternatively your message bundle may be included within a JAR and added to the OGSA-DAI server like any other JAR.

117.2. How to register a message bundle

To make your message bundle available within the OGSA-DAI server you need to register your message bundle with the OGSA-DAI message loader:

uk.org.ogsadai.common.msgs.MessageLoader

For example:

MessageLoader.registerMessageBundle("my_messages", 
                                    java.util.Locale.getDefault());

The first argument is the base name of the message bundle. If the message bundle is within a package in a JAR then this must be the package-qualified name. The second argument specifies the locale for which the message bundle is desired. This can be the default locale, as in the example above, or any other locale you wish to use. The message loader then tries to load the appropriate resource bundle from the CLASSPATH

Java resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, if there is a German message bundle provided for the family with base name my_messages the file would be named my_messages_de.properties. Whenever the German locale is requested messages from this bundle will be loaded.

Please see Sun's Java documentation for a complete description of resource bundles.