Spring Internationalization-i18n

Source: Internet
Author: User
Tags i18n

I18N (whose source is the English word internationalization the first character I and n,18 as the middle character number) is the abbreviation of "internationalization". In the field of information, internationalization (i18n) means that products (publications, software, hardware, etc.) can be adapted to the needs of different languages and regions without big changes. For the program, in the case of not modifying the internal code, can be based on different languages and regions to display the corresponding interface. In an era of globalization, internationalization is especially important because potential users of products may come from all corners of the world. There are also l10n ("localized" abbreviations) commonly associated with i18n. < from Baidu Encyclopedia Http://baike.baidu.com/view/372835.htm?fr=aladdin >

code Download

Http://pan.baidu.com/s/1sjNQmfF

maven Dependency
<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>
Project

In spring applications, the internationalization of the configuration is relatively simple, the following four steps to complete the internationalization of rapid configuration

The first step is to configure Messagesource and Localeresolver
<!-- 配置国际化资源文件路径 --><beanid="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><propertyname="basename"><!-- 定义消息资源文件的相对路径 --><value>messages/message</value></property></bean><!-- 基于Cookie的本地化解析器 --> <beanid="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver">   <property name="cookieMaxAge"value="604800"/>   <propertyname="defaultLocale"value="zh_CN"/>   <propertyname="cookieName"value="Language"></property> </bean><!-- 基于Session的本地化解析器 --><!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />-->
The second step is to write the message_*.properties

Message_en.properties

hi=hellosomething=The People‘s Republic of ChinaChinese=ChineseEnglish=Englishindex=Indexwelcome=WelcomeOtherPage=Other Page

Message_zh_cn.properties ( Kanji has been converted to Unicode code)

hi=\u4F60\u597Dsomething=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FDChinese=\u4E2D\u6587English=\u82F1\u6587OtherPage=\u5176\u4ED6\u9875\u9762index=\u9996\u9875welcome=\u6B22\u8FCE
The third step, the page introduces the Spring tag library

Introduced

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

Use

<spring:messagecode="welcome"></spring:message>
fourth Step, switch languages
@AutowiredCookieLocaleResolver resolver;//@Autowired SessionLocaleResolver resolver;/** * 语言切换 */@RequestMapping("language")public ModelAndView language(HttpServletRequest request,HttpServletResponse response,String language){language=language.toLowerCase();if(language==null||language.equals("")){returnnewModelAndView("redirect:/");}else{if(language.equals("zh_cn")){resolver.setLocale(request, response, Locale.CHINA );}elseif(language.equals("en")){resolver.setLocale(request, response, Locale.ENGLISH );}else{resolver.setLocale(request, response, Locale.CHINA );}}returnnewModelAndView("redirect:/");}

The internationalized configuration has been completed, note the difference between Sessionlocaleresolver and Cookielocaleresolver, and it is clear that the session is valid only for the current session, and the cookie is valid for sessions within the cookie's validity period. When you use cookies, you need to set the expiration time of the cookie, otherwise the cookie expires after you close the browser, and does not achieve the purpose. Of course, you can also save the user's language settings information to the database, the user login can change the language into the user-set language.

Operating effect:

< source: http://www.xdemo.org/spring-i18n/>

Spring Internationalization-i18n

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.