Sunday, November 25, 2007

Java web application Internationalization @ i18n

In order for an application to support i18n, generally, all of the permanent hard-coded text must be turned into labels and the actual text should be stored in a properties file commonly known as Application Resource or Resource Bundle.

The next step is, to declare that the view components (html/jsp/jsf) are using the UTF-8 encoding. This is very important as UTF-8 encoding supports most typeface (fonts) if not all of the languages in the world.

For static html. this can be done by using the meta tag in the
< head >
tag. example:
< meta equiv="Content-Type" content="" charset="utf-8" >

for the jsp/jsf, we can use this scriptlet before the < html > tag:
<%@ page contentType="text/html; charset=UTF-8" %>

if we are using struts as our web framework, we can skip doing the tedious and boring job of adding each of the pages with the scriptlet codes by adding this code in our struts-config.xml:
< controller contenttype="text/html; charset=UTF-8">


For the struts part, it will only work if none of the jsp/jsf skips the controller servlet. (direct linking from page to page without using global forwards).

If the Resource files are in a language that uses characters other than the normal English characters, we must convert them into Unicode ASCII characters. for example, if we have a Resource file that contains Chinese characters. In this case, we must convert it into Unicode ASCII characters.

to do this, we can use the native2ascii program that comes with the JDK itself. If we have already set the environment variable to point to our JDK directory, then we can already invoke the native2ascii command from anywhere.

Anyway, here's an example on how to convert a resource file with Chinese characters named ApplicationResources_cn.properties to Unicode ASCII encoding.

native2ascii -encoding UTF-8 ApplicationResources_zh.properties ApplicationResources_zh.properties.

Open the file again and you will see that the Chinese characters are now converted into sequences of \uxxxx. Don't worry, it will display fine when you deployed it in your web container and run it in your web browser.

No comments: