Conforming to Local Customs
Now we know how to use HTML character entities and Unicode escapes to display the characters in Western European languages. The question remains, what do we say with these languages? In general, this is a translation problem best left to a dedicated localization team. In some instances, however, Java provides some help.
For example, let’s assume that in addition to saying “Hello World,” we need our example servlet to tell the current time in a format naturally understood by the recipient. What could be a difficult formatting problem is actually quite easy because JDK 1.1 provides built-in support for localizing dynamic objects such as dates and times.
The trick is to use a java.text.DateFormat
instance appropriate for the target audience. A
DateFormat object can convert a
Date to a correctly localized
String. For example, a time stamp written in
English as “February 16, 1998 12:36:18 PM PST” would be
written in Spanish as “16 de febrero de 1998 12:36:18
GMT-08:00.”
A DateFormat object is created using a factory
method that accepts a formatting style (short, medium, long, full)
and a java.util.Locale
object that identifies the target audience (U.S. English, Mainland
Chinese, etc.). The most common Locale constructor
accepts two parameters: a two-character lowercase
language abbreviation (as we saw earlier)
and a two-character uppercase country code as defined by
ISO-3166 (available at http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html). ...