Internationalizing Validations

 < Day Day Up > 

Similar to other areas of Struts, Validator fully supports internationalization. Remember that internationalization is the process of tailoring content to a specific locale or region. In Validator’s case, internationalization means tailoring validation error messages to a specific locale and/or tailoring actual validation routines to a specific locale. This way, the U.S. and French versions of a Web site can each have their own language-specific validation error messages. Similarly, internationalization enables the U.S. and French versions of a Web site to validate entries in monetary fields differently. The U.S. version requires commas to separate dollar values and a period to demarcate cents (i.e., 123,456.78), whereas the French (Euro monetary system) version requires periods to separate dollar amounts and a comma to demarcate cents (i.e., 123.456,78).

Tailoring validation error messages to a specific locale is built into Struts by way of its Resource Bundle mechanism for externalizing application strings, messages, and labels. You simply create an file for each locale you want to support. Each locale-specific properties file will have a locale identifier at the end of the filename that denotes which locale it is for, such as for Japan. (For detailed information on internationalizing a Struts application, see Chapter 10.)

Thus, when Validator goes to load an error message, it will use the locale for the current request to determine which properties file to load the message from. Remember that each validation rule in the validator-rules.xml file specifies a key for a validation error message stored in the files. This key is the same across each locale’s properties file, thus allowing Validator to load the appropriate message based on only a locale and a key.

Tailoring validation routines to specific locales is similar to tailoring error messages; you have to define validation definitions in the validation.xml file for each locale. The validation.xml file contains a form-validation tag that contains one or more formset tags, which in turn contain one or more form tags, and so on:

<!DOCTYPE form-validation PUBLIC           "-//Apache Software Foundation//DTD Commons             Validator Rules Configuration 1.0//EN"           ""> <form-validation>   <formset>     <form name="auctionForm">       <field property="bid" depends="mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset> </form-validation>

The form-set tag takes optional attributes, country and language, to tailor its nested forms to a specific locale. In the preceding example, all locales use the same currency validation to validate the “bid” property, because neither the country attribute nor the language attribute was specified for the form-set tag.

In order to have a generic currency validation for all users of the Web site and a Euro-specific currency validation for users from France, you’d create an additional form-set definition specifically for the French users, as shown here:

<!DOCTYPE form-validation PUBLIC           "-//Apache Software Foundation//DTD Commons             Validator Rules Configuration 1.0//EN"           ""> <form-validation>   <formset>     <form name="auctionForm">       <field property="bid" depends="required,mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset>   <formset country="fr">     <form name="auctionForm">       <field property="bid" depends="required,mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(\.?\d{3})*,?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset> </form-validation>

In this listing, the second <formset> definition specifies a country attribute set to “fr”. That instructs Validator to use the enclosed validation definitions for users with a French locale. Notice that the bid validation in the second <formset> definition is different from the first definition, as the period and comma are transposed in the mask value. Thus, the first <formset> definition validates that bids are in U.S. currency format and the second definition validates that bids are in Euro currency format.

A powerful feature of using internationalized <formset> definitions is that you can define only the validations that are locale-specific, and all other validations are taken from the default <formset> definition.

 < 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 © 2008-2017.
If you may any questions please contact us: