i18n (internalisation) in Struts 2

Tags:

Problem summary

To be able to have a web site in multiple languages depending upon the users browser setting. If the browser was set to ‘en’ then the language would be English and if the browser was set to ‘hi’ (Hindi) then the language would be Hindi, you need to do the translation yourself first of course.

Download war file

It wasn’t hard to do but I get the feeling there must be an easier way that I haven’t worked out yet. I had to have a 20 minute hunt through the taglib docs for Struts 2 before settling on the i18n tag. Just put this into your code (I was using JSP):


<s:i18n name="i18n.package">
        <s:text name="text.running.struts2.i18n"></s:text>
</s:i18n>

The name attribute of the s:i18n tag references a file called package.properties that you will need to create. That file contains the text.running.struts2.i18n key that is set to the value you want to be displayed on the page.

So inside /WEB-INF/classes/i18n you place a file called package.properties that looked like this:


text.running.struts2.i18n=You are running with Struts 2.

I am sure it was easier in Struts 1 (as I don’t think the i18n tag was needed) but I haven’t found a way to do it in Struts 2 without that tag.

Comments:

Thanks a lot for this it is really useful.

Can you tell us how to create property files for different languages.

I would have thought you would create a file called package.properties in the package where the Struts actions are kept - this will be the default package used if no language is specified in the request. For each additional language create a file called package_it.properties (italian), package_ru.properties (russian) in the same folder as your package.properties. The browser will then send the requested language with the request and struts should map to the correct language file automatically.

Post a Comment:

HTML Syntax: Allowed