The i18i custom tag library contains tags that help manage the complexity of creating international web applications. These tags provide similar (though not identical) functionality to the internationalization available in the struts framework, but do not require adopting the entire struts framework.
For those not familiar with the term i18n, it comes from the fact that the word "internationalization" starts with the letter I, ends with the letter N and has 18 letters.
This custom tag library requires
Follow these steps to configure your web application with this tag library:
i18n/i18n.tld
)
to the /WEB-INF
subdirectory of your web application.
i18n/i18n.jar
) to the
/WEB-INF/lib
subdirectory of your web application.
<taglib>
element to your web application
deployment descriptor in /WEB-INF/web.xml
like this:
<taglib> <taglib-uri>http://jakarta.apache.org/taglibs/i18n</taglib-uri> <taglib-location>/WEB-INF/i18n.tld</taglib-location> </taglib>
To use the tags from this library in your JSP pages, add the following directive at the top of each page:
<%@ taglib uri="http://jakarta.apache.org/taglibs/i18n" prefix="x" %>where "x" is the tag name prefix you wish to use for tags from this library. You can change this value to any prefix you like.
This custom tag library includes several tags, with the following characteristics:
This tag loads the bundle for the provided baseName and locale. If a locale is not specified this tag senses the users browser preference and loads the bundle for the appropriate locale. If their first request is not found, it looks for their second preference, and so on. If none of these are found, it uses the default locale specified by the java runtime. This tag is required for at the beginning of the request that uses a message or require tag. (sub-pages included in a request pages do not need to use this tag)
Example:
<i18n:bundle baseName="com.wamnet.tools.jsptags.i18n-test" />
where there is a i18n-test.properties file in the com/wamnet/tools/jsptags package.
Attribute | Description | Required |
---|---|---|
baseName | This attribute tells the system where to find the ResourceBundle.
For example: "com.wamnet.tools.petstore" where there is a petstore.properties file in the com/wamnet/tools package. |
Yes |
locale | Provides a Locale object to use when looking up the bundle.
This is useful, for example, for sites that use databases to store user information that includes what language to display the site in. | No |
localeAttribute | Specifies a request, page, session, or even application attribute that
contains a Locale object to use when looking up the bundle.
This is useful, for example, for sites that allow users to select the language on when they enter the site. | No |
This tag looks up a key in the localize bundle and formats the value for display to the user. Any body inside the tag is displayed only if a value is not found for the key.
Examples:
<i18n:message key="test1" />
<i18n:message key="test2">
this only displayed <em>if</em> 'test2' not found.
</i18n:message>
<i18n:message key="test1" args="<%= objectArray %>" />
Attribute | Description | Required |
---|---|---|
key | This attribute is used to do a lookup on the ResourceBundle for the localized text to display. | Yes |
args | Allows setting an array of objects to use for arguments. This is useful when an array is already handy. Generally arguments will be added with the nested "messageArg" tag. If args are specified, any arguments provided by the nested messageArg tag will be added to the array. | No |
This tag works with the message tag to use the java.text.MessageFormat object based on the user locale. It only works when nested inside of a message tag. In the example below, text2 is mapped to "{0,date,short} {1,number,currency}" which automatically formats the date and currency according to the defaults for that locale, but a given locale could override that if it wanted to be explicit.
Examples:
<i18n:message key="test2">
<i18n:messageArg value="<%= dateArg %>" />
<i18n:messageArg value="<%= numberArg %>" />
</i18n:message>
Attribute | Description | Required |
---|---|---|
value | This attribute is to replace a positional variable in the message text. See the javadoc api for java.text.MessageText | Yes |
This tag is used to provide specific handling for locales that have or don't have a defined value for the specified key. This is the logical opposite of the ifdef tag.
Examples:
<i18n:ifdef key="specialDisclaimer">
<H3>DISCLAIMER</H3>
<i18n:message key="specialDisclaimer" />
Only those locales that have a specialDisclaimer
key
in
their ResourceBundle will see the special disclaimer text.
</i18n:ifdef>
Attribute | Description | Required |
---|---|---|
key | The key that must be defined in order for the body of the tag to be evaluated. | Yes |
This tag is used to provide specific handling for locales that have or don't have a defined value for the specified key. This is the logical opposite of the ifdef tag. While it is possible to simulate this functionality with the messageTag, this tag never displays the value of the key.
Examples:
<i18n:ifndef key="specialDisclaimer">
<H3>DISCLAIMER</H3>
<i18n:message key="disclaimer" />
Only those locales that do NOT have a specialDisclaimer
key
in
their ResourceBundle will see the standard disclaimer text.
</i18n:ifndef>
Attribute | Description | Required |
---|---|---|
key | The key that must be NOT defined in order for the body of the tag to be evaluated. | Yes |