Understanding Struts Internationalization Support

 < Day Day Up > 



Understanding Struts' Internationalization Support

Now that you have reviewed Java's built-in support for internationalization, you are ready to see how internationalization works in Struts. Struts is similar to Java in that internationalization support was a base requirement and not an afterthought. Struts was designed from the ground up to provide strong support for internationalizing an application. This is one of its most valuable features. For the most part, Struts' internationalization support is built on top of Java's. However, Struts uses some of its own facilities for internationalization where necessary.

Like Java, Struts' internationalization support is centered on the Locale class and resource bundles. However, Struts differs in that it uses its own custom set of classes for managing and accessing resource bundles. The Struts org.apache.struts .util.MessageResources class and its descendant class, org.apache.struts.util .PropertyMessageResources, provide functionality parallel to the ResourceBundle and PropertyResourceBundle classes built into Java. Struts uses these custom classes to manage resource bundles because the built-in Java classes are not serializable. Because Struts applications can be deployed in distributed (i.e., clustered) application server environments, Struts requires that any objects it manages be serializable (that is, they must implement the java.io.Serializable interface). Because Java's ResourceBundle classes are not serializable, Struts has created its own classes that duplicate their functionality and are serializable.

The following three sections cover the specifics of how internationalization works in Struts, starting with an overview of how Struts handles locale, followed by an overview of Struts' message resources, and concluding with a section on how Struts' tag libraries support internationalization.

Locale

Like other Java applications, Struts applications use Locale objects to store users' locale settings and to customize content based on those settings. Struts determines a user's locale from the HTTP requests made to the application. When making a request to a Web server, browsers pass along an HTTP header (Accept-Language) that indicates the user's locale. Java's javax.servlet.http.HttpServletRequest request object makes this locale setting available to servlets via a getLocale( ) method, which returns a Locale object containing the settings from the request.

When a user first accesses a Struts application, Struts captures the user's locale and stores it in the user's session. Storing the locale in the session allows it to be easily accessed throughout the Struts framework from one convenient place. A side benefit of placing the Locale object in a user's session (instead of simply requesting it each time from a request) is that applications can update the locale settings for a user independent of the browser settings. For example, assume that a user accesses an application from a browser with English set as its locale. You can change the user's locale (say to French) without making the user change the browser settings by simply placing another Locale object into the user's session. Struts would then operate based on the new locale because Struts determines locale settings from the object stored in the session, not the settings passed in on each request.

Conversely, sometimes it's necessary to override Struts' default behavior of storing locale settings in the session. For example, your application may want to always use the locale settings specified in the request. That way if the user changes their browser settings, Struts will pick up the change. To do this, you have to configure Struts not to store locales in the session by setting the controller element's locale attribute to false in your application's Struts configuration file, as shown here:

<controller locale="false"/>

Message Resources

As you saw in the example application in Chapter 2, message resources are a fundamental building block in Struts applications whether you're intending to internationalize your application or not. Struts uses message resources as a means of dynamically inserting text into JSPs as well as for storing error messages that get returned from data validations. By using message resources in this way, it's easy to internationalize an application, whether it is an initial requirement for your application or an enhancement. You simply store all of your application's text in resource bundles. Then, when it comes time to internationalize the application, all you have to do is create locale-specific versions of the resource bundle. Struts transparently handles selecting the right resource bundle from which to obtain resources based on the users' locales.

To use message resources, you must create a properties file for your resources to be stored in. The standard name that Struts applications use for this file is ApplicationResources .properties; however, you can use any filename with an extension of .properties. Like Java's built-in PropertyResourceBundle class, PropertyMessageResources requires that locale-specific versions of the file specify the locale information in the filename. For example, to create a French version of the ApplicationResources.properties file, you would create a file named ApplicationResources_fr.properties. Also in parallel with Java's version, PropertyMessageResources will attempt to load resources for a locale from the corresponding locale-specific resource bundle. If the resource bundle does not exist or the resource does not exist in the bundle, Struts will attempt to load the resource from the default resource bundle.

