Struts2.0 Internationalization

Source: Internet
Author: User

This article is transferred from: http://blog.sina.com.cn/s/blog_4934a04a0100dbkp.html

Thanks to the original author for sharing, I feel that the summary is good and I will post it for future use.

Struts2 is internationalized in three situations: Internationalization of foreground pages, internationalization of Action classes, and internationalization of configuration files.

First, specify the global resource file:

Introduce

<Constant name = "struts. Custom. i18n. Resources" value = "message"> </constant> (Note the location)

Or

Specify the following line in the Struts. properties file:

Struts. Custom. i18n. Resources = message

 

The specified state-based resource file is

Xxx _ language _ country. Properties

 

Message_zh_cn.properties (Simplified Chinese resource file)

Message_en_us.properties (US English resource file)

 

 

(1). Internationalization on the JSP page (using struts2 <s: Text.../> ):

 

<S: i18n name = "message">
 <S: Text name = "hello">
   <S: Param >$ {username} </S: param>
 </S: Text>
</S: i18n>

 

Message_en_us.properties file Configuration:

Hello = Hello world, {0}

Message_zh_cn.properties file Configuration:

Hello = Hello, {0}

 

(2) the label of the form element is internationalized:

Not internationalized:

<S: textfield name = "username" label = "username"> </S: textfield>

<S: textfield name = "password" label = "password"> </S: textfield>

 

After internationalization:

<S: textfield name = "username" Key = "uname"> </S: textfield>

<S: textfield name = "password" Key = "pword"> </S: textfield>

 

Message_en_us.properties file, Configuration:

Uname = Username

Pword = Password

 

Message_zh_cn.properties file, Configuration:

Uname = User Name

Pword = Password

 

 

(3) Internationalization in. Action:

Not internationalized:

This. addfielderror ("username", "the username error! ");

This. addfielderror ("password", "The Password error! ");

 

After internationalization:

This. addfielderror ("username", "username. error ");

This. addfielderror ("password", "password. error ");

 

Message_en_us.properties file Configuration:

Username. Error = the username error!

Password. Error = the Password error!

Message_zh_cn.properties file Configuration:

Username. Error = username error!

Username. Error = incorrect password!

 

(4). internationalization in the configuration file:

Take the input validation LoginAction-validation.xml as an example:

Not internationalized:

<Field name = "username">
<Field-validator type = "requiredstring">
<Param name = "trim"> true </param>
<Message> username shocould not be empty! </Message>
</Field-validator>

<Field-validator type = "stringlength">
<Param name = "minlength"> 6 </param>
<Param name = "maxlength"> 12 </param>
<Message> username shocould be between $ {minlength} and $ {maxlength }! </Message>
</Field-validator>
</Field>

 

After internationalization:

<Field name = "username">
<Field-validator type = "requiredstring">
<Param name = "trim"> true </param>
<Message key = "username. Empty! "> </Message>
</Field-validator>

<Field-validator type = "stringlength">
<Param name = "minlength"> 6 </param>
<Param name = "maxlength"> 12 </param>
<Message key = "username. Size"> </message>
</Field-validator>
</Field>
 

Message_en_us.properties file Configuration:

Username. Empty = the usernameShocould not be empty!

Username. size = the size of username shoule be between 6 and 12!

Password. Empty = the password shocould not be empty!

Password. size = the size of password shoule be between 6 and 12!

 

Message_zh_cn.properties file Configuration:

Username. Empty = username cannot be blank!

Username. size = username length: 6 to 12!

Password. Empty = password cannot be blank!

Password. size = the password length is 6 to 12!

Note: The international resource file message_zh_cn.properties cannot contain non-Western European characters..

Java provides a tool to process the Chinese characters in the file: native2ascii. This tool can be found under % java_home %/bin.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.