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. We create 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=HalloThe French ApplicationResources_fr.properties file:
app.name=Nom app.hello=BonjourThe Italian ApplicationResources_it.properties file:
app.name=Nome app.hello=CiaoThe Spanish ApplicationResources_es.properties file:
app.name=Nombre app.hello=HolaThe 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:
<%@ 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>
5. Modify the 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/
Prepared by RituAshar