spring 国际化-i18n
i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。 在全球化的时代,国际化尤为重要,因为产品的潜在用户可能来自世界的各个角落。通常与i18n相关的还有L10n(“本地化”的简称)。<摘自百度 百科http://baike.baidu.com/view/372835.htm?fr=aladdin >
代码下载
http://pan.baidu.com/s/1sjNQmfF
Maven依赖
<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>
在Spring应用中,国际化的配置比较简单,下面四步完成国际化的快速配置
第一步,配置messageSource和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" />-->
第二步,编写message_*.properties
message_en.properties
hi=hellosomething=The People's Republic of ChinaChinese=ChineseEnglish=Englishindex=Indexwelcome=WelcomeOtherPage=Other Page |
message_zh_CN.properties(汉字已转成unicode码)
hi=\u4F60\u597Dsomething=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FDChinese=\u4E2D\u6587English=\u82F1\u6587OtherPage=\u5176\u4ED6\u9875\u9762index=\u9996\u9875welcome=\u6B22\u8FCE |
第三步,页面引入spring标签库
引入
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> |
使用
<spring:message code="welcome"></spring:message> |
第四步,切换语言
@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:/");
}
已完成国际化的配置,其中请注意SessionLocaleResolver和CookieLocaleResolver的区别,很显然,通过 Session只能对当前的会话有效,Cookie则对Cookie有效期内的会话都有效。在使用Cookie的时候,需要设置Cookie的过期时间, 否则关闭浏览器之后,Cookie即失效了,没有达到目的。当然,也可以保存用户的语言设置信息到数据库,用户登录之后即可将语言改变成用户设置的语言。
<来源:http://www.xdemo.org/spring-i18n/ >
spring 国际化-i18n的更多相关文章
- spring 国际化i18n配置
i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无需做大的改 ...
- springMVC项目国际化(i18n)实现方法
SpringMVC项目国际化(i18n)实现方法 按照作息规律,每周五晚必须是分享知识的时间\(^o^)/~,这周讲点儿啥呢,项目需要逼格,咱们国际化吧(* ̄rǒ ̄)~,项目中碰到这类需求的童鞋可能并 ...
- Spring国际化
国际化(Internationalization)有时候被简称为i18n,因为有18个字母在国际化的英文单词的字母i和n之间.Spring对国际化的支持示例如下所示. 需要将spring.tld放到工 ...
- Spring之i18n配置与使用
Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...
- 【spring 国际化】springMVC、springboot国际化处理详解
在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...
- 关于Spring 国际化 No message found under code 的解决方案
用spring做国际化时经常会报: org.springframework.context.NoSuchMessageException: No message found under code 'u ...
- 国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...
- javaweb学习总结(三十一)——国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...
- Spring国际化模块
1.Spring3.1.0实现原理分析(二).国际化(i18n) https://blog.csdn.net/roberts939299/article/details/69666291
随机推荐
- C#实时读取数据----局部页面刷新【转】
I)现在刚开始学习C#,对一些基本的控件了解的不够,有个实时监控的系统,需要页面中的数据每5秒钟刷新一次, 要是每5秒钟页面全部的刷新,那页面根本就没法看了,对这个问题在CSDN上也专门开了帖子,问了 ...
- mongodb简单安装
参考文档: http://www.cnblogs.com/hanyinglong/archive/2016/07/21/5690611.html conf文件: dbpath = /usr/local ...
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table【递推】
A. Maximum in Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- shell 文件夹总大小 du -sh 文件夹
du -sh 文件夹 du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件> ...
- 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...
- JNuit
s1:@Test s2:根据提示导包 s3:选中方法名,右击run as -->JUnit Test package songyan.jdbc.learn1; import org.junit. ...
- IE8兼容性问题
由于业务的需要,我们竟然还要支持IE8,听着就让人很心酸呀.不过在进行适配的过程中,会发现还是有一定规律的,基本上帮相关问题改了,页面也就能正常显示了.下面就总结下对IE8适配过程中所进行的修改. 1 ...
- Windows 8.1中WinRT的变化(二)——新增功能
首先我们来看看现有控件中新增的功能: FlipView编程方式切换时支持平滑滚动: 在Windows8中,FlipView在用手触控翻页的时候是有动画效果的,但当我们使用键盘或代码编程翻页时,却没有这 ...
- Delphi CRC算法, 不错
http://www.cnblogs.com/tangqs/archive/2011/12/08/2280255.html
- 【mybatis】mybatis中的<if test=“”>test中多条件
mybatis中的<if test=“”>test中多条件 代码展示: 其中 accountCode和apiName都是ApiAllRespBean的属性 <select id=&q ...