+

Search Tips   |   Advanced Search

Create a formatter instance


Perform this task to set localization values for strings in an application component.

Server programs typically create LocalizableTextFormatter instances that are sent to clients as the result of some operation; clients format the objects at the appropriate time. Less typically, client programs create LocalizableTextFormatter objects locally.

 

  1. If needed for the application, write our own formatter class.

    See about implementation, see LocalizableTextFormatter class.

  2. In application code, call the appropriate constructor for the formatter class and set required localization values. Some localization values, such as resource bundle name, key and formatting application, must be set, either through a constructor or soon after construction. Other localization values can be set only as needed.

    See about the API, see the related reference.

 

Example

The following code creates a LocalizableTextFormatter instance by using the default constructor and then sets the required localization values:

import com.ibm.websphere.i18n.localizabletext.LocalizableException;
 import com.ibm.websphere.i18n.localizabletext.LocalizableTextFormatter;
 import java.util.Locale;
 public void drawAccountNumberGUI(String accountType) {
   ...
   LocalizableTextFormatter ltf = new LocalizableTextFormatter();
   ltf.setPatternKey("accountNumber");
   ltf.setResourceBundleName("BankingSample.BankingResources");
   ltf.setApplicationName("BankingSample");
   ...
}

The line of code in boldface exploits default behavior of the Java platform. By default, the Java platform looks first for a subclass of java.util.ResourceBundle called BankingResources. When none is found, the Java platform looks for a valid properties file of the same name. In this continuing example, a properties file is found.

The application that is requesting a localized message can specify the locale and time zone for message formatting, or the application can use the default values set for the Java virtual machine. For example, a GUI can enable users to select the language in which to display the interface. A default value must be set initially so that the GUI can be created properly when the application first starts, but users can then change the locale for the GUI to suit their needs.

The following code shows how to change the locale used by an application based on the selection of a menu item:

import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
... import java.util.Locale;
 public void actionPerformed(ActionEvent event) {
   String action = event.getActionCommand();
   ...
   if (action.equals("en_us")) {
      applicationLocale = new Locale("en", "US");
      ...


if (action.equals("de_de")) { applicationLocale = new Locale("de", "DE"); ...

if (action.equals("fr_fr")) { applicationLocale = new Locale("fr", "FR"); ...

... }

See "Generating localized text."

 

Next steps

Set optional localization values.

 

Related tasks


Generating localized text
Composing language-specific strings

 

Related


LocalizableTextFormatter class