This entry was posted on Monday, June 23rd, 2008 at 5:18 am and is filed under development, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
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:
- ActionClass.properties
- BaseClass.properties (all the way to Object.properties)
- Interface.properties (every interface and sub-interface)
- ModelDriven’s model (if implements ModelDriven), for the model object repeat from 1
- package.properties (of the directory where class is located and every parent directory all the way to the root directory)
- search up the i18n message key hierarchy itself
- 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}&token={2}">{0}</a>' document
user.validation.token.invalid={0} does not exists in the systemthe 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}&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,
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.
read users' comments (12)
12 Responses to “HOWTO: struts 2 i18n”
Leave a Reply
Most Popular
- HOWTO: PHP and jQuery upload progress bar (47)
- JQuery Progress Bar 1.1 (41)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (13)
- HOWTO: struts 2 i18n (12)
- JQuery Progress Bar 2.0 (11)
- JQuery Progress Bar 1.2 (10)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento (8)
- Howto: Repackageable custom extension development in Magento - Part 5 - CRUD - Retrieve (6)
Recent Comments
- donald: Hi Wen, I have checked out
- donald: Hi Ashley, Currently, I simply use
- donald: Hi Noemi, there are many
- Serge: Thanks a lot. It's the simplest
- Zoran: Excellent posts you got here...
- Craig: Brilliant tutorial! There's NOTHING on
- Will: Thnak you! I was
- Margots: Thank you a lot for
- Aswin S: Wow great dude.. For the
- Noemi Heier: This is great! How did
Latest Entries
- jQuery Progress Bar Configuration
- Extracting email addresses from inbox
- 10 Good (Free and Legal) Source for Photos and Images
- Howto: Backup Microsoft SQL Server Database, as in Dump it to a SQL Script (like MYSQL's sqldump)
- Managing client's expectation with wireframe software
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List
- JQuery Progress Bar 2.0
- HOWTO: Find icons for your new prototype system
- Google Maps Helper
- laying the cornerstones
September 30th, 2009 at 12:20 am
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 ?
September 8th, 2009 at 12:19 am
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!
July 16th, 2009 at 7:30 am
hats off to you, i was loking for an article like this.you article helped me a to set up i18n in my project
June 10th, 2009 at 2:17 am
Its really very nice article on struts2 Internationalization!
November 17th, 2008 at 11:48 am
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
September 15th, 2008 at 9:10 am
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???
September 12th, 2008 at 9:10 am
Hi,
much appreciate for your help.
September 12th, 2008 at 7:55 am
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.
September 12th, 2008 at 4:58 am
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!
August 28th, 2008 at 4:42 pm
i used a different approach, e.g.
in your .properties
index.img.welcome = /images/${getLocale().getLanguage()}/welcome.jpg
in your .jsp
<img src=”">
August 18th, 2008 at 5:44 am
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
July 16th, 2008 at 2:56 pm
Nice article pretty good work expecting other article on Struts 2 from you