Use Struts 2 for internationalization

Source: Internet
Author: User
Tags i18n

Struts2 internationalization (I18N)

Internationalization is also called I18N, which is short for Internationalization. Struts2 internationalization is based on Java internationalization, but the Struts2 framework further encapsulates Java internationalization, which simplifies the internationalization of applications.

Use struts2 for internationalization

On the basis of the previous "use Struts2 Checker to verify User Registration Information" project, achieving internationalization: http://www.cnblogs.com/likailan/p/3302284.html

 

1. Specify the base name of the resource file in strute. xml.

Add the following code to the struts tag of the strute. xml file:

         

The resource file is a properties file, and the naming rule is: Base name_ _ region code. properties. The base name is the "message" set in step 1 ". You can view the language code and region code in IE: Open IE> open internet Options> click language> click Add. The following "add language" dialog box is displayed, the code for displaying various languages and regions is displayed:

Add the Chinese-Chinese and English-American resource files under the src directory. The corresponding file name is:

Chinese-China: message_zh_CN.properties

English-US: message_en_US.properties

Message_zh_CN.properties code:

 

Message_en_US.properties code:

 

3. Display JSP page information internationally

Replace the text related to the display with <s: text name = "corresponding to the resource file key"/>. The attribute name value is the corresponding key in the resource file. For example, <s: text name = "title"/> <s: text name = "form. email"/>

Page code:

                                                                                                                                                                                                                                                                                                                                                                                                                                      "/>                                                               

Iv. Test

Start tomcat. Use IE to open the register. jsp page.

Choose internet option> language> Add. The "add language" dialog box is displayed. Add "English (US) [en-US]" and move the item to the top

Refresh page:

 

Message_zh_CN.properties:

 

Message_en_US.properties:

 

Ii. modify the configuration file of the Verification Framework

Modify the configuration file of the verification framework, remove the specific verification error information, and specify the corresponding verification error information through the key attribute of the <message> tag.

Modified RegisterAction-register-validation.xml:

                                                                                                                                                                                                                   6                                                                                                                                                                                

Iv. Test

 

Choose internet option> language> Add. The "add language" dialog box is displayed. Add "English (US) [en-US]" and move the item to the top. Refresh page:

Convert page language-region through hyperlink

1. Add the following hyperconnections to the register. jsp page:

Simplified Chinese English

Jump to a changelan action and pass the parameter request_locale (fixed write). The value is the codenamed region.

2. Compile the corresponding action class

ChangelanAction. java code:

         ChangelanAction        String execute()                   Locale locale=                  ActionContext.getContext().getSession().put("WW-TRANS-I18N-LOCALE"                }

3. Configure struts. xml

Add the following action tag to the package Tag:

    register.jsp

Iv. Test

The page is as follows:

Click Simplified Chinese:

Click English

Related Article

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.