Using the Java Locale Object to Internationalize Your Application | 2 | WebReference

Using the Java Locale Object to Internationalize Your Application | 2

By Rob Gravelle


[next]

Internationalization is a process whereby information is tailored to the user's specific geographical, political, or cultural preference. In the Java SE Platform, internationalization support is integrated into the java.util.Locale class, as well as other classes and packages that provide language- or culture-dependent functionality. This article describes the Locale class and its important features, as well as touches upon some of the important classes that are affected by locale.

Supported Locales

The Java SE 6 runtime environment (JRE) for Windows may be installed as a complete international version or as a European languages version. The JRE installer by default installs a European languages version if it recognizes that the host operating system supports only European languages. Only if the installer recognizes that any other language is needed, or the user requests support for non-European languages in a customized installation, is a complete international version installed.

The JRE for Solaris and Linux and the JDK for any platform are always installed as complete international versions. Here is a list of supported locales in JDK 6:

Language

Country

Locale ID

Albanian

Albania

sq_AL

Arabic

Algeria

ar_DZ

Arabic

Bahrain

ar_BH

Arabic

Egypt

ar_EG

Arabic

Iraq

ar_IQ

Arabic

Jordan

ar_JO

Arabic

Kuwait

ar_KW

Arabic

Lebanon

ar_LB

Arabic

Libya

ar_LY

Arabic

Morocco

ar_MA

Arabic

Oman

ar_OM

Arabic

Qatar

ar_QA

Arabic

Saudi Arabia

ar_SA

Arabic

Sudan

ar_SD

Arabic

Syria

ar_SY

Arabic

Tunisia

ar_TN

Arabic

United Arab Emirates

ar_AE

Arabic

Yemen

ar_YE

Belarusian

Belarus

be_BY

Bulgarian

Bulgaria

bg_BG

Catalan

Spain

ca_ES

Chinese (Simplified)

China

zh_CN

Chinese (Simplified)

Singapore

zh_SG

Chinese (Traditional)

Hong Kong

zh_HK

Chinese (Traditional)

Taiwan

zh_TW

Croatian

Croatia

hr_HR

Czech

Czech Republic

cs_CZ

Danish

Denmark

da_DK

Dutch

Belgium

nl_BE

Dutch

Netherlands

nl_NL

English

Australia

en_AU

English

Canada

en_CA

English

India

en_IN

English

Ireland

en_IE

English

Malta

en_MT

English

New Zealand

en_NZ

English

Philippines

en_PH

English

Singapore

en_SG

English

South Africa

en_ZA

English

United Kingdom

en_GB

English

United States

en_US

Estonian

Estonia

et_EE

Finnish

Finland

fi_FI

French

Belgium

fr_BE

French

Canada

fr_CA

French

France

fr_FR

French

Luxembourg

fr_LU

French

Switzerland

fr_CH

German

Austria

de_AT

German

Germany

de_DE

German

Luxembourg

de_LU

German

Switzerland

de_CH

Greek

Cyprus

el_CY

Greek

Greece

el_GR

Hebrew

Israel

iw_IL

Hindi

India

hi_IN

Hungarian

Hungary

hu_HU

Icelandic

Iceland

is_IS

Indonesian

Indonesia

in_ID

Irish

Ireland

ga_IE

Italian

Italy

it_IT

Italian

Switzerland

it_CH

Japanese (Gregorian calendar)

Japan

ja_JP

Japanese (Imperial calendar)

Japan

ja_JP_JP

Korean

South Korea

ko_KR

Latvian

Latvia

lv_LV

Lithuanian

Lithuania

lt_LT

Macedonian

Macedonia

mk_MK

Malay

Malaysia

ms_MY

Maltese

Malta

mt_MT

Norwegian (Bokmål)

Norway

no_NO

Norwegian (Nynorsk)

Norway

no_NO_NY

Polish

Poland

pl_PL

Portuguese

Brazil

pt_BR

Portuguese

Portugal

pt_PT

Romanian

Romania

ro_RO

Russian

Russia

ru_RU

Serbian (Cyrillic)

Bosnia and Herzegovina

sr_BA

Serbian (Cyrillic)

Montenegro

sr_ME

Serbian (Cyrillic)

Serbia

sr_RS

Slovak

Slovakia

sk_SK

Slovenian

Slovenia

sl_SI

Spanish

Argentina

es_AR

Spanish

Bolivia

es_BO

Spanish

Chile

es_CL

Spanish

Colombia

es_CO

Spanish

Costa Rica

es_CR

Spanish

Dominican Republic

es_DO

Spanish

Ecuador

es_EC

Spanish

El Salvador

es_SV

Spanish

Guatemala

es_GT

Spanish

Honduras

es_HN

Spanish

Mexico

es_MX

Spanish

Nicaragua

es_NI

Spanish

Panama

es_PA

Spanish

Paraguay

es_PY

Spanish

Peru

es_PE

Spanish

Puerto Rico

es_PR

Spanish

Spain

es_ES

Spanish

United States

es_US

Spanish

Uruguay

es_UY

Spanish

Venezuela

es_VE

Swedish

Sweden

sv_SE

Thai (Western digits)

Thailand

th_TH

Thai (Thai digits)

Thailand

th_TH_TH

Turkish

Turkey

tr_TR

Ukrainian

Ukraine

uk_UA

Vietnamese

Vietnam

vi_VN

Retrieving Information from the Current Locale

You can query a Locale instance for information about itself via several get methods. Use getCountry() to get the ISO country code and getLanguage() to get the ISO language code. To get the name of the country that is displayed to the user, you can use getDisplayCountry(). Similarly, you can use getDisplayLanguage() to get the name of the displayed language. All of the getDisplayXXX() methods are locale-sensitive and have two versions: one that uses the default locale and one that accepts the locale as an argument.

Querying methods offered by the Locale class include:

  • getDefault(): This method returns the default locale of the JVM.
  • getLanguage(): This method returns the language code of the current locale. Language codes are the lowercase two-letter codes as defined by ISO-639.
  • getCountry(): This method returns the country code of the current locale. Country codes are the uppercase two-letter codes as defined by ISO-3166.
  • getVariant(): This method returns the variant code for the current locale. (See below for an explanation of the variant property.)
  • getDisplayName(): This method returns a name for the current locale that is appropriate for display to the user.

The following example would print information about the default locale:

The output of this example would look something like this:

Each class that performs locale-sensitive operations allows you to get all the available objects of that type. For instance, you can sift through these objects by language, country, or variant, and use the display names to present a menu to the user. Locale-sensitive classes must implement a minimum of these three static class methods:

  • public static Locale[] getAvailableLocales()
  • public static String getDisplayName(Locale objectLocale, Locale displayLocale)
  • public static final String getDisplayName(Locale objectLocale) : This method will throw a MissingResourceException if the locale is not one of the available locales.

Here's some code that would print the same information as above for all available locales:

The above code would display something like the following, and far more (output truncated for brevity)!

Creating a New Locale

A Locale object can be created using one of three constructors:

  • Locale(String language)
  • Locale(String language, String country)
  • Locale(String language, String country, String variant)

The language argument is a valid ISO language code, which is composed of the lowercase, two-letter codes as defined by ISO-639.

Here are a few language code examples:

Language

Code

Arabic

ar

German

de

English

en

Spanish

es

Japanese

ja

Hebrew

iw


[next]