Struts2 international language switch

Source: Internet
Author: User
Tags i18n

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

  1. <? XML
    Version = "1.0" encoding = "UTF-8"
    ?>
  2. <! Doctype struts public
  3. "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
  4. Http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <Struts>
  7. <Constant
    Name = "struts. devmode"
    Value = "true"/>
  8. <! -- Local configuration -->
  9. <! --
  10. <Constant name = "struts. Custom. i18n. Resources" value = "com/test/Action/i18n"> </constant>
  11. -->
  12. <! -- Global configuration -->
  13. <! -->
  14. <Constant
    Name = "struts. Custom. i18n. Resources"
    Value = "test"> </Constant>
  15. <Package
    Name = "default" namespace = "/"
    Extends = "struts-Default">
  16. <Action
    Name = "" class = "com. Test. Action. i18naction">
  17. <Result
    >
    /Index. jsp</Result>
  18. </Action>
  19. </Package>
  20. </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

  1. <% @ Page
    Language = "Java" Import = "Java. util .*"
    Pageencoding = "UTF-8" %>
  2. <% @ Taglib
    Prefix = "S" uri = "/Struts-tags" %>
  3. <%
  4. String Path = request. getcontextpath ();
  5. String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
  6. %>
  7.  
  8. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
  9. <HTML>
  10. <Head>
  11. <Base
    Href = "<% = basepath %>">
  12.  
  13. <Title>My JSP 'index. jsp 'starting page</Title>
  14. <Meta
    HTTP-equiv = "Pragma"
    Content = "no-Cache">
  15. <Meta
    HTTP-equiv = "cache-control"
    Content = "no-Cache">
  16. <Meta
    HTTP-equiv = "expires"
    Content = "0">
  17. <Meta
    HTTP-equiv = "keywords"
    Content = "keyword1, keyword2, keyword3">
  18. <Meta
    HTTP-equiv = "Description"
    Content = "this is my page">
  19. </Head>
  20.  
  21. <Body>
  22. <
    Href = "<% = basepath %>? Local = zh_cn">Chinese</A>
  23. <
    Href = "<% = basepath %>? Local = en_us">English</A>
  24. This is my JSP page.<Br>
  25. <S: Debug> </S: Debug>
  26. Property:<S: Property
    Value = "gettext ('hello ')"/> <Br>
  27. Text:<S: Text
    Name = "hello"> </S: Text> <br>
  28. I18n:<S: i18n
    Name = "test">
  29. <S: Text
    Name = "hello"> </S: Text>
  30. </S: i18n>
  31. </Body>
  32. </Html>

 

 

6. to switch between Chinese and English, add this sentence to the action.

Java code

  1. Locale locale = new locale ("ZH", "cn"); // (this can be dynamically changed based on your incoming values)
  2. 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

  1. String local = arg0.getparameter ("local ");
  2. If (local! = NULL ){
  3. String loc [] = Local. Split ("_");
  4. Locale locale = new locale (loc [0], Loc [1]);
  5. (Httpservletrequest) arg0). getsession (). setattribute ("ww_trans_i18n_locale", locale );
  6. }
  7. Arg2.dofilter (arg0, arg1 );

In this way, you can dynamically switch between Chinese and English.

 

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.