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=hello
something=The People's Republic of China
Chinese=Chinese
English=English
index=Index
welcome=Welcome
OtherPage=Other Page

message_zh_CN.properties(汉字已转成unicode码)

1
2
3
4
5
6
7
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

第三步,页面引入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配置的更多相关文章

  1. Spring之i18n配置与使用

    Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...

  2. spring 国际化-i18n

    i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大 ...

  3. springMVC项目国际化(i18n)实现方法

    SpringMVC项目国际化(i18n)实现方法 按照作息规律,每周五晚必须是分享知识的时间\(^o^)/~,这周讲点儿啥呢,项目需要逼格,咱们国际化吧(* ̄rǒ ̄)~,项目中碰到这类需求的童鞋可能并 ...

  4. Spring国际化

    国际化(Internationalization)有时候被简称为i18n,因为有18个字母在国际化的英文单词的字母i和n之间.Spring对国际化的支持示例如下所示. 需要将spring.tld放到工 ...

  5. 【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  6. 关于Spring 国际化 No message found under code 的解决方案

    用spring做国际化时经常会报: org.springframework.context.NoSuchMessageException: No message found under code 'u ...

  7. spring mvc+myBatis配置详解

    一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...

  8. spring boot所有配置

    转载 http://blog.csdn.net/lpfsuperman/article/details/78287265 # 日志配置# 日志配置文件的位置. 例如对于Logback的`classpa ...

  9. 基于注解的Spring多数据源配置和使用

    前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...

随机推荐

  1. 在SQL Server中批量修改有规律列的定义

    )=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...

  2. JDBC中执行sql语句的 增 , 删 , 改 , 查 的方法

    executeQuery()  : 执行 SELECT 语句,它几乎是使用最多的 SQL 语句 executeUpdate() :   执行 INSERT.UPDATE 或 DELETE 语句以及 S ...

  3. java 整型数据转换为小数类型 BigDecimal 装换为Double

    A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...

  4. [转]Javascript removeChild()删除节点及删除子节点的方法(同样适用于jq)

    Javascript removeChild()删除节点及删除子节点的方法 这篇文章主要介绍了Javascript removeChild()删除节点及删除子节点的方法的相关资料,需要的朋友可以参考下 ...

  5. php源码建博客5--建库建表-配置文件-错误日志

    主要: 整理框架 建库建表 配置文件类 错误日志记录 --------------本篇后文件结构:-------------------------------------- blog ├─App │ ...

  6. Django templates加载css/js/image等静态资源

    配置步骤: 1.首先在应用下面创建static目录 2.将静态资源拷贝进去 3.在应用的settings.py文件中添加 import os BASE_PATH = os.path.dirname(o ...

  7. docker swarm的应用----docker集群的构建

    一.docker安装 这里我们安装docker-ce 的18.03版本 yum    -y remove docker  删除原有版本 #安装依赖包 [root@Docker ~]# yum -y i ...

  8. 分享一个强大的makedown编辑器

    Yosoro 官网地址 https://yosoro.coolecho.net/ 很强大,支持直接粘贴图片,是直接上传到github仓库. 可直接导出md,html,pdf格式,特别方便 找了好几天的 ...

  9. win10每次重新启动,eclipse不能打开,要重新配jdk环境的解决办法

    在后面加上反斜杠就好,也不知道是什么原因,知道的同学希望可以在下面的评论告诉我.

  10. Excelファイルを扱う方法

    概要 データをローカルに落としたいという要件がある場合.ユーザーはExcelを希望するケースが多いだろう.そんな時は以下の汎用モジュールを使用して簡単に作る事ができます.使用方法は.GUI_UPLOA ...