specific localesuch as by translating messages into the local languageis called localization.
The first tool is inherent in the language: Strings are in Unicode, which can express almost any written
language on our planet. Someone must still translate the strings, and displaying the translated text to users
requires fonts for those characters. Still, having Unicode is a big boost for localizing your code.
The nexus of internationalization and localization is the locale, which defines a "place." A place can be a
language, culture, or countryanything with an associated set of customs that requires changes in program
behavior. Each running program has a default locale that is the user's preferred place. It is up to each program
to adapt to a locale's customs as best it can. The locale concept is represented by the Locale class, which is
part of the java.util package.
Given a locale, several tools can help your program behave in a locally comprehensible fashion. A common
pattern is for a class to define the methods for performing locale-sensitive operations. A generic "get instance"
static method of this class returns an object (possibly of a subclass) suitable for the default locale. The class
will also provide an overload of each "get instance" method that takes a locale argument and returns a suitable
object for a particular locale. For example, by invoking the class's getInstance methods, you can get an
appropriate java.util.Calendar object that works with the user's preferred dates and times. The
returned Calendar object will understand how to translate system time into dates using the customs of the
default locale. If the user were Mexican, an object that was a Calendar adapted to Mexican customs could
be returned. A Chinese user might get an object of a different subclass that worked under the Chinese calendar
customs.
If your program displays information to the user, you will likely want to localize the output: Saying "That's
not right" to someone who doesn't understand English is probably pointless, so you would like to translate
(localize) the message for speakers in other locales. The resource bundle mechanisms help you make this
possible by mapping string keys to arbitrary resources. You use the values returned by a resource bundle to
make your program speak in other tonguesinstead of writing the literal message strings in your code, you look
up the strings from a resource bundle by string keys. When the program is moved to another locale, someone
can translate the messages in the resource bundle and your program will work for that new locale without your
changing a line of code.
The classes described in this chapter come mostly from the package java.util. There are occasional brief
discussions of classes in the text internationalization and localization package java.text, with an overview
of some of its capabilities in Section 24.6 on page 708, but a full discussion on this subject is outside the
scope of this book.
24.1. Locale
A java.util.Locale object describes a specific placecultural, political, or geographical. Using a locale,
objects can localize their behavior to a user's expectations. An object that does so is called locale-sensitive.
For example, date formatting can be localized by using the locale-sensitive DateFormat class (described
later in this chapter), so the date written in the United Kingdom as 26/11/72 would be written 26.11.72
in Iceland, 11/26/72 in the United States, or 72.26.11 in Latvia.
A single locale represents issues of language, country, and other traditions. There can be separate locales for
U.S. English, U.K. English, Australian English, Pakistani English, and so forth. Although the language is
arguably in common for these locales, the customs of date, currency, and numeric representation vary.
Your code will rarely get or create Locale objects directly but instead will use the default locale that reflects
the user's preference. You typically use this locale implicitly by getting resources or resource bundles as
shown with other locale-sensitive classes. For example, you get the default calendar object like this: