The Locale ClassAn 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
Supported LocalesCurrently, 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
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
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
|