East-Asian character ( Chinese, Japanese, Korean ) is different from the western character. But our web application should be accessed from all over the world. So we should support display those east-Asian characters on the web page correctly.
The solution to resolve the 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 the client web page.
Java provides the native2ascii command to implement this goal, and eclipse also has a native2ascii plugin that can make the Unicode converter. If you develop a web application with the spring tool suite, it has integrated the native2ascii plugin by default, this article will tell you how to use the above tool to convert the 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 the 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 the 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 an eclipse plugin that can help you to convert Chinese words to Unicode directly when you input the words in a .properties file. Follow the below steps to install it.
- Open eclipse, click Help —> Eclipse Market Place menu item.
- Input the keyword Properties Editor ( or native2ascii ) after the Find: text box, click the Go button to search. Then click the Install button to install it.
- After install, you can create the messages.properties file in the java project. Now when you input Chinese, Japanese, or Korean words, they will be converted to Unicode automatically.
- If you use Spring Tool Suite to develop a spring web application, it has contains the native2ascii plugin already.