Struts 2 Chinese localization issue

A Struts 2 i18n localization issues for displaying the Chinese characters…

Case 1 : properties file with special characters

A properties file store the message of username, password and submit in Chinese character. This properties file is created in UTF-8 format, but the content is not encode with native2ascii.

struts2-localization-case1-1

Let try to display the Chinese character via few UI tags. The view page is declared to display in UTF-8 format with HTML meta tag.


...
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
...
<s:form action="validateUser">
	<s:textfield key="global.username" name="username"/>
	<s:password key="global.password" name="password"/>	
	<s:submit key="global.submit" name="submit" />
	
	<div>Testing 1 : <s:property value="getText('global.username')" /></div>
	<div>Testing 2 : <s:text name="global.password" /></div></br/>
</s:form>
...
<s:url id="localezhCN" namespace="/" action="locale" >
   <s:param name="request_locale" >zh_CN</s:param>
</s:url>
...
<s:a href="%{localezhCN}" >Chinese</s:a>
...

Result

struts2-localization-case1-2

Surprisingly, the following three UI tags are able to display the Chinese message correctly,


<s:textfield key="global.username" name="username"/>
<s:password key="global.password" name="password"/>	
Testing 2 : <s:text name="global.password" />

However, the “s:submit” and “getText()” are unable to display it?

According to Java’s i18n documentation, to display a special character correctly with resource bundle, it must be precessed with the native2ascii tool.

After dig into the TextProvider.getText() source code, it’s using the resource bundle.getString() to retrieved the message from resource bundle, so the incorrect messages are reasonable. But why “s:text“, “s:textfield” and “s:password” are able to display the Chinese message correctly, and why “s:submit” will failed? Too many questions in my mind, let go to case 2…

Case 2 : properties file with special characters (Encoded)

This time, the properties file is processed with native2ascii tool and the Chinese characters are encoded properly.

struts2-localization-case2-1

Result

struts2-localization-case2-2

The result is totally reverse, now the “s:submit” and “getText()” are able to display it correctly, but the other UI components are failed. This is work as expected, as Struts 2 recommend getText() to display i18n or localization message. The problem is, why “s:submit” is different?

Struts2..What’s wrong?

Here’s some questions in my mind…

  1. Why the s:submit is perform so differently?
  2. The i18n should be very straightforward, why Struts 2 has this kind of issue? Or i misunderstood how Struts 2 i18n work?
  3. Why there are so many ways to display a message from resource bundle? Why not just group it in one method? In Struts 1, just use the “bean:message”, why it look so complicated in Struts 2?
  4. Is Struts 2 support the XML resource bundle? I just do not like to use the native2ascii tool to encode the data to UTF-8 format, it make the properties file unreadable. Apache Wicket did a very good job in this problem, may be Struts 2 should learn from it.
  5. So, just how to display the Chinese characters correctly in Struts 2?
Many articles and tutorials are told that the following methods are able to display the message from resource bundle:


<s:text name="global.username"/>
<s:property value="getText('global.username')"/>

However, this is only apply to the English or some “English-like (Europe)” characters , e.g France, German. But for Chinese or Japanese, the ‘big square’ characters, both methods will return totally different output. Really no idea how to do the Struts 2 localization for Chinese and Japanese.

updated…(16/6/2010)

Digging the solution from the Baidu search engine, found the indirect solution in one of the Chinese article – Struts 2 internationalizing example

Solution

The problem is in the HTML meta tag,


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
In Struts 1, the above meta tag is required to display the UTF-8 data correctly, but this is not true for Struts 2.

In Struts 2, the meta tag is no longer function, we should put the <%@ page contentType=”text/html;charset=UTF-8″ %> tag in the first line of the view page. For example,


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
...

Result

struts2-localization-work

All the Chinese messages are display correctly.

Answer to my previous questions

  1. Why the s:submit is perform so differently?
    A: No comment at this
  2. The i18n should be very straightforward, why Struts 2 has this kind of issue? Or i misunderstood how Struts 2 i18n work?
    A: Make sure to put <%@ page contentType=”text/html;charset=UTF-8″ %>” in the first line of the view page.
  3. Why there are so many ways to display a message from resource bundle? Why not just group it in one method? In Struts 1, just use the “bean:message”, why it look so complicated in Struts 2?
    A: s:text, key, getText(), name… , all are able to render the Chinese or UTF-8 encoded data correctly, just make sure the put the correct “charset” in the view page. I still prefer only one method to control the message bundle (Like Struts 1), too many equivalent options are just drive confuse to the developers.

  4. Is Struts 2 support the XML resource bundle? I just do not like to use the native2ascii tool to encode the data to UTF-8 format, it make the properties file unreadable. Apache Wicket did a very good job in this problem, may be Struts 2 should learn from it.
    A: Hope Struts 2 can support the XML resource bundle in the next release.

  5. So, just how to display the Chinese characters correctly in Struts 2?
    A: See the solution above.

