Struts Internationalization (i18n)


Struts Internationalization (i18n) can be done with some handy modifications in our existing application. We have to know the two Internationalization (i18n) components that are packaged with the Struts Framework. The first of these components, which is managed by the application Controller, is a Message class that references a resource bundle containing Locale-dependent strings. The second Internationalization (i18n) component is a JSP custom tag, <bean:message />, which is used in the View layer to present the actual strings managed by the Controller.

In this section we will move with an example to understand the whole process. We are continuing the same example which we used earlier to understand the simple struts example in the section Struts Example.

First thing we will require for Internationalization (i18n) is a set of simple Java properties files. Each file contains a key/value pair for each message that you expect your application to present, in the language appropriate for the requesting client.

This property file contains the key/value pairs for the default language of your application. The naming format for this file is ResourceBundleName.properties. An example of this default file, using English as the default language, would be ApplicationResources.properties A sample entry in this file would be app.name=Name, this tells Struts that for every occurrence of the app.name key the Name will be substituted.

We must define a properties file for each language that your application will use. This file must follow the same naming convention as the default properties file, except that it must include the two-letter ISO language code of the language that it represents. Example of this naming convention
For an German-speaking client would be ApplicationResources_de.properties
For an French-speaking client would be ApplicationResources_fr.properties
For an Italian-speaking client would be ApplicationResources_it.properties
For an Spanish-speaking client would be ApplicationResources_es.properties

Now add the respective entries in each properties files you require.

After you define all of the properties files for your application, you need to make Struts aware of them. It is achieved by adding a <message-resources> sub-element to the struts-config.xml file. Then you should copy all the resource bundles into the application classpath, /WEB-INF/classes/example, and then use the package path plus the base file name as the value of the <message-resources> subelement. The following snippet shows an example of using the <message-resources> subelement to configure a resource bundle, using the properties files described above

<message-resources parameter="example.ApplicationResources"/>

Once this part is done we customise the view part , this is achieved throught the second i18n component defined by the Struts Framework is a JSP custom tag, <bean:message />, which is used to present the actual strings that have been loaded by the Controller.

To use the <bean:message />, we must first deploy the bean tag library, which contains the <bean:message /> tag. Deploying a tag library is a very simple process that requires only the addition of a new <taglib> entry in the web.xml file of the Web application using the bean library. Here is an example of this
entry:

<taglib>
  <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

Also check that the struts-bean.tld file is copied to the /WEB-INF/ folder.


<bean:message /> tag and how it is configured for use.

Now we are done we will check step by step of our saple application.

1. WeCreate the resource bundles that will contain the key/value pairs used in your application. For our application, we will have four properties files that contain our resource bundles.

The German ApplicationResources_de.properties file.
app.name=Name
app.hello=Hallo

The French ApplicationResources_fr.properties file.
app.name=Nom
app.hello=Bonjour

The Italian ApplicationResources_it.properties file.
app.name=Nome
app.hello=Ciao

The Spanish ApplicationResources_fr.properties file.
app.name=Nombre
app.hello=Hola

The English ApplicationResources.properties file.
app.name=Name
app.hello=Hello

2. Copy all of the properties files to the /WEB-INF/classes/example directory.
Add an application <message-resources /> subelement, naming the wiley. ApplicationResources to the struts-config.xml file, as shown

<struts-config>
  <form-beans>
    <form-bean name="nameForm" type="example.NameForm"/>
  </form-beans>
  <action-mappings>
    <action path="/Name" type="example.NameAction" name="NameForm" >
      <forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
    </action>
  </action-mappings>
   
  <message-resources parameter="example.ApplicationResources"/>
</struts-config>

3. Modify the web.xml file as discussed above by adding

<taglib>
  <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

4. Modify the index.jsp file
index.jsp

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>Sample Struts Application</title>
</head>
<body>
  <html:form action="Name" name="nameForm" type="example.NameForm">
    <table width="80%" border="0">
      <tr>
        <td><bean:message key="app.name" />:</td>
        <td><html:text property="name" /></td>
      </tr>
      <tr>
        <td><html:submit /></td>
      </tr>
    </table>
  </html:form>
</body>
</html>

Modify the diplayname.jsp
displayname.jsp


<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>Sample Struts Display Name</title>
</head>
<body>
    <table width="80%" border="0">
      <tr>
        <td><bean:message key="app.hello" /><%= request.getAttribute("NAME") %> !!</td>
      </tr>
    </table>
</body>
</html>

So we are done with Internationalization (i18n) you need to open your Web browser to the following URL:
http://localhost:port/example/



Struts
MVC
Downloading Struts
Installing Struts
Configuring Struts
Struts 1.0 and 1.1
Struts Controller
Struts Model
Struts View
Struts Tag Library
Struts Example
Struts Internationalization

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions