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依赖
|
1
2
3
4
5
6
7
8
9
10
11
|
<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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 配置国际化资源文件路径 --> <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
|
1
2
3
4
5
6
7
|
hi=hellosomething=The People's Republic of ChinaChinese=ChineseEnglish=Englishindex=Indexwelcome=WelcomeOtherPage=Other Page |
message_zh_CN.properties(汉字已转成unicode码)
|
1
2
3
4
5
6
7
|
hi=\u4F60\u597Dsomething=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FDChinese=\u4E2D\u6587English=\u82F1\u6587OtherPage=\u5176\u4ED6\u9875\u9762index=\u9996\u9875welcome=\u6B22\u8FCE |
第三步,页面引入spring标签库
引入
|
1
|
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> |
使用
|
1
|
<spring:message code="welcome"></spring:message> |
第四步,切换语言
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@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即失效了,没有达到目的。当然,也可以保存用户的语言设置信息到数据库,用户登录之后即可将语言改变成用户设置的语言。
运行效果截图:





spring 国际化i18n配置的更多相关文章
- Spring之i18n配置与使用
Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...
- spring 国际化-i18n
i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大 ...
- springMVC项目国际化(i18n)实现方法
SpringMVC项目国际化(i18n)实现方法 按照作息规律,每周五晚必须是分享知识的时间\(^o^)/~,这周讲点儿啥呢,项目需要逼格,咱们国际化吧(* ̄rǒ ̄)~,项目中碰到这类需求的童鞋可能并 ...
- Spring国际化
国际化(Internationalization)有时候被简称为i18n,因为有18个字母在国际化的英文单词的字母i和n之间.Spring对国际化的支持示例如下所示. 需要将spring.tld放到工 ...
- 【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 ...
- spring mvc+myBatis配置详解
一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...
- spring boot所有配置
转载 http://blog.csdn.net/lpfsuperman/article/details/78287265 # 日志配置# 日志配置文件的位置. 例如对于Logback的`classpa ...
- 基于注解的Spring多数据源配置和使用
前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...
随机推荐
- markdown常用命令(持续整理更新...)
编写使用的工具 VS Code 拥有丰富插件支持的代码编辑器,当然也支持markdown MdEditor一款在线编辑markdown网站 1.标题 示例: # 一级标题 ## 二级标题 ### 三级 ...
- linux 特殊命令(二)
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...
- Oracle之基础操作
sqlplus常用命令: 进入sqlplus模式:sqlplus /nolog 管理员登录: conn / as sysdba 登录本机的数据库 conn sys/123456 as sysdba 普 ...
- 查看Pyton的版本号和32/64位平台
怎么查看Python的版本号?使用的Python是32位还是64位的?用以下两条Python 指令就可以知道. 方法1:通过Python代码查看 import platform import sys ...
- hadoop 1.x 集群环境的搭建
本文主要以个人工作学习总结为主,同时也为了方便更多的兴趣爱好者参与学习交流,现将具体的搭建步骤分享如下: 一.基础环境 1.1 jdk的安装与配置 Hadoop是用Java开发的,Hadoop的编译及 ...
- ACM1000:A + B Problem
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to ...
- 使用bison和yacc制作脚本语言(1)
使用bison和yacc制作脚本语言(1) 环境: 环境 windows 10 Cygwin64 语言 C 工具 mingw bison flex 主要是使用bison和flex这两个软件,编译器无所 ...
- 北京Uber优步司机奖励政策(12月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- COGS 2199. [HZOI 2016] 活动投票
2199. [HZOI 2016] 活动投票 ★★ 输入文件:hztp.in 输出文件:hztp.out 简单对比时间限制:0.5 s 内存限制:2 MB [题目描述] 衡中活动很多, ...
- Updating Homebrew... 长时间不动解决方法
确保你已安装Homebrew 依次输入下面的命令(注意:不要管重置部分的命令,这里原作者贴出来.我也贴出来是以防需要重置的时候有参考操作命令) 替换brew.git: cd "$(brew ...