East-Asian character ( Chinese, Japanese, Korean ) is different from western character. But our web application should be accessed from all over the world. So we should support display those east-asian characters in web page correctly. The solution to resolve character charset issue is to use unicode for all languages messages in our web application. So we need to convert the character to unicode before render them to client web page.
Java provide native2ascii command to implement this goal, and eclipse also has native2ascii plugin that can make the unicode converter. If you develop web application with spring tool suite, it has integrate the native2ascii plugin by default, this article will tell you how to use above tool to convert east-asian character to unicode in java.
1. Use native2ascii Tool To Convert Character To Unicode.
- Save original Chinese words ( 今天天气很好 ) in a file chinese.txt.
- Open a terminal and run native2ascii command. The first argument is the source text file which contains Chinese words, the second argument is the target unicode character file which will be generated. You should make sure your JDK has been installed correctly, and JAVA_HOME/bin folder has been added in the system PATH environment variable value for Windows OS.
192:~ $ native2ascii chinese.txt chinese_unicode.properties
- Open the generated text file, you can find below unicode content.
2. Use Eclipse native2ascii Plugin.
There is a eclipse plugin which can help you to convert Chinese words to unicode directly when you input the words in a .properties file. Follow below steps to install it.
- Open eclipse, click Help —> Eclipse Market Place menu item.
- Input native2ascii after Find text box, click Go to search. Then click Install button to install it.
- After install, you can create a messages.properties file in the java project. Now when you input Chinese, Japanese or Korean words, it will be converted to unicode automatically.
- If you use Spring Tool Suite to develop spring web application, it has contains the native2ascii plugin already.