JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

java.util.Locale - Localization and Internationalization

This section provides a tutorial example on how to use java.util.Locale class to verify the default locale and list all supported locales. A locale is an identification of a combination of human language and country.

Internationalization: A 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)
   es, , , Spanish
   es, AR, , Spanish (Argentina)
   es, BO, , Spanish (Bolivia)
   es, CL, , Spanish (Chile)
   es, CO, , Spanish (Colombia)
   es, CR, , Spanish (Costa Rica)
   es, DO, , Spanish (Dominican Republic)
   es, EC, , Spanish (Ecuador)
   es, ES, , Spanish (Spain)
   es, GT, , Spanish (Guatemala)
   es, HN, , Spanish (Honduras)
   es, MX, , Spanish (Mexico)
   es, NI, , Spanish (Nicaragua)
   es, PA, , Spanish (Panama)
   es, PE, , Spanish (Peru)
   es, PR, , Spanish (Puerto Rico)
   es, PY, , Spanish (Paraguay)
   es, SV, , Spanish (El Salvador)
   es, UY, , Spanish (Uruguay)
   es, VE, , Spanish (Venezuela)
   et, , , Estonian
   et, EE, , Estonian (Estonia)
   fi, , , Finnish
   fi, FI, , Finnish (Finland)
   fr, , , French
   fr, BE, , French (Belgium)
   fr, CA, , French (Canada)
   fr, CH, , French (Switzerland)
   fr, FR, , French (France)
   fr, LU, , French (Luxembourg)
   hi, IN, , Hindi (India)
   hr, , , Croatian
   hr, HR, , Croatian (Croatia)
   hu, , , Hungarian
   hu, HU, , Hungarian (Hungary)
   is, , , Icelandic
   is, IS, , Icelandic (Iceland)
   it, , , Italian
   it, CH, , Italian (Switzerland)
   it, IT, , Italian (Italy)
   iw, , , Hebrew
   iw, IL, , Hebrew (Israel)
   ja, , , Japanese
   ja, JP, , Japanese (Japan)
   ko, , , Korean
   ko, KR, , Korean (South Korea)
   lt, , , Lithuanian
   lt, LT, , Lithuanian (Lithuania)
   lv, , , Latvian (Lettish)
   lv, LV, , Latvian (Lettish) (Latvia)
   mk, , , Macedonian
   mk, MK, , Macedonian (Macedonia)
   nl, , , Dutch
   nl, BE, , Dutch (Belgium)
   nl, NL, , Dutch (Netherlands)
   no, , , Norwegian
   no, NO, , Norwegian (Norway)
   no, NO, NY, Norwegian (Norway,Nynorsk)
   pl, , , Polish
   pl, PL, , Polish (Poland)
   pt, , , Portuguese
   pt, BR, , Portuguese (Brazil)
   pt, PT, , Portuguese (Portugal)
   ro, , , Romanian
   ro, RO, , Romanian (Romania)
   ru, , , Russian
   ru, RU, , Russian (Russia)
   sh, , , Serbo-Croatian
   sh, YU, , Serbo-Croatian (Yugoslavia)
   sk, , , Slovak
   sk, SK, , Slovak (Slovakia)
   sl, , , Slovenian
   sl, SI, , Slovenian (Slovenia)
   sq, , , Albanian
   sq, AL, , Albanian (Albania)
   sr, , , Serbian
   sr, YU, , Serbian (Yugoslavia)
   sv, , , Swedish
   sv, SE, , Swedish (Sweden)
   th, , , Thai
   th, TH, , Thai (Thailand)
   th, TH, TH, Thai (Thailand,TH)
   tr, , , Turkish
   tr, TR, , Turkish (Turkey)
   uk, , , Ukrainian
   uk, UA, , Ukrainian (Ukraine)
   zh, , , Chinese
   zh, CN, , Chinese (China)
   zh, HK, , Chinese (Hong Kong)
   zh, TW, , Chinese (Taiwan)
   en, , , English
   en, US, , English (United States)

Sorry about the length of the output. But it might be useful as a reference source.

The output shows that I can make up a new locale with any combination of language and country, like "en, CN, , , English (China)", or "ll, CC, , , ll (CC)".

Sections in This Chapter

java.util.Locale - Localization and Internationalization

Locale Sensitive Operations

java.util.ResourceBundle - Resource Bundle Abstract Class

java.util.ListResourceBundle - Resource Bundles as Lists

java.util.PropertyResourceBundle - Resource Bundles as Properties Files

java.util.ResourceBundle.getBundle() - Retrieving Resource Bundles

Dr. Herong Yang, updated in 2008
java.util.Locale - Localization and Internationalization