The Locale Class

   

The Locale Class

An internationalized program can display information differently throughout the world. For example, the program will display different messages in Paris, Tokyo, and New York. If the localization process has been fine- tuned , the program will display different messages in New York and London to account for the differences between American and British English. The internationalized program identifies the appropriate language and region of its end users by referencing a particular Locale object.

A Locale object encapsulates just enough information about a specific locale to uniquely identify the locale's region. When a locale-sensitive method is passed a Locale object as a parameter, it attempts to modify its behavior for that particular locale. A Locale is initialized with a language code, a country code, and an optional variant code. These three things define a region, although you need not specify all three. For example, you could have a Locale object for American English, California variant. If you ask the Calendar class what the first month of the year is, the Calendar tries to find a name suitable for Californian American English. Because month names are not affected by what state you are in, the Calendar class has no built-in support for Californian English, and it tries to find a best fit. It next tries American English, but because month names are constant in all English-speaking countries , this fails as well. Finally, the Calendar class returns the month name that corresponds to the English Locale. This best-fit lookup procedure allows the programmer complete control over the granularity of internationalized code.

You create a Locale object using the following syntax:

 Locale theLocale = new Locale("en", "US"); 

en specifies English, and US specifies United States. These two-letter codes are used internally by Java programs to identify languages and countries. They are defined by the ISO-639 and ISO-3166 standards documents, respectively. More information on these two documents can be found at

 http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html 

Table 24.1 shows a few of the default language codes supported by the Java language. This is not the complete list, but are the codes probably used most often.

Table 24.1. A Sampling of the Language Codes Supported by Java
Code Description
de German
en English
fr French
ja Japanese
jw Javanese
ko Korean
zh Chinese

Supported Locales

Currently, Java supports the language and country combinations, shown in Table 24.2, in all its locale-sensitive classes, such as Calendar, NumberFormat, and so on. This list might change in the future, so be sure to check the documentation in the docs/guide/intl directory under your SDK installation root directory with future versions.

Table 24.2. Locales Supported by the SDK
Locale Country Language
da_DK Denmark Danish
de_AT Austria German
de_CH Switzerland German
de_DE Germany German
el_GR Greece Greek
en_CA Canada English
en_GB United Kingdom English
en_IE Ireland English
en_US United States English
es_ES Spain Spanish
fi_FI Finland Finnish
fr_BE Belgium French
fr_CA Canada French
fr_CH Switzerland French
fr_FR France French
it_CH Switzerland Italian
it_IT Italy Italian
ja_JP Japan Japanese
ko_KR Korea Korean
nl_BE Belgium Dutch
nl_NL Netherlands Dutch
no_NO Norway Norwegian (Nynorsk)
no_NO_B Norway Norwegian (Bokm l)
pt_PT Portugal Portuguese
sv_SE Sweden Swedish
tr_TR Turkey Turkish
zh_CN China Chinese(Simplified)
zh_TW Taiwan Chinese (Traditional)

You can also get a List of the installed Locale's programmatically by using the getAvailableLocales method on the Locale class. The method is static and returns an array of Locale objects. Here's an example of how you can use the method:

 public void getAvailableLocales()   {     Locale[] availableLocales = Locale.getAvailableLocales();     int size = availableLocales.length;     for ( int i = 0; i < size; i++ )     {       Locale aLocale = availableLocales[i];       System.out.println( aLocale.getDisplayName() );     }   } 

Programmers can also create their own custom Locale s by specifying a unique sequence of country, language, and variant. You can use an underscore character to separate multiple variants. To create a variant of Californian American English running on a Windows machine, use the following code:

 Locale theLocale = new Locale("en", "US", "CA_WIN"); 

Remember that methods that do not understand this particular variant will try to find a best fit match, in this case probably en_US.

The two-letter abbreviations listed here are not to be displayed to the user ; they are meant only for internal representation. For display purposes, use one of the Locale methods listed in Table 24.3. You will notice that these methods are generally overloaded so that you can get the parameter either for the current locale or the one specified.

Table 24.3. Locale Display Methods
Method Name Description
getDisplayCountry()  
getDisplayCountry(Locale) Country name, localized for default Locale, or specified Locale.
getDisplayLanguage()  
getDisplayLanguage(Locale) Language name, localized for default Locale, or specified Locale.
getDisplayName()  
getDisplayName(Locale) Name of the entire Locale, localized for default Locale, or specified Locale.
getDisplayVariant()  
getDisplayVariant(Locale) Name of the Locale 's variant. If the localized name is not found, this returns the variant code.

These methods are useful when you want to have a user interact with a Locale object. Here's an example of using the getDisplayLanguage() method:

 //Set default Locale to American English Locale.setDefault( new Locale("en", "US") ); //Create locale for Japan Locale japanLocale = new Locale("ja", "JP"); System.out.println( japanLocale.getDisplayLanguage() ); System.out.println( japanLocale.getDisplayLanguage( Locale.FRENCH ) ); 

This code fragment prints out the name of the language used by japanLocale. In the first case, it is localized for the default Locale, which has been conveniently set to American English. The output would therefore be Japanese. The second print statement localizes the language name for display in French, which yields the output Japonais.

All the Locale "display" methods use this same pattern. Almost all Internationalization API methods allow you to explicitly control the Locale used for localization, but in most cases, you'll just want to use the default Locale.

Another thing to note in the preceding example is the use of the static constant Locale.FRENCH. The Locale class provides a number of these useful constants, each of which is a shortcut for the corresponding Locale object. A list of these objects is shown in Table 24.4.

Table 24.4. Locale Static Objects
Constant Name Locale Shortcut For
CANADA English Canada new Locale("en", "CA", "")
CANADA_FRENCH French Canada new Locale("fr", "CA", "")
CHINA SCHINESE PRC Chinese (Simplified) new Locale("zh", "CN", "")
CHINESE Chinese Language new Locale("zh", "", "")
ENGLISH English Language new Locale("en", "", "")
FRANCE France new Locale("fr", "FR", "")
FRENCH French Language new Locale("fr", "", "")
GERMAN German Language new Locale("de", "", "")
GERMANY Germany new Locale("de", "DE", "")
ITALIAN Italian Language new Locale("it", "", "")
ITALY Italy new Locale("it", "IT", "")
JAPAN Japan new Locale("jp", "JP", "")
JAPANESE Japanese Language new Locale("jp", "", "")
KOREA Korea new Locale("ko", "KR", "")
KOREAN Korean Language new Locale("ko", "", "")
TAIWAN TCHINESE (Traditional Chinese) Taiwan new Locale("zh", "TW", "")
UK Great Britain new Locale("en", "GB", "")
US United States new Locale("en", "US", "")
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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