|註冊 | 登入 | 全文檢索 | 排行榜|
» Java 程式分享區
列印話題 寄給朋友 訂閱主題
|作者||[教學] i18n My experience|
Java, Ruby, Haskell
Date: Nov. 2002
* The Java Tutorial: Internationalization ( Sun Micro. )
* Java internationalization basics ( IBM developerWorks )
* Java Internationalization ( O’Reilly )
* Java I/O ( O’Reilly )
This article will not teach you how to do i18n.
Anything about local, format, resource bundle... etc. for i18n,
you could see the references to get more information.
This article is a note of how I resolve my small application about i18n problem,
more specific, is about how I show the Chinese character in my application.
In Window-XP English version, my application shows like diagram (A).
This is classic encoding problem; most time, I resolve this problem by using
NJStar software to convert and show me the Chinese character.
But this method does not work for Java Swing application:
First, Java String uses Unicode, can NJStar auto-convert Unicode String ?
Even NJStar can do that, there are a fatal problem, Java Swing is lightweight
components, tree and table are using "drawing" technique, which make
NJStar useless, because there are no OS native resource related,
so NJStar cannot catch the OS resource component and translate.
And when you ship your application, I think, you do not like assuming
your user always has NJStar.
So, this is the note about how I fix this problem, and hope this can also help
people who have the same problem as mine.
Step #1: Unicode conversion
For convert a local encoding data to Unicode data, you could do :
* Using "native2ascii.exe" tool.
* Using InputStreamReader. ( InputStreamReader( InputStream in, String charsetName ) )
But, In my case, I cannot use either those two methods, in fact,
my GB2312 code data is store in some database, via JDBC's getString
method, I got a Java Unicode String but the content is GB2312 data.
I do not like to research how to turning the JDBC driver to return correct Unicode String.
( it may-be possible for some Database. )
Now the problem is how to convert the String to correct Unicode String?
After look at String API, the simple way is:
The Step #1 works, but it will create too many temporary objects.
Imagine if I have 60.000 string to convert, the step #1 will create
60.000 new string and 60.000 byte array objects.
The String.getBytes(int srcBegin, int srcEnd, byte dst, int dstBegin) is deprecated.
( I do not like use deprecated method to save 60.000 byte array creation )
I find out most of my data are ASCII, only data has character between 128 to 255 code
( 0x0080~0x00FF ) need to be convert. So, I create StringUtilities class to help convert:
This reuses the _char object, and creates new string only for the string need to be converted.
The LATIN_1_SUPPLEMENT represents char code 0x0080~0x00FF.
OK, After this step, my application looks like diagram ( B ).
Step #2: Swing, Font
After we have correct Unicode String, we need the
Unicode font to show those Chinese characters.
I found two Microsoft fonts can support Unicode :
"Arial Unicode MS.TFF" and "Arialuni.TTF"
If the font installed on the system, you could simply create the font by :
If not, you could use Font.createFont ( int fontFormat, InputStream fontStream )
to create the font by using FileInputStream.
After that, just set the Swing components's font. That's it.
Step #2 works perfectly, but if you have many swing components,
that may-be tedious to reset the font for all those swing components.
In my case, I need create customize TreeCellRender and TableCellRender
to set the Font. If most of components need use Unicode font,
the better way is make the Unicode font become default font. So I did :
That help me to avoid create 2 customize renders!
OK, finally! Happy to see beautiful Chinese characters back again. ( diagram ( C ) )
Copyright by T55555, Nov. 2002
|7147||[教學] i18n My experience||T55555||6615||2004-02-28 07:08|
|4030||Re:[分享] i18n My experience||T55555||870||2004-02-28 07:11|
|» JWorld@TW » Java 程式分享區|