code download
http://pan.baidu.com/s/1sjNQmfF
Maven dependencies
<properties> <springframework>4.0.5.RELEASE</springframework> </properties> <dependencies> <!-- Spring web mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework}</version> </dependency> </dependencies>
In Spring applications, the configuration of internationalization is relatively simple. The following four steps complete the rapid configuration of internationalization.
The first step is to configure messageSource and localeResolver
<!-- Configure internationalized resource file path--> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <!-- Define the relative path of the message resource file--> <value>messages/message</value> </property> </bean> <!-- Cookie-based localization parser--> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieMaxAge" value="604800"/> <property name="defaultLocale" value="zh_CN"/> <property name="cookieName" value="Language"></property> </bean> <!-- Session-based localization parser--> <!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver
The second step, write message_*.properties
message_en.properties
hi=hello something=The People's Republic of China Chinese=Chinese English=English index=Index welcome=Welcome OtherPage=Other Page
message_zh_CN.properties ( Chinese characters have been converted to unicode )
hi=\u4F60\u597D something=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD Chinese=\u4E2D\u6587 English=\u82F1\u6587 OtherPage=\u5176\u4ED6\u9875\u9762 index=\u9996\u9875 welcome=\u6B22\u8FCE
The third step, the page introduces the spring tag library
introduce
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
use
<spring:message code="welcome"></spring:message>
The fourth step, switch language
//@Autowired SessionLocaleResolver resolver; /** * Language switch */ @RequestMapping("language") public ModelAndView language(HttpServletRequest request,HttpServletResponse response,String language){ language=language.toLowerCase(); if(language==null||language.equals("")){ return new ModelAndView("redirect:/"); }else{ if(language.equals("zh_cn")){ resolver.setLocale(request, response, Locale.CHINA ); }else if(language.equals("en")){ resolver.setLocale(request, response, Locale.ENGLISH ); }else{ resolver.setLocale(request, response, Locale.CHINA ); } } return new ModelAndView("redirect:/"); }
The internationalization configuration has been completed. Please note the difference between SessionLocaleResolver and CookieLocaleResolver. Obviously, the session can only be valid for the current session, and the cookie is valid for the session within the validity period of the cookie. When using a cookie, you need to set the expiration time of the cookie. Otherwise, after closing the browser, the cookie will be invalid and the purpose will not be achieved. Of course, the language setting information of the user can also be saved to the database, and the user can change the language to the language set by the user after logging in.