Internationalization of struts2

Source: Internet
Author: User

I have seen i18n before, and now I know that i18n is internationalization, because it starts with I and ends with N, and has 18 letters in total, that is, the meaning of internationalization. I didn't see the switch between Chinese and English on the baidu search homepage. After all, Baidu was designed for the Chinese in China and there is no need to switch between English and Chinese. There are some on the Google search homepage, because Google's servers in China are deployed in Hong Kong, so there is also a traditional switch. How is this done? In fact, the principle is very simple, and it is easy to implement with struts2. The following example describes how to implement a switchover between Chinese and English:

Step 1:Add two resource files under the src directoryThe naming method is very important. Format: basename_language_country.properties. There are also two naming methods, both of which can be used. This does not need to be studied too much. For example, we need to add resource_zh_cn.properties and resource_en_us.properties to implement a switchover between Chinese and English. Then add the key-value pairs to these two files, and the key fields must be unified. For example:

Here, the value can use placeholders, such as: Hello {0}. As for how to pass the parameter, I will explain it later.

Step 2:Configure the Struts. properties FileThis file is also stored in the src directory.

The first key-value pair is used to tellProgramThe international resource file is a resource file named after loginresource.

In fact, the configuration is complete, and the next step is to use it in various places.

(1) Use in JSP. Here are two examples.

<S: Text name = "login. Title"> </S: Text>

<S: textfield name = "username" Key = "login. username"> </S: textfield>

You can also output information with placeholders. For example, if there is a login. Welcome = Hello {0}, you can use the following method to display it:

<S: Text name = "login. Welcome"> <s: param> <s: property value = "username"/> </S: param> </S: Text>

(2) used in action. For example:

Gettext ("login. username ");

You can also use information with placeholders. For example, if there is a login. Welcome = Hello {0}, you can use the following method to display it:

String Params [] = {"James "};

String welcome = gettext ("login. Welcome", Params );

Step 3,Switch between Chinese and English on the JSP page.

Index. jspCodeAs follows:

 1        <  A  Href  = "Lang. Action? Request_locale = zh_cn"  > Chinese </  A  >  2         <  A  Href  = "Lang. Action? Request_locale = en_us"  > English </  A >  3         4         <  S: Form  ID  = "Loginform"  Action  = "Login"  Method  = "Post"  >  5             <  S: textfield  Name  = "Username" Key  = "Login. username"  > </  S: textfield  >  6             <  S: textfield  Name  = "PSD"  Key  = "Login. PSD"   > </  S: textfield  >  7             < S: Submit  Key  = "Login. Submit"  > </  S: Submit  >  8         </  S: Form  > 

Mainly look at the first two lines of code, Lang. action is actually an action that implements page conversion. There is no substantive content. You can define a helloaction at will. java: Return success in the execute () method. The request_locale parameter in the URL is read by the i18n interceptor, and then the language environment is set based on the value. The i18n interceptor is the default interceptor in struts.

The Struts. xml configuration is as follows:

 1           <  Action Name  = "Login"  Class  = "Com. Main. Action. loginaction"  >  2               <  Exception-Mapping  Result  = "Error"  Exception  = "Java. Lang. runtimeexception"  > </  Exception-Mapping  >  3              <  Result  Name  = "Success"  > /Success. jsp </  Result  >  4               <  Result  Name  = "Error"  > /Index. jsp </  Result >  5               <  Result  Name  = "Input"  > /Index. jsp </  Result  >  6               <  Result  Name  = "Regist"  > /Regist. jsp</  Result  >      7           </  Action  >  8           9           <  Action  Name  = "Lang"  Class  = "Com. Main. Action. helloaction"  >  10              <  Result  Name  = "Success"  > /Index. jsp </  Result  >  11           </  Action  > 

Oh, it's basically done. The next step is to see the results. Run:

 

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.