Create log resource bundles and message files

 

Overview

Every method that accepts messages will localize those messages. The mechanism for providing localized messages is the Resource Bundle support provided by the IBM Developer Kit, Java Technology Edition. If you are not familiar with resource bundles as implemented by the Developer's Kit, you can get more information from various texts, or by reading the Javadoc for the java.util.ResourceBundle, java.util.ListResourceBundle and java.util.PropertyResourceBundle classes, as well as the java.text.MessageFormat class.

The PropertyResourceBundle is the preferred mechanism to use.

We can forward messages that are written to the internal WAS logs to other processes for display. For example, messages displayed on the administrator console, which can be running in a different location than the server process, can be localized using the late binding process. Late binding means that WebSphere Application Server does not localize messages when they are logged, but defers localization to the process that displays the message.

To properly localize the message, the displaying process must have access to the resource bundle where the message text is stored. This means that package the resource bundle separately from the application, and install it in a location where the viewing process can access it.

By default, the WAS runtime localizes all the messages when they are logged. This eliminates the need to pass a .jar file to the application, unless we need to localize in a different location. However, use the early binding technique to localize messages as they are logged. An application that uses early binding must localize the message before logging it. The application looks up the localized text in the resource bundle and formats the message. Use the early binding technique to package the application's resource bundles with the application.

To create a resource bundle, perform the following steps.

 

Procedure

  1. Create a text properties file that lists message keys and the corresponding messages. The properties file must have the following characteristics:

    • Each property in the file is terminated with a line-termination character.

    • If a line contains only white space, or if the first non-white space character of the line is the pound sign symbol (#) or exclamation mark (!), the line is ignored. The # and ! characters can therefore be used to put comments into the file.

    • Each line in the file, unless it is a comment or consists only of white space, denotes a single property. A backslash (\) is treated as the line-continuation character.

    • The syntax for a property file consists of a key, a separator, and an element. Valid separators include the equal sign (=), colon (:), and white space ( ).

    • The key consists of all characters on the line from the first non-white space character to the first separator. Separator characters can be included in the key by escaping them with a backslash (\), but doing this is not recommended, because escaping characters is error prone and confusing. It is instead recommended that you use a valid separator character that does not appear in any keys in the properties file.

    • White space after the key and separator is ignored until the first non-white space character is encountered. All characters remaining before the line-termination character define the element.

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

  2. The file can then be translated into localized versions of the file with language-specific file names (for example, a file named DefaultMessages.properties can be translated into DefaultMessages_de.properties for German and DefaultMessages_ja.properties for Japanese).

  3. When the translated resource bundles are available, put the bundle in a directory that is part of the application's classpath.

  4. When a message logger is obtained from the log manager, it can be configured to use a particular resource bundle. Messages logged via the Logger() API will use this resource bundle when message localization is performed. At run time, the user's locale setting 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.

  5. If the message loggers msg() method is called, a resource bundle name must be explicitly provided.

 

What to do next

The application locates the resource bundle based on the file's location relative to any directory in the classpath. For instance, if the property resource bundle named DefaultMessages.properties is located in the baseDir/subDir1/subDir2/resources directory and baseDir is in the class path, the name subdir1.subdir2.resources.DefaultMessage is passed to the message logger to identify the resource bundle.

 

See also


Developing log resource bundles