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.
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:
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 ); } } }
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 |
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.