i18n Components of a Struts Application

 < Day Day Up > 



Two main i18n components 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 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.

The Controller

The standard method used when internationalizing a Struts application begins with the creation of 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.

Defining the Resource Bundles

A resource bundle is a file that contains the key/value pairs for the default language of your application. The naming format for this file is ResourceBundleName_language_COUNTRY.properties. An example of this default file, using English in the United States, would be:

 ApplicationResources_en_US.properties 

All resource requests from a client in the United States speaking the English language will use this file to retrieve its application-specific key/values pairs.

A sample entry in this file would be:

 app.symbol=Symbol 

This combination says that when a client, using the previous en_US Locale, requests the key app.symbol the value Symbol is substituted for every occurrence of the app.symbol key. You see how these keys are used when you get to the View section of this chapter.

When developing an i18n application, you must define a properties file for each language that your application will use. This file must follow the same naming convention as the previous properties file, except that it must include the Locale code of the language and country that it represents. An example of this naming convention for an Italian-speaking client in Italy would be:

 ApplicationResources_it_IT.properties 

And a sample entry in this file would be:

 app.symbol=Simbolo 

This file will be used by all clients in Italy using the Italian language. You can find all of Java supported Locales at http://java.sun.com/j2se/1.4.1/docs/guide/intl/locale.doc.html.

start sidebar

The ApplicationResources.properties files are loaded upon application startup. If you make changes to this file, you must reload the properties file, either by restarting the entire container or by restarting the Web application referencing the properties files.

end sidebar

Deploying the Resource Bundles

Once you have defined all of the properties files for your application, you need to make Struts aware of them. This is accomplished using one of the ActionServlet's <init-parameter> tags. The parameter that is used is the application parameter. To make our instance of the ActionServlet aware of our properties files, we need to modify the ActionServlet's <servlet> definition (found in the web.xml file) to look like the following snippet:

 <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>l</load-on-startup> </servlet> 

This <init-param> sub-element tells the Struts Controller that all of our properties files exist in the <CATALINA_HOME>/webapps/wroxapp/WEB-INF/classes/ directory and are named ApplicationResources_xx_XX.properties.

start sidebar

Notice that we are not using a package name in our <param-value> sub-element. This is because we are using the default package. If you were to place your properties files into a package structure, then you would need to prepend this structure just like any other class file.

end sidebar

Now all you need to do is copy all of your resource bundles into the application classpath, which in this case is the <CATALINA-HOME>/webapps/wroxapp/WEB-INF/classes/ directory, and restart Tomcat.

The View

To actually leverage these new resource bundles, you must use Struts's second i18n component. 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. This section describes the <bean:message /> tag and how to configure it.

Deploying the bean Tag Library

Before you can use <bean:message />, you must first deploy the bean tag library. Deploying a Struts tag library is a simple process that requires only the addition of a new <taglib> entry in the web.xml file. Here is the entry that you should add:

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

This entry simply tells the JSP/servlet container that this Web application uses a tag library, which exists in the classpath and is described by the TLD located in the <CATALINA_HOME>/webapps/wroxapp/WEB-INF/struts-bean.tld file. To make this a true statement, you need to copy this TLD, struts-bean.tld, from the Struts archive to this directory and make sure the struts.jar file exists in the <CATALINA_HOME>/webapps/wroxapp/WEB-INF/lib directory.

Using the <bean:message /> Tag

The <bean:message /> tag is a useful tag that retrieves keyed values from a previously defined resource bundle and displays them in a JSP. The <bean:message /> tag defines nine attributes and has no body. Of these nine attributes, we are interested in only the first: key. The key attribute is the unique value used to retrieve a message from the previously defined resource bundle. The key attribute is a request-time attribute that is required.

To see the <bean:message /> tag in action, let's see how it is used. The following code snippet contains a simple example of using the <bean:message /> tag:

 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <html>   <head>     <title><bean:message key="app.title"/></title>   </head>   <body>   </body> </html> 

As you look over the previous snippet, you will see two lines in bold. We need to focus on these two areas. The first of these lines is a JSP taglib directive that must be included by all JSPs that will use the <bean:message /> tag.

start sidebar

The URI defined in the previous taglib directive should match the <taglib-uri> defined in the previously defined web.xml file.

end sidebar

The second line that we need to look at is the actual <bean:message /> tag. The <bean:message /> instance that we use in this snippet contains the key attribute; it retrieves the value stored in the resource bundle that is referenced by the key app.title and substitutes it for the occurrence of the <bean:message /> tag. The result of this is a JSP that will have an HTML <title> that matches the Locale of the requesting client.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

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