Internationalization

To talk about internationalization, there is an interesting joke on its acronym “i18n”. You know why it is called i18n?

The answer is simple, just because there are 18 characters in between its first and last letter. :)

Last week, my colleague discussed with me on how to make the i18n possible since he was unable to do so. At that time, I couldn’t really answer on why, but I can say, it must be possible, as my old company did that.

After then, I did some researches, and finally solved the problem. It can be divided into several parts. (Mainly focus on how to show Simplified Chinese)

  1. Include Chinese directly in the JAVA class
  2. Show a Chinese String which is retrieved from database
  3. Send the Chinese String from the client to server side

The solutions or methods to each case are as follow:

1. Include Chinese directly in the JAVA class

This one is so simple. Just type the chinese in the JAVA class should be ok. If you’re not ok, it should be just your computer’s problem. Try reinstalling the JDK, it’s no matter to the stable release of JDK version.

2. Show a Chinese String which is retrieved from database

This is one is not difficult either. For the database retrieving part, just like other cases, use the very simple SQL statement is ok, since the JDBC connector will take the content as usual Strings. After retrieving the contents to the Action / Servlet, you should send the content to the JSP, in this step, you should set the character set to UTF-8, so that the string is encoded in Unicode(UTF-8) like the following:

response.setContentType(”text/html; charset=UTF-8″);

3. Send the Chinese String from the client to server side

For this one, there are more to do.
Firstly, as we’re using JBoss which Tomcat is embedded as the web container. We should do some tricks to let Tomcat knows the characters are chinese.

The first thing to do is to make use of the tomcatEncoding-1-0.jar, put this into the /WEB-INF/lib/ folder.
Then we need to add some elements to the /WEB-INF/web.xml, like the following:

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.apache.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Actually, we just put a filter into Tomcat, so that for every request made to Tomcat, the parameters will pass through this filter and be encoded to UTF-8 Strings. As a resul, we can pass chinese characters into the servlet

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • Technorati
  • YahooMyWeb

 

July 2006
S M T W T F S
    Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories