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…
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.
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!
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
- 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.
Most Popular
- HOWTO: PHP and jQuery upload progress bar (48)
- JQuery Progress Bar 1.1 (42)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (16)
- HOWTO: struts 2 i18n (13)
- JQuery Progress Bar 2.0 (12)
- JQuery Progress Bar 1.2 (11)
- Howto: Repackageable custom extension development in Magento (9)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List (8)
Recent Comments
- learning methods: All the posts you talk
- best travel agency: Wow! I really enjoyed this
- Muzafar Ali: Hi, thanks for sharing a beautiful
- andy65007: After the progress bar
- Mark: Excellent tutorials. Most "how to
- Mark: As Jim said before, you
- Jeremy Roberts: I tried and it works
- Silas Nordes: Came to this site by
- 150cc mopeds: That was a superb blog
- Jason: if the situation is like
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