springMVC I18N 国际化

Spring XML 配置

 <!-- 国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <!-- 表示多语言配置文件在根路径下,以language开头的文件-->
    <property name="basename" value="classpath:language"/>
    <property name="useCodeAsDefaultMessage" value="true"/>
</bean>



  <!-- 配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用 -->
<bean id="localeResolver"  
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> 


    <!-- 配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象-->
<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  <!-- 自定义locael 拦截的名字 -->
        <property name="paramName" value="lang"/>
    </bean>
</mvc:interceptors>

jsp页面

 <%@ page language="java" contentType="text/html; charset=UTF-8"%>
 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>SpringMVC<spring:message code="internationalisation"/></title>
</head>
<body>
Language:
<a href="?lang=zh_CN"><spring:message code="language.cn"/></a>    
<a href="?lang=en_US"><spring:message code="language.en"/></a>
<h1>
    <spring:message code="welcome"/>
    <spring:message code="internationalisation"/>
</h1>
当前语言: ${pageContext.response.locale }
</body>

</html>

language_en_US.properties

  language.cn = \u4e2d\u6587
  language.en = English
  internationalisation = \u0020Internationalisation
  welcome = This is the English environment
  introduce= This is I18N Demo

language_zh_CN.properties

language.cn = \u4e2d\u6587
language.en = English
internationalisation = \u56fd\u9645\u5316  \u8428\u82AC\u65AF\u8482\u82AC
welcome = \u8fd9\u662f\u4e2d\u6587\u73af\u5883
introduce= \u8fd9\u662f\u56fd\u9645\u5316\u7684\u4e8b\u4f8b
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,988评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,802评论 18 399
  • 这夜刚醒 你却已熟睡 一只燃半的兰州 兀自低飞 茶香伴着月色 冲淡了白天的疲惫 烟雾钻进壶嘴 倒出 成了梦中的吗啡...
    夏屿先生阅读 115评论 0 1
  • 今天测量身体,发现骨指数达标了,心里小激动,早上没吃饭,和中午饭汇到一起了,吃饱饭后,爬山去。一路上我的车速70发...
    天堂雨_479f阅读 137评论 2 2