Internationalize dynamic messages
Build flexible formatters for international applications with Java 1.1
By Todd Sundsted, JavaWorld.com, 02/10/98
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
If you have ever used a piece of software written for speakers of one language -- human, not computer -- that has been hastily
rewritten for speakers of another, you probably noticed that the interface, unless it had been redone
very professionally, felt a bit awkward.
This awkwardness comes from a number of sources -- some related to poor word selection and some related to differences in
the syntax of the two languages.
From the perspective of the programmer rewriting the software in a new language, the words are usually the easiest part of
the code to get right. For example, when you change the strings in the code from English to German, "Yes" becomes "Ja," "No"
becomes "Nein," and so on. Pretty simple stuff. Even simple phrases, such as "Do you wish to proceed?" can be replaced in
their entirety with the appropriate translation.
If translations were limited to these simplistic examples, there would be no problem. Consider, however, the following example
of a compiler diagnostic:
"There are 3 errors in 2 files"
Unlike the earlier examples, this example contains two pieces of information that change depending on the result of the compilation:
the number of errors and the number of source files compiled.
A naive implementation of the code to generate this diagnostic might look like this:
String str = "There were " + nErrorCount +
" errors in " + nFileCount +
"."
In this case the variables nErrorCount and nFileCount hold the number of errors and the number of files, respectively.
Imagine that we wanted to make it possible to display this compiler diagnostic in other languages. Using resource bundles
and the resource bundle API (which we covered in detail in last month's column), we could easily modify the code so that it doesn't contain any non-localized text.
ResourceBundle res = ResourceBundle.getBundle("Strings");
String str = res.getString("first") + " " + nErrorCount +
res.getString("second") + " " + nFileCount +
res.getString("third");
The resource bundle keys "first," "second," and "third" in the resource bundle named "Strings" are associated with the textural
pieces that make up the diagnostic. Now, each piece of the original can be translated and placed in the appropriate resource
bundle -- and the appropriate string will be constructed at runtime. We're in great shape, right?
Wrong!
The example above contains implicit assumptions about the position and order of the elements that make up the text. That might
not seem like such a problem, but it is.
A closer look
Consider the following three phrases:
"You copied 3 files"
"You deleted 2 files"
"You moved 0 files"
These three statements are identical except for the operation (copied, deleted, and moved) and the number of files affected
(3, 2, and 0). Let's call the operation, parameter zero {0}, and the number of files, parameter one {1}.
Here's the phrase with the operation and number of files affected replaced by parameter marker zero and one.
"You {0} {1} files"
What if, like the Japanese language, the English language required the verb to be at the end of the sentence? Our three phrases
then become:
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- The internationalization specification http://java.sun.com/products/jdk/1.1/intl/html/intlspecTOC.doc.html
- Java CookbookCreating Global Applications http://www.ibm.com/java/education/globalapps/
- The Java Tutorial (on writing global programs) http://java.sun.com/docs/books/tutorial/intl/index.html
- The java.text.MessageFormat class http://java.sun.com/products/jdk/1.1/docs/api/java.text.MessageFormat.html
- Download this article and the complete source as a gzipped tar file /javaworld/jw-02-1998/howto/jw-02-howto.tar.gz
- Download this article and the complete source as a zip file /javaworld/jw-02-1998/howto/jw-02-howto.zip
- Previous How-To Java articles
- "Localize this!" Use resource bundles to make your applications multicultural.
- "Write world-class applications" From Chicago to Copenhagen, Colombia to Cameroon -- Java provides the tools for writing truly international applications.
- "Use the two "R"s of Java 1.1 -- Readers and Writers" Learn how to use the two new additions to the
java.io package -- class Reader and class Writer -- to filter out unwanted e-mail.
- "Waging war on electronic junk mail" Put Java on the front line in the war against electronic junk mail.
- "Build dynamically extensible applications" Find out how to build programs that you can extend dynamically -- even while they execute.
- "3D computer graphicsGetting the hang of VRML" Learn about VRML and how you can use it to define your own virtual world.
- "3D computer graphicsMoving from wire-frame drawings to solid, shaded models" Find out how to create surfaces and add illumination to make your models more realistic.
- "3D computer graphicsSlide it, spin it, make it move -- transforming your virtual world" Learn how to make your virtual world satisfy even the toughest customer.
- "3D computer graphicsModel your world" From its start as an exotic research topic in government and university labs, virtual reality is making its move into the
mainstream of corporate America -- find out how you can gain entry into this elite club.
- "When static images just don't make the cut" Learn how to spice up your applets and applications with animated images.
- "How Java uses the the producer/consumer model to handle images -- An insider's look" Learn more about Java's powerful image-handling technique, then follow my simple procedures for building your own producer
and consumer components.
- "Learn how applets load network-based images asynchronously" Here's a close look at the way Java applets handle images in a network-based environment.
- "Drawing text is easy with three Java classes" Find out how to create text that's visually appealing through this explanation of what classes to use and how they work
together.
- "Examining HotSpot, an object-oriented drawing program" Learn how the pieces of the Java language and class library fit together through a study of this Java program
- "Using the Graphics class" A close look at the Graphics class and the drawing primitives it provides, and a demonstration of its use.
- "Observer and Observable" An introduction to the Observer interface and Observable class using the Model/View/Controller architecture as a guide.
- "The effective user interface" Five ways to enhance the appearance and effectiveness of your user interface
- "Java and event handling" How events are passed to user interface components, how to create event handlers, and more.
- "Introduction to the AWT" A description of Java's user interface toolkit.