International support for STRUTS2

Source: Internet
Author: User
Tags gettext i18n

Each box price will have international support, struts2 internationalization is broadly divided into the internationalization of the page, action internationalization and the internationalization of XML

First, add the following in the Struts.properties file:
Struts.custom.i18n.resources=messageresource
Or join in the Struts.xml
<constant name= "struts.custom.i18n.resources" value= "Messageresource" ></constant>

Resource file Naming format: Name _ Language Code _ Country code. Properties
If you create internationalization of Chinese and English, the resource file name is called
Messageresource_zh_cn.properties and Messageresource_en_us.properties

1. The internationalization of JSP pages
/> output internationalization by using the label <s:text name= "Label.helloworld"
Label.helloworld the key defined in the resource file


Add the following in Messageresource_en_us.properties
Label.hello=hello {0}
Label.helloworld=hello,world

Add the following in Messageresource_zh_cn.properties
label.hello= Hello {0}
label.helloworld= Hello, world

(1). <s:text name= "Label.helloworld"/>
<s:property value= "%{gettext (' Label.helloworld ')}"/>
The above two are both representations of the output of a Hello Word

<s:textfield name= "name" key= "Label.helloworld"/>
<s:textfield name= "name" label= "%{gettext (' Label.helloworld ')}"/>
Displays a text box with the title of the text box internationalized

(2). Specify the data from a particular resource file using the <s:i18n> tag
<s:i18n name= "Messageresource" >
<s:text name= "Label.helloworld" ></s:text>
</s:i18n>
Specifies that resources are fetched from the Messageresource

(3).
<s:text name= "Label.hello" >
<s:param>callan</s:param>
</s:text>
Use resource .<s:param> with parameters to replace {0} in Label.hello=hello {0}

2. Internationalization of Action
The internationalization of the action is primarily the Java code    Public string execute () implemented through the GetText (String key) method  throws  exception {                             // gettext (String)  string to key              string str1 = gettext (" Label.helloworld ");              system.out.println ( STR1);                             //  with parameters                string str2 = gettext ("Label.hello", new string[]{"FJF"});              system.out.println (str2);                        //  As with the previous implementation                list l = new arraylist ();               l.add ("Callan");              string str3 = gettext ("Label.hello", L);               system.out.println (STR3);                              return SUCCESS;          }  

3. Parameterized internationalization
Add the following in Messageresource_en_us.properties
Username=username
Username.required=${gettext (' UserName ')} is required

Add the following in Messageresource_zh_cn.properties
Username= User Name
Username.required=${gettext (' UserName ')} cannot be empty

In the action
String STR4 = GetText ("username.required");
System.out.println (STR4);

Username.required=${gettext (' UserName ')} will take the internationalized user name

4. When using the check box price, the hint information can be internationalized
<field name= "UserName" >
<field-validator type= "Requiredstring" >
<message key= "username.required" > </message>
</field-validator>
</field>

The


internationalized resource file is divided into three levels
(1)   Global resource files, which can be referenced by the entire program, that is, the file specified by Struts.custom.i18n.resources=messageresource
(2   Package-level resource files, a new resource file can be created under the root of each package and only accessed by classes in the current package. File name format is: Package_ language code _ Country code.
(3)  action level resource file, only the current action reference, name action Name _ language code _ Country code
Lookup order for small to large range, action-level priority maximum  

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.