Locales


When you look at an application that is adapted to an international market, the most obvious difference you notice is the language. This observation is actually a bit too limiting for true internationalization: Countries can share a common language, but you still may need to do some work to make computer users of both countries happy.[1]

[1] "We have really everything in common with America nowadays, except, of course, language." Oscar Wilde.

In all cases, menus, button labels, and program messages will need to be translated to the local language; they may also need to be rendered in a different script. There are many more subtle differences; for example, numbers are formatted quite differently in English and in German. The number

 123,456.78 

should be displayed as

 123.456,78 

for a German user. That is, the role of the decimal point and the decimal comma separator are reversed! There are similar variations in the display of dates. In the United States, dates are somewhat irrationally displayed as month/day/year. Germany uses the more sensible order of day/month/year, whereas in China, the usage is year/month/day. Thus, the date

 3/22/61 

should be presented as

 22.03.1961 

to a German user. Of course, if the month names are written out explicitly, then the difference in languages becomes apparent. The English

 March 22, 1961 

should be presented as

 22. März 1961 

in German, or

19613 22

in Chinese.

There are several formatter classes that take these differences into account. In order to control the formatting, you use the Locale class. A locale describes

  • A language;

  • Optionally, a location; and

  • Optionally, a variant.

For example, in the United States, you use a locale with

language=English, location=United States.

In Germany, you use a locale with

language=German, location=Germany.

Switzerland has four official languages (German, French, Italian, and Rhaeto-Romance). A German speaker in Switzerland would want to use a locale with

language=German, location=Switzerland

This locale would make formatting work similarly to how it would work for the German locale; however, currency values would be expressed in Swiss francs, not German marks.

If you only specify the language, say,

language=German

then the locale cannot be used for country-specific issues such as currencies.

Variants are, fortunately, rare and are needed only for exceptional or system-dependent situations. For example, the Norwegians are having a hard time agreeing on the spelling of their language (a derivative of Danish). They use two spelling rule sets: a traditional one called BokmŒl and a new one called Nynorsk. The traditional spelling would be expressed as a variant

language=Norwegian, location=Norway, variant=Bokmål

To express the language and location in a concise and standardized manner, the Java programming language uses codes that were defined by the International Organization for Standardization. The local language is expressed as a lowercase two-letter code, following ISO-639, and the country code is expressed as an uppercase two-letter code, following ISO-3166. Tables 10-1 and 10-2 show some of the most common codes.

Table 10-1. Common ISO-639 Language Codes

Language

Code

Chinese

zh

Danish

da

Dutch

nl

English

en

French

fr

Finnish

fi

German

de

Greek

el

Italian

it

Japanese

ja

Korean

ko

Norwegian

no

Portuguese

pt

Spanish

sp

Swedish

sv

Turkish

TR


Table 10-2. Common ISO-3166 Country Codes

Country

Code

Austria

AT

Belgium

BE

Canada

CA

China

CN

Denmark

DK

Finland

FI

Germany

DE

Great Britain

GB

Greece

GR

Ireland

IE

Italy

IT

Japan

JP

Korea

KR

The Netherlands

NL

Norway

NO

Portugal

PT

Spain

ES

Sweden

SE

Switzerland

CH

Taiwan

TW

Turkey

tr

United States

US


NOTE

For a full list of ISO-639 codes, see, for example, http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt. You can find a full list of the ISO-3166 codes at a number of sites, including http://www.niso.org/3166.html.


These codes do seem a bit random, especially since some of them are derived from local languages (German = Deutsch = de, Chinese = zhongwen = zh), but at least they are standardized.

To describe a locale, you concatenate the language, country code, and variant (if any) and pass this string to the constructor of the Locale class.

 Locale german = new Locale("de"); Locale germanGermany = new Locale("de", "DE"); Locale germanSwitzerland = new Locale("de", "CH"); Locale norwegianNorwayBokmŒl = new Locale("no", "NO", "B"); 

For your convenience, the JDK predefines a number of locale objects:

 Locale.CANADA Locale.CANADA_FRENCH Locale.CHINA Locale.FRANCE Locale.GERMANY Locale.ITALY Locale.JAPAN Locale.KOREA Locale.PRC Locale.TAIWAN Locale.UK Locale.US 

The JDK also predefines a number of language locales that specify just a language without a location:

 Locale.CHINESE Locale.ENGLISH Locale.FRENCH Locale.GERMAN Locale.ITALIAN Locale.JAPANESE Locale.KOREAN Locale.SIMPLIFIED_CHINESE Locale.TRADITIONAL_CHINESE 

Besides constructing a locale or using a predefined one, you have two other methods for obtaining a locale object.

The static getdefault method of the Locale class initially gets the default locale as stored by the local operating system. You can change the default Java locale by calling setDefault; however, that change only affects your program, not the operating system. Similarly, in an applet, the getLocale method returns the locale of the user viewing the applet.

Finally, all locale-dependent utility classes can return an array of the locales they support. For example,

 Locale[] supportedLocales = DateFormat.getAvailableLocales(); 

returns all locales that the DateFormat class can handle.

TIP

For testing, you may want to switch the default locale of your program. Supply language and region properties when you launch your program. For example, here we set the default locale to German (Switzerland):


java -Duser.language=de -Duser.region=CH Program


Once you have a locale, what can you do with it? Not much, as it turns out. The only useful methods in the Locale class are the ones for identifying the language and country codes. The most important one is getdisplayName. It returns a string describing the locale. This string does not contain the cryptic two-letter codes, but it is in a form that can be presented to a user, such as

 German (Switzerland) 

Actually, there is a problem here. The display name is issued in the default locale. That may not be appropriate. If your user already selected German as the preferred language, you probably want to present the string in German. You can do just that by giving the German locale as a parameter: The code

 Locale loc = new Locale("de", "CH"); System.out.println(loc.getDisplayName(Locale.GERMAN)); 

prints

 Deutsch (Schweiz) 

This example shows why you need Locale objects. You feed it to locale-aware methods that produce text that is presented to users in different locations. You can see many examples in the following sections.


 java.util.Locale 1.1 

  • Locale(String language)

  • Locale(String language, String country)

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

    construct a locale with the given language, country, and variant

  • static Locale getDefault()

    returns the default locale.

  • static void setDefault(Locale loc)

    sets the default locale.

  • String getDisplayName()

    returns a name describing the locale, expressed in the current locale.

  • String getDisplayName(Locale loc)

    returns a name describing the locale, expressed in the given locale.

  • String getLanguage()

    returns the language code, a lowercase two-letter ISO-639 code.

  • String getDisplayLanguage()

    returns the name of the language, expressed in the current locale.

  • String getDisplayLanguage(Locale loc)

    returns the name of the language, expressed in the given locale.

  • String getCountry()

    returns the country code as an uppercase two-letter ISO-3166 code.

  • String getDisplayCountry()

    returns the name of the country, expressed in the current locale.

  • String getDisplayCountry(Locale loc)

    returns the name of the country, expressed in the given locale.

  • String getVariant()

    returns the variant string.

  • String getDisplayVariant()

    returns the name of the variant, expressed in the current locale.

  • String getDisplayVariant(Locale loc)

    returns the name of the variant, expressed in the given locale.

  • String toString()

    returns a description of the locale, with the language, country, and variant separated by underscores (e.g., "de_CH").


 java.awt.Applet 1.0 

  • Locale getLocale() [1.1]

    gets the locale for this applet.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net