Download it – Struts2-i18n-issue-Example

Reference

  1. http://www.mkyong.com/java/java-convert-chinese-character-to-unicode-with-native2ascii/
  2. http://forums.sun.com/thread.jspa?threadID=5185040
  3. http://www.mail-archive.com/user@struts.apache.org/msg85490.html
  4. http://www.coderanch.com/t/452139/Struts/applicationresources-properties-utf-characters#2013557
  5. http://struts.apache.org/2.1.8/docs/localization.html
  6. http://hxzon00.blog.163.com/blog/static/10489241620088121449163/

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
11 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
ZiyadeepakNeoskizmicSharvan Kumar Recent comment authors
newest oldest most voted
Ziya
Guest
Ziya

I am implementing i18n with struts2 for english and french. But I need to toggle between these two languages, if I select English then option automatically should be turn into French and else it should be in English. I am using struts2 s:url and s:a tags to display links. If i am using s:if then how should I compare and display language selector. please help me ASAP.

Thanks,
Ziya

deepak
Guest
deepak

I want to insert chinese characters into ms sql server database. And also i am using struts 1.2. I am having textfiled on jsp page and entered chinese text into this field but this text is getting changed into some corrupted characters.
Please help me on this…+

Sharvan Kumar
Guest
Sharvan Kumar

Your example is for presentation.
My requirement is .. data input is in japanese or chinese.
I set page encoding, meta characters , character encoding and struts.xml encoding to utf-8 also included a filter to set character encoding to UTF-8 but all invain nothing is working.

My japanese or chinese characters turn up into crap…

Any urgent help is requested.

skizmic
Guest
skizmic

Sharvan, have you tried encoding the characters to HTML?

Here’s a tool which works for me, and displays the Chinese/Japanese, or any special characters correctly:

http://www.unicodetools.com/unicode/convert-to-html.php

Example:
?????????
would be encoded as:
おなかが空きました
and then would be rendered as HTML.

I don’t know if you have solved your problem already, but I hope this helps.

mathewxiang
Guest
mathewxiang

your system doesn’t support Chinese?

Sathish Kumar T
Guest
Sathish Kumar T

Hi,

i use freemarker as my stuts 2 result type … how to include @page tab in freemarker ?

Regards,
Sathish

Neo
Guest
Neo

@Sathish – Did you get any solution for this issue with freemarker?

Dhanya
Guest
Dhanya

Hi,
Thanks alot. This solution helped me a lot. Was trying to find this from long time. Keep posting such stuff!!!

Thanks
Dhanya

vishal
Guest
vishal

Hey Hi, I am also facing same problem , i am try to creat webshop in CZECH langugae. Czech uses ISO 8859_2 (or Windows1250 or Mac CE). I am doing the changes in my application like put in my view pages but still showing wrong encoding. e.g. for login button czech specific encoing is Přihlásit se (english:- Login) but on view page showing as it is (Přihlásit se). I am using struts 2 with tiles framework. For submit button i am using struts 2 tag lib like then encoding getting fail but in case of normal HTML with struts 2… Read more »

Jimmy
Guest
Jimmy

Dude,

You just saved my day!
Had to modify an existing project for work and I searched my ass off to make the French translation work. The one who made the project initially was still stuck in the Struts 1 era and used the old meta way to set a page to UTF-8.

I’m glad I found this entry,
But no matter what, I so DISLIKE Struts 2!

Anyway,

Thanks man!

Sam
Guest
Sam

good. save me a day! i had been struggling until i saw this post.

Martine Stelluti
Guest
Martine Stelluti

Can we get some more responses from guys looking at this post….I Do like the post,but i have had different views on the subject and i would love to know what others think

trackback
Struts 2 – i18n or localization example

[…] A login page with a textbox, password and submit UI components. To support Struts 2 localization, you HAVE TO declared the <%@ page contentType=”text/html;charset=UTF-8″ %> in your view page, else you will have problem to display the “UTF-8 data” correctly, especially the Chinese characters. Read this article about Struts 2 Chinese localization issue. […]