Struts 2: Internationalization of resources

Source: Internet
Author: User

Struts 2: Internationalization of resources


Add the following content to the struts. properties file:
Struts. custom. i18n. resources = messageResource
Or add it to struts. xml.

    

Resource file naming format: name_language code_country code. Properties
If you create Chinese and English internationalization, the resource file name is
MessageResource_zh_CN.properties and messageResource_en_US.properties
There are three levels of international resource files
(1) The global resource file can be referenced by the whole program, that is, the file specified by struts. custom. i18n. resources = messageResource.
(2) package-level resource file. You can create a new resource file under the root directory of each package, which is only accessed by the class in the current package. The file name format is package _ language code _ country code.
(3) Action-level resource file, which is only referenced by the current Action and named as action name_language code _ Country Code
The search sequence is from small to large, and the Action-level priority is the highest.

Jsp page internationalization by using tags The output is international. label. helloWorld is the key defined in the resource file.
Add the following content to messageResource_en_US.properties:
label.hello=hello{0}   label.helloWorld=hello,world   

Add the following content to messageResource_zh_CN.properties:
Label. hello = hello {0} label. helloWorld = hello, world

(1 ).
   

Both of the above output two hello word representations.
   
   

Display a text box. The title of the text box is internationalized.
(2). Use Tag specifies to retrieve data from a specific resource file
              
     
    

Specify to retrieve resources from messageResource
(3 ).

                     
    
     clf
      
    

Use resources with parameters. Replace {0} in label. hello = hello {0}

Internationalization of actions
The internationalization of Action is mainly implemented through the getText (Stringkey) method.
Java code
Public String execute () throws Exception {// getText (String) string is key String str1 = getText ("label. helloWorld "); System. out. println (str1); // String str2 = getText ("label. hello ", new String [] {" clf "}); System. out. println (str2); // same as the previous implementation List l = newArrayList (); l. add ("callan"); String str3 = getText ("label. hello ", l); System. out. println (str3); return SUCCESS ;}

The following content is added to message_en_US.properties for internationalization of action Errors:
Username. invalid = usernameinvalid...
Add the following content to message_zh_CN.properties:
username.invalid=\u7528\u6237\u540d\u4e0d\u5408\u6cd5...

Modify the validate method in RegisterAction and add the error to ActionError. Here, the getText method in ActionSupport is used to obtain information related to the international resource file.
Take username verification as an example:
if (null ==username || username.length() < 5 || username.length() > 10) {       this.addActionError(this.getText("username.invalid"));   }  

In this way, the username. invalid value is read from the resource file and added to ActionError.


International verification framework (field-level error)
Add the following content to the message_en_US.properties file:
username.xml.invalid=validateinformation

Add the following content to the message_zh_CN.properties file:
username.xml.invalid=\u9a8c\u8bc1\u6846\u67b6\u4fe1\u606f

Modify the verification framework and add the content in the properties file to the framework.
Take username as an Example

          
               
      true          
            
       
      



Add the property key to the message tag and set the value to the key in the properties file.
Most of the keys in tags are related to internationalization.

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.