1. First, build the struts2 environment,
2. Create an action. Test i18n.
3. Below is a simple configuration of Struts. XML, which contains the configuration of the properties file in 2. One is global and the other is local,
XML Code
- <? XML
Version = "1.0" encoding = "UTF-8"
?>
- <! Doctype struts public
- "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
- Http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <Struts>
- <Constant
Name = "struts. devmode"
Value = "true"/>
- <! -- Local configuration -->
- <! --
- <Constant name = "struts. Custom. i18n. Resources" value = "com/test/Action/i18n"> </constant>
- -->
- <! -- Global configuration -->
- <! -->
- <Constant
Name = "struts. Custom. i18n. Resources"
Value = "test"> </Constant>
- <Package
Name = "default" namespace = "/"
Extends = "struts-Default">
- <Action
Name = "" class = "com. Test. Action. i18naction">
- <Result
>/Index. jsp</Result>
- </Action>
- </Package>
- </Struts>
4. According to the struts2 configuration, the plug-in is a configuration file named test_en_us.properties and test_zh_cn.properties,
The content in test_en_us.properties is: Hello = Hi, hello
The content in test_zh_cn.properties is: Hello = \ u4f60 \ u597d (Note: This is compiled by encoding. You can also use the properties of myeclipse to automatically edit the conversion ).
5. The following is the JSP display page: I have sorted out the following implementation methods,
HTML code
- <% @ Page
Language = "Java" Import = "Java. util .*"
Pageencoding = "UTF-8" %>
- <% @ Taglib
Prefix = "S" uri = "/Struts-tags" %>
- <%
- String Path = request. getcontextpath ();
- String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
- %>
-
- <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
- <HTML>
- <Head>
- <Base
Href = "<% = basepath %>">
-
- <Title>My JSP 'index. jsp 'starting page</Title>
- <Meta
HTTP-equiv = "Pragma"
Content = "no-Cache">
- <Meta
HTTP-equiv = "cache-control"
Content = "no-Cache">
- <Meta
HTTP-equiv = "expires"
Content = "0">
- <Meta
HTTP-equiv = "keywords"
Content = "keyword1, keyword2, keyword3">
- <Meta
HTTP-equiv = "Description"
Content = "this is my page">
- </Head>
-
- <Body>
- <
Href = "<% = basepath %>? Local = zh_cn">Chinese</A>
- <
Href = "<% = basepath %>? Local = en_us">English</A>
- This is my JSP page.<Br>
- <S: Debug> </S: Debug>
- Property:<S: Property
Value = "gettext ('hello ')"/> <Br>
- Text:<S: Text
Name = "hello"> </S: Text> <br>
- I18n:<S: i18n
Name = "test">
- <S: Text
Name = "hello"> </S: Text>
- </S: i18n>
- </Body>
- </Html>
6. to switch between Chinese and English, add this sentence to the action.
Java code
- Locale locale = new locale ("ZH", "cn"); // (this can be dynamically changed based on your incoming values)
- Servletactioncontext. getrequest (). getsession (). setattribute ("ww_trans_i18n_locale", locale );
7. Basically, dynamic link switching can be implemented in both Chinese and English. However, there is still a small problem that needs to be solved. That is, you need to click 2 Chinese to switch to Chinese,
The same is true in English. How can this problem be solved?
8. It is actually very easy to solve the problem. Just configure a fitler Interceptor. It is best to configure this interceptor in front of the struts2 interceptor,
The content of the interceptor is:
Java code
- String local = arg0.getparameter ("local ");
- If (local! = NULL ){
- String loc [] = Local. Split ("_");
- Locale locale = new locale (loc [0], Loc [1]);
- (Httpservletrequest) arg0). getsession (). setattribute ("ww_trans_i18n_locale", locale );
- }
- Arg2.dofilter (arg0, arg1 );
In this way, you can dynamically switch between Chinese and English.