Creating resource bundles and message files

This section provides an overview of how to create resource bundles that can be translated to provide localized messages in WebSphere applications. The Java programming language provides the java.util.ResourceBundle class and its subclasses, java.util.ListResourceBundle and java.util.PropertyResourceBundle, to enable national language support for applications. The ResourceBundle class is used in conjunction with the java.text.MessageFormat class to provide localized (translated) text support. See the Java documentation for a full discussion of the ResourceBundle and MessageFormat classes.

ResourceBundle is a class that encapsulates the retrieval of text. Entries in a resource bundle consist of message keys and their corresponding message text. When a resource bundle is translated, only the message text is translated into the national language. The translated resource bundles are packaged together and shipped with the application to provide localized messages.

This section discusses how to create resource bundles in the form of text properties files that can be accessed by PropertyResourceBundle. You can also create resource bundles by using a Java class that extends ListResourceBundle. The class encapsulates the mapping of keys to values by using arrays. For information on creating resource bundles by using ListResourceBundle, see the Java documentation.

The simplest way to create a resource bundle is to create a text properties file that lists message keys and the corresponding messages. The properties file must have the following characteristics:

See the Java documentation for the java.util.Properties class for a full description of the syntax and construction of properties files.

The following example shows a properties file named DefaultMessages.properties.

Figure 1. Sample resource bundle


# Contents of DefaultMessages.properties file
MSG_KEY_00=A message with no substitution parameters.
MSG_KEY_01=A message with one substitution parameter: parm1={0}
MSG_KEY_02=A message with two substitution parameters: parm1={0}, parm2={1}
MSG_KEY_03=A message with three substitution parameters: parm1={0}, parm2={1}, \
parm3={2}

This file can then be translated into localized versions of the file (for example, DefaultMessages_de.properties for German and DefaultMessages_ja.properties for Japanese). When the translated resource bundles are available, they are written to a system-managed persistent storage medium. Resource bundles are then used to convert the messages into the requested national language and locale. When a message logger is obtained from the JRas manager, it can be configured with a default resource bundle. At run time, the user's locale is used to determine the properties file from which to extract the message specified by a message key, thus ensuring that the message is delivered in the correct language. If a default resource bundle is not specified, the msg method of the RASIMessageLogger interface can be used to specify a resource bundle name.

The application locates the resource bundle based on the file's location in the directory structure. For instance, if the resource bundle is located in the baseDir/subDir1/subDir2/resources directory and baseDir is in the classpath, the name subDir1.subDir2.resources.DefaultMessage is passed to the message logger to identify the resource bundle.