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
<!-- 配置国际化资源文件路径 -->
<
bean
id
=
"messageSource"
class
=
"org.springframework.context.support.ResourceBundleMessageSource"
>
<
property
name
=
"basename"
>
<!-- 定义消息资源文件的相对路径 -->
<
value
>messages/message</
value
>
</
property
>
</
bean
>
<!-- 基于Cookie的本地化解析器 -->
<
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的本地化解析器 -->
<!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />-->
|
The second step is to write the 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 ( Kanji has been converted to Unicode code)
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
Introduced
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> |
Use
< spring:message code = "welcome" ></ spring:message > |
fourth Step, switch languages
@Autowired
CookieLocaleResolver resolver;
//@Autowired SessionLocaleResolver resolver;
/**
* 语言切换
*/
@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 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