29.12. Review Questions

 
[Page 862 ( continued )]

26.2. The Locale Class

A Locale object represents a geographical, political, or cultural region in which a specific language or custom is used. For example, Americans speak English, and the Chinese speak Chinese. The conventions for formatting dates, numbers , currencies, and percentages may differ from one country to another. The Chinese, for instance, use year/month/day to represent the date, while Americans use month/day/year. It is important to realize that locale is not defined only by country. For example, Canadians speak either Canadian English or Canadian French, depending on which region of Canada they reside in.

Note

Every Swing user -interface class has a locale property inherited from the Component class.


To create a Locale object, use one of the three constructors with a specified language and optional country and variant, as shown in Figure 26.1.

Figure 26.1. The Locale class encapsulates a locale.
(This item is displayed on page 863 in the print version)

The language should be a valid language code, that is to say, one of the lowercase two-letter codes defined by ISO-639. For example, zh stands for Chinese, da for Danish, en for English, de for German, and ko for Korean. Table 26.1 lists the language codes.

Table 26.1. Common Language Codes
(This item is displayed on page 863 in the print version)
Code Language
da Danish
de German
el Greek
en English
es Spanish
fi Finnish
fr French
it Italian
ja Japanese
ko Korean
nl Dutch
no Norwegian
pt Portuguese
sv Swedish
tr Turkish
zh Chinese

The country should be a valid ISO country code, that is to say, one of the uppercase, two-letter codes defined by ISO-3166. For example, CA stands for Canada, CN for China, DK for Denmark, DE for Germany, and US for the United States. Table 26.2 lists the country codes.

Table 26.2. Common Country Codes
(This item is displayed on page 863 in the print version)
Code Country
AT Austria
BE Belgium
CA Canada
CH Switzerland
CN China
DE Germany
DK Denmark
ES Spain
FI Finland
FR France
GB United Kingdom
GR Greece
IE Ireland
HK Hong Kong
IT Italy
JP Japan
KR Korea
NL Netherlands
NO Norway
PT Portugal
SE Sweden
TR Turkey
TW Taiwan
US United States

The argument variant is rarely used and is needed only for exceptional or system-dependent situations to designate information specific to a browser or vendor. For example, the Norwegian language has two sets of spelling rules, a traditional one called bokm l and a new one called nynorsk . The locale for traditional spelling would be created as follows :


[Page 864]
   new   Locale(   "no"   ,   "NO"   ,   "B"   ); 

For convenience, the Locale class contains many predefined locale constants. Locale.CANADA is for the country Canada and language English; Locale.CANADA_FRENCH is for the country Canada and language French. Several other common constants are:

 Locale.US, Locale.UK, Locale.FRANCE, Locale.GERMANY, Locale.ITALY, Locale.CHINA, Locale.KOREA, Locale.JAPAN, and Locale.TAIWAN 

The Locale class also provided the following constants based on language:

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

Tip

You can invoke the static method getAvailableLocales() in the Locale class to obtain all the available locales supported in the system. For example,

 Locale[] availableLocales = Calendar.getAvailableLocales(); 

returns all the locales in an array.


Tip

Your machine has a default locale. You may override it by supplying the language and region parameters when you run the program, as follows:

 java Duser.language=zh Duser.region=CN MainClass 


An operation that requires a Locale to perform its task is called locale-sensitive . Displaying a number as a date or time, for example, is a locale-sensitive operation; the number should be formatted according to the customs and conventions of the user's locale. The following sections introduce locale-sensitive operations.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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