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

Sample programs listed in this chapter have been tested with JDK 1.3.1, 1.4.1 and 1.5.0.

Locale

Internationalization: An process that tailors information for the user based on his/her specific geographical, political, or cultural preference. For example, each time when I insert my Fleet Bank client card into a Fleet Bank ATM machine, it will greet me and display instructions in Chinese. This process of check my language preference setting and tailoring the display information my preferred language is called internationalization.

The Locale class, java.util.Locale, is class presenting a specific geographical, political, or cultural region, where information is displayed in a way different than other regions. For example, French speaking areas in Canada is a locale, and English speaking areas in Canada is another locale.

Major methods offered on Locale class:

  • getDefault(): Returns the default locale of the JVM.
  • getLanguage(): Returns the language code of this locale. Language codes are the lower-case two-letter codes as defined by ISO-639.
  • getCountry(): Returns the country code of this locale. Country codes are the upper-case two-letter codes as defined by ISO-3166.
  • Locale(l,c): Constructs a new locale with a given language and a given country.

The following program shows some interesting features of the Locale class:

/**
 * LocaleTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
class LocaleTest {
   public static void main(String[] a) {
      // get the default locale
      Locale l = Locale.getDefault();
      System.out.println("   Language, Country, Variant, Name");
      System.out.println("");
      System.out.println("Default locale: ");
      System.out.println("   "+l.getLanguage()+", "+l.getCountry()+", "
         +", "+l.getVariant()+", "+l.getDisplayName());
      // get a predefined locale
      l = Locale.CANADA_FRENCH;
      System.out.println("A predefined locale - Locale.CANADA_FRENCH:"); 
      System.out.println("   "+l.getLanguage()+", "+l.getCountry()+", "
         +", "+l.getVariant()+", "+l.getDisplayName());
      // define a new locale
      l = new Locale("en", "CN");
      System.out.println("User defined locale -Locale(\"en\",\"CN\"):");
      System.out.println("   "+l.getLanguage()+", "+l.getCountry()+", "
         +", "+l.getVariant()+", "+l.getDisplayName());
      // define another new locale
      l = new Locale("ll", "CC");
      System.out.println("User defined locale -Locale(\"ll\",\"CC\"):");
      System.out.println("   "+l.getLanguage()+", "+l.getCountry()+", "
         +", "+l.getVariant()+", "+l.getDisplayName());
      // get the supported locales
      Locale[] s = Locale.getAvailableLocales();
      System.out.println("Supported locales: ");
      for (int i=0; i<s.length; i++) {
         System.out.println("   "+s[i].getLanguage()+", "
            +s[i].getCountry()+", "+s[i].getVariant()+", "
            +s[i].getDisplayName());
      }
   }
}

Output:

   Language, Country, Variant, Name

Default locale:
   en, US, , , English (United States)
A predefined locale - Locale.CANADA_FRENCH:
   fr, CA, , , French (Canada)
User defined locale - Locale("en","CN"):
   en, CN, , , English (China)
User defined locale - Locale("ll","CC"):
   ll, CC, , , ll (CC)   
Supported locales:
   ar, , , Arabic
   ar, AE, , Arabic (United Arab Emirates)
   ar, BH, , Arabic (Bahrain)
   ar, DZ, , Arabic (Algeria)
   ar, EG, , Arabic (Egypt)
   ar, IQ, , Arabic (Iraq)
   ar, JO, , Arabic (Jordan)
   ar, KW, , Arabic (Kuwait)
   ar, LB, , Arabic (Lebanon)
   ar, LY, , Arabic (Libya)
   ar, MA, , Arabic (Morocco)
   ar, OM, , Arabic (Oman)
   ar, QA, , Arabic (Qatar)
   ar, SA, , Arabic (Saudi Arabia)
   ar, SD, , Arabic (Sudan)
   ar, SY, , Arabic (Syria)
   ar, TN, , Arabic (Tunisia)
   ar, YE, , Arabic (Yemen)
   be, , , Byelorussian
   be, BY, , Byelorussian (Belarus)
   bg, , , Bulgarian
   bg, BG, , Bulgarian (Bulgaria)
   ca, , , Catalan
   ca, ES, , Catalan (Spain)
   cs, , , Czech
   cs, CZ, , Czech (Czech Republic)
   da, , , Danish
   da, DK, , Danish (Denmark)
   de, , , German
   de, AT, , German (Austria)
   de, CH, , German (Switzerland)
   de, DE, , German (Germany)
   de, LU, , German (Luxembourg)
   el, , , Greek
   el, GR, , Greek (Greece)
   en, AU, , English (Australia)
   en, CA, , English (Canada)
   en, GB, , English (United Kingdom)
   en, IE, , English (Ireland)
   en, IN, , English (India)
   en, NZ, , English (New Zealand)
   en, ZA, , English (South Africa)

(Continued on next part...)

Part:   1   2  3  4  5 

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