Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Java: Good World Citizen


See the Code

Figure 4 shows a sample Chinese language Web page that is written in Unicode and encoded with UTF-8. Users with the latest operating systems and browsers will usually be able to properly render this page properly.

Figure 4: Welcome to the Classical Music Site.

But not everyone has a Unicode capable computer, operating system, and browser. A user who browsed to this page with a browser set to use a Big5 character set would see the screen shown in Figure 5:

Figure 5: Welcome to the Illegible Music Site.

Solving this problem is easy with Java. All I have to do is develop my content in Unicode, then use Java's built-in classes to churn out localized versions suitable for users of whatever encodings are needed.

Listing 1 shows the WebWriter class that I used for this article. This program has a hard-coded web page defined as an internal string. By using three different encodings, it creates web pages for browsers set to Unicode, Big5, and GB2312. As you can see, choosing the correct character set and encoding from Java is trivial.

import java.io.*;
public class WebWriter {
  static String eol = System.getProperty( "line.separator" );
  static String s =
  "<HTML>" + eol +
  "<BODY>" + eol +
  "<TABLE cellspacing=\"5\">" + eol +
  " <TR>" + eol +
  "  <TD><img src=\"michael.jpg\"></TD>" + eol +
  "  <TD><H2>" +
  "\u6b61\u8fce\u5149\u81e8\u53e4\u5178\u97f3\u6a02\u7a7a\u9593" +
  "</H2></TD>" + eol +
  "  <TD><img src=\"violin.jpg\"></TD>" + eol +
  " </TR>" + eol +
  "</BODY>" + eol +
  "</HTML>" + eol;

 public static void main(String[] args)
 {
  try {
  FileOutputStream fos = new FileOutputStream("c:/temp/page_utf8.htm");
  Writer out = new OutputStreamWriter( fos, "UTF8" );
  out.write( s );
  out.close();
  fos = new FileOutputStream( "c:/temp/page_gb.htm" );
  out = new OutputStreamWriter( fos, "GBK" );
  out.write( s );
  out.close();
  fos = new FileOutputStream( "c:/temp/page_big5.htm" );
  out = new OutputStreamWriter( fos, "BIG5" );
  out.write( s );
  out.close();
  }
  catch ( Exception e )
  {
    System.out.println( "Exception " + e );
  }
 }
}
Listing 1: WebWriter.java.

Details

You can see the actual HTML files created for this article on my web site. Note that well-written web pages use meta tags to help a browser figure out what encoding and character set to use. (See the section 5.2.2 of the HTML Spec for details.) These web pages don't; they provide no information intentionally, making experimentation a bit easier.

To view the web pages in their correct encoding, you will need to change your browser encoding setting. In Internet Explorer, you select this from the View|Encoding portion of the menu. If you are an English-speaking computer user, you will undoubtedly have to install Chinese or Unicode fonts as well.If you are running Internet Explorer with Windows XP, when you set your encoding to either Simplified or Traditional Chinese, you will be prompted to install the a language pack. This process is fairly painless, but it may requires that you have access to your Windows XP CDs.

The web pages can be found here:

If you look at the source code, each Chinese ideograph will be two or more characters, which will look to your ANSI text-editor something like this:

The text shown above contains ten GB2312 characters encoded using 20 bytes. For non-Chinese speakers, Table 1 shows the translation of the individual characters, as well as the more meaningful translation of short phrases consisting of multiple characters.

Ideograph Unicode GB2312 Big5 Character Meaning Phrase Meaning
U+6B61 2722 C577 happy, pleased, glad; joy; enjoy Welcome
U+8FCE 5113 AAEF receive, welcome, greet
U+5149 2566 A5FA light, brilliant, shine; only to
U+81E8 3357 C17B draw near, approach; descend
U+53E4 2537 A56A old, classic, ancient classic
U+5178 2168 A8E5 law, canon; documentation; class
U+97F3 5084 ADB5 sound, tone, pitch, pronunciation music
U+6A02 3254 BCD6 happy, glad; enjoyable; music
U+7A7A 3153 AAC5 empty, hollow, bare, deserted space
U+9593 2868 B6A1 interval, space; place, between

Table 1: Translation of the ideographs in the sample web page.

Conclusion

Successful products today need to support customers all over the world. Using Unicode for your core content makes this much easier, and Java is ready to help you on this path. More importantly, Java makes it simple to continue talking to devices on the edges of your network that are still using old-school character sets and encodings.

Unfortunately we still have to use human beings to do the difficult work of translating our content from one language to another, but outside of that Java does everything we need.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.