Struts 2 Internationalization support

Source: Internet
Author: User
Tags constant 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 action is primarily achieved through the GetText (String key) method

Java Code   

1. public String execute () throws Exception {

2.

3.

4.

5.//GetText (String) string to key

6.

7. String str1 = GetText ("Label.helloworld");

8.

9. SYSTEM.OUT.PRINTLN (STR1);

10.

11.

12.

13.///with parameter

14.

String str2 = GetText ("Label.hello",new string[]{"FJF"});

16.

System.out.println (STR2);

18.

19.

20.

21.//As with the previous implementation

22.

List L = new ArrayList ();

24.

L.add ("Callan");

26.

A. String STR3 = GetText ("Label.hello", L);

28.

System.out.println (STR3);

30.

31.

32.

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.