Internationalization Topics

Java Locale Example

This example contains a Java applet and associated HTML that illustrate how Java and the IBM ICU package can be used to format dates, time, numeric and currency data for various locales. The example also illustrates how information about supported locales can be obtained and displayed.

The HTML source is:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>

<p>&nbsp;</p>
<p><applet code="Dates.class" align="baseline" width="400"
height="400"></applet></p>

<br>

</BODY>
</HTML>

Please ignore the fact that the header violates all internationalization rules.

The Java source is:

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.applet.*;
/*
<applet code="Dates" width=400 height=400>
</applet>
*/

public class Dates extends Applet implements ActionListener, ItemListener {
  public void init() {
    DateFormatTest();
  }

  void DateFormatTest() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.EAST;
    int i = 0;
    add(new Label("User Lang"), gbc, 0, i, 1, 1); i++;
    add(new Label("Locale"), gbc, 0, i, 1, 1); i++;
    add(new Label("Country"), gbc, 0, i, 1, 1); i++;
    add(new Label("Language"), gbc, 0, i, 1, 1); i++;
    add(new Label("Region Code"), gbc, 0, i, 1, 1); i++;
    add(new Label("ISO ll-CC Code"), gbc, 0, i, 1, 1); i++;
    add(new Label("toString"), gbc, 0, i, 1, 1); i++;
    add(new Label("Date"), gbc, 0, i, 1, 1); i++;
    add(new Label("Time"), gbc, 0, i, 1, 1); i++;
    add(new Label("Numeric"), gbc, 0, i, 1, 1); i++;
    add(new Label("Currency"), gbc, 0, i, 1, 1);

    gbc.anchor = GridBagConstraints.WEST;
    i = 0;
    add(userLanguage, gbc, 1, i, 2, 1); i++;
    add(localeChoice, gbc, 1, i, 2, 1); i++;
    add(countryText, gbc, 1, i, 2, 1); i++;
    add(languageText, gbc, 1, i, 2, 1); i++;
    add(regionText, gbc, 1, i, 2, 1); i++;
    add(isoText, gbc, 1, i, 2, 1); i++;
    add(toStringText, gbc, 1, i, 2, 1); i++;
    add(dateText, gbc, 1, i, 2, 1); i++; int dT=i-1;
    add(timeText, gbc, 1, i, 2, 1); i++; int tT=i-1;
    add(numericText, gbc, 1, i, 2, 1); i++;
    add(currencyText, gbc, 1, i, 2, 1);

    add(dateStyleChoice, gbc, 3, dT, 2, 1);
    add(timeStyleChoice, gbc, 3, tT, 2, 1);

    locales = DateFormat.getAvailableLocales();
    for (i = 0; i < locales.length; i++)
      localeChoice.add(locales[i].getDisplayName());
      localeChoice.select(
      Locale.getDefault().getDisplayName());
      currentDate = new Date();
      currentTime = new Date();
      updateDisplay();

      localeChoice.addItemListener(this);
      dateStyleChoice.addItemListener(this);
      timeStyleChoice.addItemListener(this);
    }

  public void add(Component c, GridBagConstraints gbc,int x, int y, int w, int h) {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    add(c, gbc);
  }

  public void actionPerformed(ActionEvent evt) {
  }

  public void itemStateChanged(ItemEvent evt) {
    if (evt.getSource() instanceof Choice) {
      if (evt.getStateChange() == ItemEvent.SELECTED) {
        updateDisplay();
      }
    }
  }

  public void updateDisplay() {
    Locale currentLocale = locales[localeChoice.getSelectedIndex()];
    String c = currentLocale.getDisplayCountry();
    countryText.setText(c);

    String l = currentLocale.getDisplayLanguage();
    languageText.setText(l);

    String r = currentLocale.getCountry();
    regionText.setText(r);

    String iso = currentLocale.getISO3Language() + " - " +
    currentLocale.getISO3Country();
    isoText.setText(iso);

    String tS = currentLocale.toString();
    toStringText.setText(tS);

    int dateStyle = dateStyleChoice.getValue();
    currentDateFormat = DateFormat.getDateInstance(dateStyle, currentLocale);
    String d = currentDateFormat.format(currentDate);
    dateText.setText(d);

    int timeStyle = timeStyleChoice.getValue();
    currentTimeFormat = DateFormat.getTimeInstance(timeStyle, currentLocale);
    String t = currentTimeFormat.format(currentTime);
    timeText.setText(t);

    double number = 9876543.21;
    currentNumberFormat = NumberFormat.getNumberInstance(currentLocale);
    String n = currentNumberFormat.format(number);
    numericText.setText(n);

    currentNumberFormat = NumberFormat.getCurrencyInstance(currentLocale);
    n = currentNumberFormat.format(number);
    currencyText.setText(n);
  }

  private Locale[] locales;

  private Date currentDate;
  private Date currentTime;
  private DateFormat currentDateFormat;
  private DateFormat currentTimeFormat;
  private NumberFormat currentNumberFormat;

  private Choice localeChoice = new Choice();
  private EnumChoice2 dateStyleChoice =
    new EnumChoice2(DateFormat.class,
    new String[] { "Default", "Full", "Long",
    "Medium", "Short" });
  private EnumChoice2 timeStyleChoice
    = new EnumChoice2(DateFormat.class,
    new String[] { "Default", "Full", "Long",
    "Medium", "Short" });
  private TextField userLanguage = new TextField(30);
  private TextField dateText = new TextField(30);
  private TextField dateFormat = new TextField(30);
  private TextField timeText = new TextField(30);
  private TextField timeFormat = new TextField(30);
  private TextField parseText = new TextField(30);
  private TextField countryText = new TextField(20);
  private TextField languageText = new TextField(20);
  private TextField regionText = new TextField(20);
  private TextField isoText = new TextField(20);
  private TextField toStringText = new TextField(20);
  private TextField dateFormatText = new TextField(30);
  private TextField currencyText = new TextField(30);
  private TextField numericText = new TextField(30);
}

class EnumChoice2 extends Choice {

  public EnumChoice2(Class cl, String[] labels) {
    for (int i = 0; i < labels.length; i++) {
      String label = labels[i];
      String name = label.toUpperCase().replace(' ', '_');
      int value = 0;
      try {
        java.lang.reflect.Field f = cl.getField(name);
        value = f.getInt(cl);
      }
      catch(Exception e) {
        label = "(" + label + ")";
      }
      table.put(label, new Integer(value));
      add(label);
    }
    select(labels[0]);
  }

  public int getValue() {
    return ((Integer)table.get(getSelectedItem())).intValue();
  }

  private Hashtable table = new Hashtable();
}

A screen shot for the en_US locale:

Figure 1

A screen shot for the de_CH locale (German as used in Switzerland):

Figure 2

Displays for other locales can be obtained by selecting a locale from the Locale dropdown menu. Different Java-supported time and date displays can be obtained from the dropdown menus to the immediate right of the date and time displays. Figure 1 displays the en_US time in the Java default format while Figure 2 displays the time in the Java full format. This applet will display a different list of locales when run on IE, Netscape and the Sun platform, reflecting the different locale support of those platforms.