Internationalization of sessions:

Source: Internet
Author: User

Internationalization of sessions:

Other links: http://blog.csdn.net/jinwufeiyang/article/details/52338268

How can we make other content (such as menus and titles) of our website internationalized? This is what we will discuss in this article-> internationalization.

Add the following content to the spring. xml file of the project:

 

[Html]View plain copy
  1. <Mvc: interceptors>
  2. <Span style = "white-space: pre"> </span> <! -- If the international operation interceptor is based on (Request/Session/Cookie), it must be configured. -->
  3. <Bean class = "org. springframework. web. servlet. i18n. LocaleChangeInterceptor"/>
  4. </Mvc: interceptors>

 

Add the myproperties. properties, myproperties_zh _. properties, and myproperties_en _. properties files to the source folder resources of the project.

 

The following is some simple information about the jsp page, which is not considered in the Demonstration:

 

[Html]View plain copy
  1. <% @ Page language = "java" contentType = "text/html; charset = UTF-8"
  2. PageEncoding = "UTF-8" %>
  3. <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <Html>
  5. <Head>
  6. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  7. <Title> Insert title here </title>
  8. </Head>
  9. <%
  10. Locale name = (Locale) session. getAttribute ("i18nlanguage ");
  11. ResourceBundle myResourcesBundle = ResourceBundle. getBundle ("myproperties", name );
  12. %>
  13. <Body>
  14. <A href = "$ {pageContext. request. contextPath}/index/findex. do? LangType = en & page = Home "> ENG </a> |
  15. <A href = "$ {pageContext. request. contextPath}/index/findex. do? LangType = zh & page = Home "> <% = myResourcesBundle. getString (" simplified ") %> </a>
  16. </Body>
  17. </Html>


The code for the background Action layer is as follows:

 

 

[Html]View plain copy print?
  1. Package com. zhidao. oms. index;
  2. Import java. util. Locale;
  3. Import javax. servlet. http. HttpServletRequest;
  4. Import org. springframework. stereotype. Controller;
  5. Import org. springframework. web. bind. annotation. RequestMapping;
  6. Import org. springframework. web. bind. annotation. RequestParam;
  7. @ Controller
  8. @ RequestMapping ("/index ")
  9. Public class IndexAction {
  10. @ RequestMapping ("/findex ")
  11. Public String Findex (HttpServletRequest request, @ RequestParam String langType, String page ){
  12. If (langType. equals ("zh ")){
  13. Locale locale = new Locale ("zh", "CN ");
  14. Request. getSession (). setAttribute ("i18nlanguage", locale );
  15. }
  16. Else if (langType. equals ("en ")){
  17. Locale locale = new Locale ("en", "US ");
  18. Request. getSession (). setAttribute ("i18nlanguage", locale );
  19. } Else {
  20. Request. getSession (). setAttribute ("i18nlanguage", Locale. getDefault ());
  21. }
  22. Return "/front/" + page + ". jsp ";
  23. }
  24. }


Let's test the presentation! I hope everyone will criticize and correct the poor writing.

Related Article

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.