Once you have created a properties file, it has to be placed somewhere on your application's classpath (i.e., somewhere beneath the /WEB-INF/classes/ directory or inside a .jar file underneath the /WEB-INF/lib directory). This is necessary because Struts uses Java's class loader to load the properties file. This is very important. If the file is not in your application's classpath, Struts will not be able to access the file. Next, you have to configure Struts to know where the file is located. This is done by placing a message-resources element in your application's Struts configuration file, as shown next:

<!-- Message Resources Configuration --> <message-resources   parameter="com.jamesholmes.minihr.ApplicationResources"/>

The parameter attribute of the message-resources element informs Struts of the location of your application's resource bundle. Notice that the directory of the file is specified using Java's package notation with dots. Thus, the resource bundle in the preceding example is in a directory named com/jamesholmes/minihr that is somewhere in the classpath, and the resource bundle filename is ApplicationResources. Struts automatically appends .properties to the filename specified with the parameter attribute. If you mistakenly specify the .properties extension, Struts will not be able to locate your file.

You can define multiple resource bundles in the Struts configuration files by using multiple message-resources elements. Each additional resource bundle beyond the main, default bundle has to specify a bundle name using the key attribute, as shown next:

<!-- Message Resources Configuration --> <message-resources   parameter="com.jamesholmes.minihr.ApplicationResources"/> <message-resources         key="alternate"   parameter="com.jamesholmes.minihr.AlternateResources"/>

The main resource bundle does not specify a name because it is the default bundle. Additional bundles have to specify a name so that they can be explicitly used by name by the application.

After you have created a resource bundle and configured Struts to use it, you can make use of it in your application a couple ways. First, you can dynamically insert text into JSPs by using the Bean Tag Library's message tag, as shown here:

… <head> <title><bean:message key="searchPage.title"/></title> </head> …

This example dynamically inserts the value of the searchPage.title resource into the JSP at run time. Second, you can also return a set of messages from an action to a JSP by using Struts' ActionMessages class.

When your application first accesses a resource bundle, Struts loads the bundle into memory and caches it so that it can be quickly accessed again. Because of this, if you modify your resource bundles while your application is running, you will have to restart your application server before you can see the changes.

Struts' Tag Library Support for Internationalization

Several of Struts' tag library tags support internationalization by way of allowing certain attributes' values to be specified as keys to properties in a message resources bundle. At run time, Struts uses the keys to look up a corresponding value and then uses the value for the given attribute. The following table lists each of the tags and their attributes that support internationalization.

Library

Tag

Attribute

Bean

message

key

Bean

write

formatKey

HTML

button

altKey

HTML

button

titleKey

HTML

cancel

altKey

HTML

cancel

titleKey

HTML

checkbox

altKey

HTML

checkbox

titleKey

HTML

file

altKey

HTML

file

titleKey

HTML

frame

titleKey

HTML

hidden

altKey

HTML

hidden

titleKey

HTML

image

altKey

HTML

image

pageKey

HTML

image

srcKey

HTML

image

titleKey

HTML

img

altKey

HTML

img

pageKey

HTML

img

srcKey

HTML

img

titleKey

HTML

link

titleKey

HTML

multibox

altKey

HTML

multibox

titleKey

HTML

option

key

HTML

password

altKey

HTML

password

titleKey

HTML

radio

altKey

HTML

radio

titleKey

HTML

reset

altKey

HTML

reset

titleKey

HTML

select

altKey

HTML

select

titleKey

HTML

submit

altKey

HTML

submit

titleKey

HTML

text

altKey

HTML

text

titleKey

HTML

textarea

altKey

HTML

textarea

titleKey

Note 

Several of Struts' tags have a bundle attribute that allows you to specify the name of a bundle from which values are loaded. That way you're not limited to putting all of your internationalized properties into a single resource bundle.



 < 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