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). An empty string for the country code indicates the default country for the language.
Example 12-4 shows the HelloSpain servlet using a DateFormat object to print the current time in a format naturally understood by a Spanish-speaking recipient.
import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloSpain extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); res.setHeader("Content-Language", "es"); Locale locale = new Locale("es", ""); DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); fmt.setTimeZone(TimeZone.getDefault()); out.println("En Espa\u00f1ol:"); out.println("\u00a1Hola Mundo!"); out.println(fmt.format(new Date())); } }
This servlet first creates a Locale that represents a generic Spanish environment. Then it uses that Locale to create a DateFormat instance that formats dates in Spanish. Next, it sets the time zone to the default time zone (the time zone of the server). The reason is that, by default, a DateFormat object formats its times to match the time zone in which it assumes the intended recipient is located, in this case Spain. Because this servlet can't be sure that's a correct assumption, it overrides the default and sets the time zone to match the server's. It would be better, of course, to set the time zone to accurately match the client's location, but that's not currently possible without additional user-provided information. Finally, after saying its " Hello World," this servlet prints the correctly formatted date and time. The output is shown in Figure 12-3.
This example provides just a glimpse of the dynamic formatting capabilities of Java. If you're interested in more complicated formatting, there are several other classes in the java.text package you may find useful. Look especially at those that extend java.text.Format.
Copyright © 2001 O'Reilly & Associates. All rights reserved.