Jun 23, 2008

HOWTO: struts 2 i18n

Author: gaweee | Filed under: development, howto

i18n in struts is really flexible if set up correctly. Unfortunately searching for a document to learn the ‘hidden’ tricks of it is really frustrating… So in revenge, i’m writing my own! I hope someone out there finds this useful…

Step 1: Edit your struts.properties file – Add/Edit the following line:

struts.custom.i18n.resources=languages_actions,languages_views

This definition instructs struts to use the files languages_actions.properties and languages_view.properties for your language definitions. When there is a new request locale, such as zh_CN (chinese), struts will find for the file languages_actions_zh_CN.properties, or if defaults to en_US (english), struts will use the file language_actions_en_US.properties.
On top of that, there can be many places where language definitions can be found. They will be searched in the following order:

  1. ActionClass.properties
  2. BaseClass.properties (all the way to Object.properties)
  3. Interface.properties (every interface and sub-interface)
  4. ModelDriven’s model (if implements ModelDriven), for the model object repeat from 1
  5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
  6. search up the i18n message key hierarchy itself
  7. global resource properties

I put my files in the main WEB-INF\classes folder where struts.properties can be found
Read here for more information on how struts 2 searches for i18n definitions.

Step 2: Writing the language file itself

user.submit.image=/images/en_US/submit.gif
user.welcome=Welcome, {0}
user.link=<a href="somelink.action?id={1}&amp;token={2}">{0}</a>' document
user.validation.token.invalid={0} does not exists in the system

the above is an example of a en_US language definition file. You can see how the image and welcome message would be different in a zh_CN language definition file. but just for the heck of it, here it is anyway

user.submit.image=/images/zh_CN/submit.gif
user.welcome={0}, 你好
user.link=<a href="somelink.action?id={1}&amp;token={2}">{0}</a> 的文件
user.validation.token.invalid={0} 不纯在于系统

The key here in flexibility is the use of # parameters for the definitions (very printf-ish). This allows the dynamic data to be reformatted to any language structure (left to right, right to left, orientation of grammer, verbs, subject, topics, etc). Really smart way to do it!

Step 3: Using the definitions:
in JSP,

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1><s:text name="user.welcome">
	<s:param value="user.name" />
</s:text></h1>
<s:text name="user.link">
	<s:param value="document.name" />
	<s:param value="document.id" />
	<s:param value="document.token" />
</s:text>
<form input="">
	<s:submit type="image" src="%{getText('user.submit.image')}" cssClass="submit" />
</form>

Or in Java,

public class SomeActions extends ActionSupport {
	public String someMethod() throws Exception {
		if (!token.equalsIgnoreCase("abcdefghi")) {
			String [] messageargs = { token };
			addActionError(getText("user.validation.token.invlaid", messageargs));
		}
	}
 
	private String token;
	public String getToken() 	{ return  token; }
	public void setToken(String s) 	{ token = s; }
}

The above examples goes to show how you can combine dynamic data with the i18n strings in a JSP or a Java file

Tips

  • When using a utf8 language definition file (such as for zh_CN), you have to write out your files then run it through the native2ascii program. On windows it would look like this:
    "c:\Program Files\Java\jdk1.6.0_04\bin\native2ascii.exe" -encoding UTF8 \path\to\zh_CN.locale \new\path\to\languages_actions_zh_CN.properties

    Change your jdk\bin directory to where appropriate

  • Flood HTML with s:text language definitions – If you’ve already decided to make your app internationalized, its a big step, so make sure everything is pulled off a dynamic language pack.
  • Always use the s:date taglib to represent date, save yourself all the time (and frustration) in the world
  • Set up your default language definition file first. Struts makes it such that if your definition doesn’t exists then it searches the default file for it. That is to say if your definition for user.link doesn’t exists in languages_actions_zh_CN.properties, struts will continue to find for it in languages_actions.properties so at least there will be a link there, even if its untranslated.

12 Responses to “HOWTO: struts 2 i18n”

  1. Michael Says:

    I have a bit of a question. What if you need to internationalize a JAVA class that doesn’t inherit from ActionSuport (ie is not an action class). Say for instance you have a util running some place to send a mail and maybe some of the mail body needs to be internationalized. Is there any way to access the struts2 bundles from such a class ?

  2. bano Says:

    very helpful article! but with different languages, we also need to adjust some styles (css) since text with non-english alphabet/characters tend to take up space differently. can we also “internationalize” css files? like having a different set of css for each language. thanks!

  3. Ciju Says:

    hats off to you, i was loking for an article like this.you article helped me a to set up i18n in my project

  4. Rakesh Ranjan Srivastava Says:

    Its really very nice article on struts2 Internationalization!

  5. Rashmi Says:

    Hi All,

    I am using struts 2.0, where i am trying to read values from the properties files, but not overriding struts.custom.i18n.resources but by overriding struts.custom.properties. i tried it so many ways but still not able to read values from properties files. really i want a global properties file without i18n support. can any one one help me out. Thanks Rashmi, Bangalore

  6. roopam Says:

    Hi

    Nice article ,can any one of u tell me whether it is possible to replace all package.properties file data in database and can we use database as replacement for various proerties files of different local ?? if yes ,how???

  7. Stefan Says:

    Hi,

    much appreciate for your help.

  8. Jai Says:

    Hi,

    Thanks a ton. I was struggling for a day to get Indian languages working with struts properties file and your blog helped me solve the problem.

  9. Alberto Says:

    Really good man! Thank you, you just turned my frustration into joy. From reading a lot of empty pages that left me the way i was (why not to say it, in struts 2 in action, by manning), i passed to see my web app changing from english to spanish smooth and easily!

  10. some one Says:

    i used a different approach, e.g.

    in your .properties
    index.img.welcome = /images/${getLocale().getLanguage()}/welcome.jpg

    in your .jsp
    <img src=”">

  11. Daniel Schildsky Says:

    Great job! I’m experienced in struts 1 but it’s hell catching up with struts 2. Hope that you can come out with more articles about struts 2.
    Oh yes, do skip those brief-history kind of discussion…thanks..hehe

  12. Praveen Says:

    Nice article pretty good work expecting other article on Struts 2 from you

Leave a Reply