Internationalizing the Mini HR Application

 < Day Day Up > 



Now that you've seen how Struts' internationalization support works, you are ready to revisit the Mini HR application and update it to support internationalization. Following is the list of steps involved in adding internationalization support to Mini HR:

  1. Add entries for all application text to the ApplicationResources.properties file.

  2. Create a Spanish version of the ApplicationResources.properties file.

  3. Update JSPs to retrieve all application text from the ApplicationResources.properties file.

  4. Repackage and run the updated application.

The following sections walk through each step of the process in detail.

Add Entries for All Application Text to the ApplicationResources.properties File

The first step in updating Mini HR to support internationalization is to add entries for all application text to the ApplicationResources.properties file. As you'll see, having all application text in this file allows it to be easily translated into other languages. The ApplicationResources.properties file from the original Mini HR application in Chapter 2 already contains some of the application's text; however, all text needs to be placed in this file for internationalization to work.

Following is the updated ApplicationResources.properties file in its entirety:

# Title Resources title.application=ABC, Inc. Human Resources Portal title.employee.search=Employee Search # Link Resources link.employee.add=Add an Employee link.employee.search=Search for Employees # Label Resources label.search.name=Name label.search.ssNum=Social Security Number # Error Resources error.search.criteria.missing=<li>Search Criteria Missing</li> error.search.ssNum.invalid=<li>Invalid Social Security Number</li> error.search.not.found=No Employees Found errors.header=<font color="red"><b>Validation Error(s)</b></font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true">

Notice that there are several new entries. All of the text from the JSPs has been moved into this file so that the application can have both an English and a Spanish version.

Create a Spanish Version of the ApplicationResources.properties File

Once entries for all of the application text have been put in the ApplicationsResources .properties file, you have to create a Spanish version of the file to support Spanish users. Do this by creating the ApplicationResources_es.properties file, shown here:

# Title Resources title.application=ABC, Inc. Recursos Humanos Porta title.employee.search=Búsqueda Del Empleado # Link Resources link.employee.add=Agregue a empleado link.employee.search=Búsqueda para los empleados # Label Resources label.search.name=Nombre label.search.ssNum=Número De la Seguridad Social # Error Resources error.search.criteria.missing=<li>El Faltar De los Criterios De la Búsqueda</li> error.search.ssNum.invalid=<li>Número Inválido De la Seguridad Social</li> error.search.not.found=Ningunos Empleados Encontraron errors.header=<font color="red"><b>Error De la Validación(s)</b></font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true">

Notice that this file's name is only slightly different from ApplicationResources .properties in that it has _es appended to the ApplicationResources part of the original filename. The _es denotes which locale the file is for-in this case, the Spanish language. You could, of course, include a country code along with the language code to make the file specific to a certain country/language combination. This file contains all the same entries as the ApplicationResources.properties file, but the property values are in Spanish. The property names are universal across all locales' files. That's how applications reference the text.

Note 

The translations in this section were performed using Google's translation service and are for demonstration purposes only.

Update JSPs to Retrieve All Application Text from the ApplicationResources.properties File

After you have updated ApplicationResources.properties and created an ApplicationResources_es.properties file, you have to update Mini HR's JSPs. The original JSPs have a mix of hard-coded text and text that is dynamically inserted into the page from the ApplicationResources.properties file. In order to support internationalization, all of the hard-coded text has to be moved into the ApplicationResources.properties file so that it can be obtained dynamically, based on locale. Following are the updated JSPs, index.jsp and search.jsp, with all the hard-coded text replaced with <bean:message> tags that dynamically insert the text into the page.

index.jsp:

<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %> <html> <head> <title><bean:message key="title.application"/></title> </head> <body> <font size="+1"><bean:message key="title.application"/></font><br> <hr width="100%" noshade="true"> &#149; <bean:message key="link.employee.add"/><br> &#149; <html:link forward="search"> <bean:message key="link.employee.search"/></html:link><br> </body> </html>

search.jsp:

<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %> <html> <head> <title> <bean:message key="title.application"/> - <bean:message key="title.employee.search"/> </title> </head> <body> <font size="+1"> <bean:message key="title.application"/> - <bean:message key="title.employee.search"/> </font><br> <hr width="100%" noshade="true"> <html:errors/> <html:form action="/search"> <table> <tr> <td align="right"><bean:message key="label.search.name"/>:</td> <td><html:text property="name"/></td> </tr> <tr> <td></td> <td>-- or --</td> </tr> <tr> <td align="right"><bean:message key="label.search.ssNum"/>:</td> <td><html:text property="ssNum"/> (xxx-xx-xxxx)</td> </tr> <tr> <td></td> <td><html:submit/></td> </tr> </table> </html:form> <logic:present name="searchForm" property="results"> <hr width="100%" size="1" noshade="true"> <bean:size  name="searchForm" property="results"/> <logic:equal name="size" value="0"> <center><font color="red"><b> <bean:message key="error.search.not.found"/> </b></font></center> </logic:equal> <logic:greaterThan name="size" value="0"> <table border="1"> <tr> <th><bean:message key="label.search.name"/></th> <th><bean:message key="label.search.ssNum"/></th> </tr> <logic:iterate  name="searchForm" property="results"> <tr> <td><bean:write name="result" property="name"/></td> <td><bean:write name="result" property="ssNum"/></td> </tr> </logic:iterate> </table> </logic:greaterThan> </logic:present> </body> <html>

Note that the Bean Tag Library definition had to be added to index.jsp so that it could use the <bean:message> tag to source in text.

Repackage and Run the Updated Application

Because no Java source code files had to be modified to update the Mini HR application to support internationalization, you do not have to recompile the application. All you have to do is repackage the application and redeploy it before running it again. Assuming that you've made modifications to the original Mini HR application and it was set up in the c:\java\MiniHR directory (as described in Chapter 2), the following command line will repackage the application:

jar cf MiniHR.war *

This command should be run from the directory where you have set up the Mini HR application (e.g., c:\java\MiniHR).

To test the Spanish version of the application, you have to change your browser's language settings to Spanish. Following are the instructions for doing this with Microsoft Internet Explorer 6:

  1. Open Internet Explorer's Internet Options dialog box by selecting Tools | Internet Options.

  2. Click the Languages button on the General tab of the Internet Options dialog box, as shown here:

    click to expand

  3. Click the Add button in the Language Preference dialog box.

  4. Add the Spanish (United States) [es-us] language preference by selecting it in the list, as shown here, and then clicking OK.

  5. Select the Spanish (United States) [es-us] language preference and click Move Up so that it is the first preference in the list, like this:

Once you have added the Spanish language setting for your browser, you need to place the new MiniHR.war file that you just created into Tomcat's webapps directory and start Tomcat. As before, to access the Mini HR application, point your browser to http://localhost:8080/MiniHR/. When you run the updated Mini HR application with your browser set to Spanish, it should detect that you are accessing it from a Spanish-language browser and automatically serve the Spanish version of the application. Figure 10-1 shows the search page in Spanish.

click to expand
Figure 10-1: The Spanish version of the search page.



 < Day Day Up > 



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2003
Pages: 134
Authors: James Holmes

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net