| < Day Day Up > |
|
Now that we have seen the components involved in internationalizing a Struts application, we can apply them to our wroxapp application. In this section, we take you through the step-by-step process for internationalizing a Struts Web application. Each of these steps is described below:
Create the resource bundles that will contain the key/value pairs used in your application. For our application, we have two properties files that contain our resource bundles. These properties files appear in Listings 9.1 and 9.2.
Listing 9.1: The Italian ApplicationResources_it_IT.properties file.
app.symbol=Simbolo app.price=Prezzo Corrente
Listing 9.2: The English ApplicationResources_en_US.properties file.
app.symbol=Symbol app.price=Current Price
Copy these properties files to the <CATALINA_HOME>/webapps/wroxapp/ WEB-INF/classes/ directory.
Add an ActionServlet <init-param> sub-element, naming the ApplicationResources, to the web.xml file, as shown in the following snippet:
<init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param>
Add a <taglib> entry describing the bean tag library to the application's web.xml file, as shown in Listing 9.3.
Listing 9.3: The modified web.xml file.
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <!-- Standard ActionServlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Standard ActionServlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Standard Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> </web-app>
Make sure that you are using the <load-on-startup> element when describing the ActionServlet. This ensures that all of the key/value pairs are loaded prior to any requests.
Modify your JSP files to include a taglib directive referencing the bean tag library and replace all text strings presented to the user with matching <bean:message /> tags. Listings 9.4 and 9.5 show our modified JSPs. Notice that all of the former static strings have been placed in the properties files, listed earlier, and are now referenced using a <bean:message /> tag with the appropriate key.
Listing 9.4: The internationalized index.jsp.
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <html> <head> <title>Wrox Struts Application</title> </head> <body> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> <tr> <td height="68" width="48%"> <div align="left"> <img src="/books/2/574/1/html/2/images/wxmainlogowhitespace.gif"> </div> </td> </tr> <tr> <td> </td> </tr> </table> <html:form action="Lookup"> <table width="45%" border="0"> <tr> <td><bean:message key="app.symbol" />:</td> <td><html: text property="symbol" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit /></td> </tr> </table> </html:form> </body> </html>
Listing 9.5: The internationalized quote.jsp.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <html> <head> <title>Wrox Struts Application</title> </head> <body> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> <tr> <td height="68" width="48%"> <div align="left"> <img src="/books/2/574/1/html/2/images/wxmainlogowhitespace.gif"> </div> </td> </tr> <tr> <td> </td> </tr> <tr> <td> </td> </tr> <tr> <td> </td> </tr> <tr> <td> <bean:message key="app.price" />: <%= request.getAttribute("PRICE") %> </td> </tr> <tr> <td> </td> </tr> </table> </body> </html>
That's all there is to it. To see these changes take effect, restart Tomcat and open the following URL:
http://localhost:8080/wroxapp/
You should see results that look exactly like your previous encounters with the wroxapp application, except that now all user-presented strings are retrieved from the ApplicationResources.properties file that matched the requesting client's Locale.
| < Day Day Up > |
|