Create JRas resource bundles and message files
The WebSphere Application Server message logger provides the message and msg methods so the user can log localized messages. In addition, the message logger provides the textMessage method to log messages that are not localized. Applications can use either or both, as appropriate.
The JRas framework described in this task and its sub-tasks is deprecated. However, we can achieve similar results using Java logging.
The mechanism for providing localized messages is the resource bundle support provided by the IBM Developer Kit, Java Technology Edition. If we are not familiar with resource bundles as implemented by the Developer Kit, we can get more information from various texts, or by reading the API documentation for the java.util.ResourceBundle, java.util.ListResourceBundle and java.util.PropertyResourceBundle classes, as well as the java.text.MessageFormat class.
The PropertyResourceBundle class is the preferred mechanism to use. In addition, note that the JRas extensions do not support the extended formatting options such as {1, date} or {0, number, integer} provided by the MessageFormat class.
We can forward messages that are written to the internal WebSphere Application Server logs to other processes for display. For example, messages that are displayed on the 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 WAS 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. We must package the resource bundle separately from the application, and install it in a location where the viewing process can access it. If we do not want to take these steps, we can use the early binding technique to localize messages as they are logged.
The two techniques are described as follows:
- Early binding
- The application must localize the message before logging it. The application looks up the localized text in the resource bundle and formats the message. When formatting is complete, the application logs the message using the textMessage method. Use this technique to package the application resource bundles with the application.
- Late binding
- The application can choose to have the WAS run time localize the message in the process where it displays. Using this technique, the resource bundles are packaged in a stand-alone .jar file, separately from the application. We must then install the resource bundle .jar file on every machine in the installation from which an console or log viewing program might be run. We must install the .jar file in a directory that is part of the extensions class path. In addition, if you forward logs to IBM service, you must also forward the .jar file containing the resource bundles.
To create a resource bundle, perform the following steps.
- 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 number 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 using this approach is not recommended because escaping characters is error prone and confusing. Instead, use a valid separator character that does not display 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 that remain 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.
- Translate the file into localized versions of the file with language-specific file names for example, the DefaultMessages.properties file can be translated into DefaultMessages_de.properties for German and DefaultMessages_ja.properties for Japanese.
- When the translated resource bundles are available, write them to a system-managed persistent storage medium. Resource bundles are used to convert the messages into the requested national language and locale.
- When a message logger is obtained from the JRas manager, configure the logger to use a particular resource bundle. Messages logged through the message API 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, ensuring that the message is delivered in the correct language.
- If the message loggers msg method is called, explicitly identify a resource bundle name.
What to do next
The application locates the resource bundle based on the file location relative to any directory in the class path. For instance, if the DefaultMessages.properties property resource bundle is 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.
Subtopics
- JRas resource bundles
We can create resource bundles in several ways. The best and easiest way is to create a properties file that supports a PropertiesResourceBundle resource bundle. This sample shows how to create such a properties file.