Locales

The java.util.Locale class is the centerpiece of the various internationalization APIs. Instances of this class represent locales, which are geographical, political, or cultural regions.

NOTE: Java version 6 introduced the Locale-Sensitive Services SPI (Service Provider Interface) to let third parties support unsupported locales by introducing new implementations of various locale-sensitive classes located in the java.text and java.util packages. For example, you can use this SPI to provide new implementations of the java.util.Breaklterator class (discussed later in this chapter). To learn about Locale-Sensitive Services, and to explore an example that introduces a new currency, check out this topic in my book Beginning Java SE 6 Platform: From Novice to Professional(Apress, 2007; ISBN: 159059830).

Locale declares constants (such as CANADA) that describe some common locales. This class also declares three constructors for initializing Locale objects, in case you cannot find an appropriate Locale constant for a specific locale:

■ Locale(String language) initializes a Locale instance to a language code; for example, "fr" for French.

■ Locale(String language, String country) initializes a Locale instance to a language code and a country code; for example, "en" for English and "US" for United States.

■ Locale(String language, String country, String variant) initializes a Locale instance to a language code, a country code, and a vendor- or browser-specific variant code; for example, "de" for German, "DE" for Germany, and "WIN" for Windows (or "MAC" for Macintosh).

The International Standards Organization (ISO) defines language and country codes. ISO 639 (http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt) defines language codes. ISO 3166 (http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3l66.html) defines country codes. Locale works with both standards.

NOTE: ISO 639 is not a stable standard and some language codes have changed; specifically, iw, ji, and in have changed to he, yi, and id, respectively.

Variant codes are useful for dealing with computing platform differences. For example, font differences may force you to use different characters on Windows-, Linux-, and Unix-based operating systems (such as Solaris). Unlike language and country codes, variant codes are not standardized.

Although applications can create their own Locale objects (perhaps to let users choose from similar locales), they will often call API methods that work with the default locale, which is the locale made available to the virtual machine at startup. An application can call Locale's static Locale getDefault() method if it needs to access this locale.

For testing or other purposes, the application can override the default locale by calling Locale's static void setDefault(Locale locale) method. setDefault() sets the default locale to locale. However, passing null to locale causes setDefault() to throw NullPointerException.

Listing 9-9 demonstrates getDefault() and setDefault(). Listing 9-9. Viewing and changing the default locale import java.util.Locale;

public class MyLocale {

public static void main(String[] args) {

System.out.println(Locale.getDefault()); Locale.setDefault(Locale.CANADA); System.out.println(Locale.getDefault());

When I run this application, I observe the following output:

en_US en_CA

You can change the default locale that is made available to the virtual machine by assigning appropriate values to the user.language and user.country system properties when you launch the application via the java tool. For example, the following java command line changes the default locale to fr_FR:

java -Duser.language=fr -Duser.country=FR MyLocale

As you continue to explore Locale, you will discover additional useful methods. For example, static String[] getISOLanguages() returns an array of ISO 639 language codes (including former and changed codes), and static String[] getISOCountries() returns an array of ISO 3166 country codes.

NOTE: Read John O'Conner's "Internationalization: Understanding Locale in the Java Platform" article (http://java.sun.com/developer/technicalArticles/J2SE/locale/) for more information on Locale.

Was this article helpful?

0 0

Post a comment