Search from Struts2 Library

Monday, September 22, 2008

The i18n and text tags

Many applications need to work in multiple languages. The process of making this happen is called internationalization, or i18n for short. (There are 18 letters between the I and the N in the word internationalization.) Chapter 11 discusses Struts 2’s internationalization support in detail, but we’d like to take a moment to detail the two tags that are central to this functionality: the i18n tag and the text tag.
The text tag is used to display language-specific text, such as English or Spanish, based on a key lookup into a set of text resources. This tag retrieves a message value from the ResourceBundles exposed through the framework’s own internationalization mechanisms. For now, we’ll just note the usage of the tag; it takes a name attribute that specifies the key under which the message retrieval should occur. The framework’s default Locale determination will determine the Locale under which the key will be resolved.


You can also name an ad hoc ResourceBundle for resolving your text tags. If you want to manually specify the ResourceBundle that should be used, you can use the i18n tag. Table below lists the attributes of the i18n tag.


Here’s a quick example that shows how to set the ResourceBundle with the i18n tag and then extract a message text from it with the text tag:

<s:i18n name="manning.chapterSix.myResourceBundle_tr">
In <s:text name="language"/>,
<s:text name="girl" var="foreignWord"/>
</s:i18n>
"<s:property value="#foreignWord"/>" means girl.



The i18n tag simply specifies a resource bundle to use. The bundle is only used during the body of the tag. However, as our example demonstrates, you can “persist” a message from the bundle using the text tag’s var attribute to set the message to the ActionContext as a named reference. This usage of the var attribute should be more familiar by now. The first text tag writes the message associated with the key “language” directly to the output. The second text tag stores the message associated with the key “girl” under the reference name “foreignWord”.
These tags are simple, but seem out of context without full knowledge of the uiltininternationalization features of Struts 2. In particular, you’ll need to know how the framework locates, loads, and uses properties file ResourceBundles.

No comments: