JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Internationalization

Part:   1  2  3   4  5 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

The following program shows how to internationalize the formatting operation of dates, numbers and currencies:

/**
 * FormatLocaleTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
import java.text.*;
class FormatLocaleTest {
   public static void main(String[] a) {
      showDateFormat();
      showNumberFormat();
   }
   public static void showDateFormat() {
      Date now = new Date(); // the current time
      DateFormat df;
      SimpleDateFormat sdf;
      System.out.println("Default date format:");
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG);
      System.out.println("   Locale(Default): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.FRANCE);
      System.out.println("   Locale(France): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.FRENCH);
      System.out.println("   Locale(French): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.GERMAN);
      System.out.println("   Locale(German): " + df.format(now));
      System.out.println("Customized date format:");
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
      System.out.println("   Locale(Default): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
         Locale.FRENCH);
      System.out.println("   Locale(French): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
         Locale.GERMAN);
      System.out.println("   Locale(German): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
         Locale.CHINESE);
      System.out.println("   Locale(Chinese): " + sdf.format(now));
   }
   public static void showNumberFormat() {
      double num = -1234.5678;
      NumberFormat nf;
      System.out.println("Number format:");
      nf = NumberFormat.getNumberInstance();
      System.out.println("   Locale(Default): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.FRANCE);
      System.out.println("   Locale(France): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.FRENCH);
      System.out.println("   Locale(French): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.JAPAN);
      System.out.println("   Locale(Japanese): "+ nf.format(num));

      System.out.println("Currency format:");
      nf = NumberFormat.getCurrencyInstance();
      System.out.println("   Locale(Default): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
      System.out.println("   Locale(France): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.FRENCH);
      System.out.println("   Locale(French): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.JAPAN);
      System.out.println("   Locale(Japanese): "+ nf.format(num));
   }
}

Output:

Default date format:
   Locale(Default): November 1, 2002 9:45:44 AM EST
   Locale(France): 1 novembre 2002 09:45:44 EST
   Locale(French): 1 novembre 2002 09:45:44 EST
   Locale(German): 1. November 2002 09:45:44 EST
Customized date format:
   Locale(Default): Fri Nov 01 09:45:44 EST 2002
   Locale(French): ven. nov. 01 09:45:44 EST 2002
   Locale(German): Fr Nov 01 09:45:44 EST 2002
   Locale(Chinese): ??? ??? 01 09:45:44 EST 2002
Number format:
   Locale(Default): -1,234.568
   Locale(France): -1á234,568
   Locale(French): -1á234,568
   Locale(Japanese): -1,234.568
Currency format:
   Locale(Default): ($1,234.57)
   Locale(France): -1á234,57 F
   Locale(French): -1á234,57 ñ
   Locale(Japanese): -?1,235

Note that:

  • When the locale is changed, the default formatter not only changes the language, but also changes the pattern.
  • The "?" in the output of Chinese locale is caused by the console window not able to display the Chinese characters.
  • The currency formatter is sensitive to not only the language, but also to the country.
  • The precision is also adjusted in the default currency formatter for Japanese.

Internationalization with Resource Bundles

The ResourceBundle class, java.util.ResourceBundle, is an abstract class providing a foundation for subclasses to represent a specific map of resource keys and the associated localized resource objects.

Resource bundles are commonly used to internationalize the text messages in an application, where a key name is assigned to each individula text message. There are 3 ways to map the keys to the localized text messages:

  • Implement a series of subclasses of ResourceBundle class
  • Implement a series of subclasses of ListResourceBundle class
  • Implement a series of property files, and let the system to automatically create objects of PropertyResourceBundle class

The following sample classes and demo program illustrate those different ways.

Creating Resource Bundle with ResourceBundle Classes: ErrorMessage

(Continued on next part...)

Part:   1  2  3   4  5 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Internationalization