[ Team LiB ] Previous Section Next Section

8.1 A Word About Locales

A locale represents a geographic, political, or cultural region. In Java, locales are represented by the java.util.Locale class. A locale is frequently defined by a language, which is represented by its standard lowercase two-letter code, such as en (English) or fr (French). Sometimes, however, language alone is not sufficient to uniquely specify a locale, and a country is added to the specification. A country is represented by an uppercase two-letter code. For example, the United States English locale (en_US) is distinct from the British English locale (en_GB), and the French spoken in Canada (fr_CA) is different from the French spoken in France (fr_FR). Occasionally, the scope of a locale is further narrowed with the addition of a system-dependent variant string.

The Locale class maintains a static default locale, which can be set and queried with Locale.setDefault( ) and Locale.getDefault( ). Locale-sensitive methods in Java typically come in two forms. One uses the default locale, and the other uses a Locale object that is explicitly specified as an argument. A program can create and use any number of nondefault Locale objects, although it is more common simply to rely on the default locale, which is inherited from the underlying default locale on the native platform. Locale-sensitive classes in Java often provide a method to query the list of locales that they support.

Finally, note that AWT and Swing GUI components (see Chapter 11) have a locale property, so it is possible for different components to use different locales. (Most components, however, are not locale-sensitive; they behave the same in any locale.)

    [ Team LiB ] Previous Section Next Section