Web Development

Internationalize your Struts application for multiple locales

You may be familiar the power of the Java-based Struts Framework to makes your code easily reusable. Now see how you can leverage Struts to internationalize content. Walk through the property files and the JSP modifications.

Struts allows you to internationalize your application easily for multiple locales (which combine languages and geographic locations). By using built-in features in Struts, you can modify your existing Struts application to be multilingual in four steps:
  1. ·        Modify your JSP pages.
  2. ·        Create a default properties file that associates each keyword with text.
  3. ·        Create a properties file for each additional language you will support.
  4. ·        Configure your Struts XML file to find the properties files.

Here, I will change the JSP in Listing A to handle different languages.

Overview of Struts
For more information on Struts, check out these articles on Builder.com:

Step 1: JSP modifications
You will need to make three changes in each JSP that will support multiple languages.

First, include the struts-bean and struts-html tag libraries if not already present. Second, add the line <html:html locale=”true”>. This defaults the locale to the value of Accept_language in the request header. Lastly, replace all text with a “<bean:write key=””> tag. It is useful to prefix your keywords to indicate what JSP uses which keywords. In Listing B, I used the prefix poker.

Step 2: Default properties file
Create a default application.properties file. Properties files are text files that contain a series of keywords and values. Note the keywords are identical to the key values specified in the <bean:message> tags that you added to the JSP files in step 1. Struts will use the default application.properties file if no locale-specific properties file exists, as shown in Listing C.

Step 3: Additional properties files
Create a properties file for each language you will support. Certain languages will require you to use Unicode-encoded characters (e.g., /u0084).

The _de suffix indicates the locale. The suffixes are based on ISO standards 3166 and 639. If you use the ISO standards, Struts will load the correct files for the user’s locale.

I don't know German, so I used the Babel Fish Web site to translate the text (see Listing D).

Step 4: Configuration
Specify the location of your properties files, Struts html, and bean tag libraries in your web.xml. The location is relative to the application’s location. The param-value in Listing E tells the ActionServlet that the properties files can be found in WEB-INF/classes/resources/, or as a JAR file in WEB-INF/lib.

In Struts 1.1, you can specify the location of the properties files in the struts-config.xml file. In this example, I would add this line:
 
<message-resources parameter="resources.application"/>

Test it out
Figure A shows the sample Struts page.

Figure A
Sample English page


Set your preferred language to German in your Web browser, restart your browser, and refresh the page to see the new page shown in Figure B below.

Figure B
Sample German page


Going further
The Struts ActionServlet makes available a session-scoped Locale object that can be manipulated programmatically. You can also create new locales. This allows you to present text in several languages on one page or allow the user to select a locale.

Struts has many tags besides <bean:message> that support different locales. Struts 1.1has increased tags that support localization. For example, Struts 1.1 added several properties to <bean:write> to allow localized formatting. Explore the standard Struts tags and plug-ins for additional localization features.
0 comments

Editor's Picks