1、国际化(internationalization), 简称i18n,是一种让软件在开发阶段就支持多种语言的技术
java.util.Locale
语言代码_国家代码
注:国家代码可省略
zh_CN
2、在resources加入两个文件(中英双语):
i18n_zh_CN.properties 和 i18n_en_US.properties
加入后会自动生成存放i18n文件的文件夹

讯享网
i18n_zh_CN.properties:存放中文:
yhzh.homePage=欢迎来到首页 yhzh.label=用户名 yhmm.label=用户密码
讯享网
i18n_en_US.properties:存放英文:
讯享网yhzh.homePage=welcome to homePage yhzh.label=userName yhmm.label=password
注意:都是以键值对存在
3、修改pom.xml
<!--注1:需修改pom.xml将国际化资源文件输出到target文件夹--> <resource> <directory>src/main/resources</directory> <includes> <!--<include>jdbc.properties</include>--> <include>*.properties</include> <include>*.xml</include> </includes> </resource>
4、配置文件:
4-1:通过ResourceBundleMessageSource加载资源文件(basenames属性)
讯享网 <!--1) 配置国际化资源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>i18n</value> </list> </property> </bean>
4-2: 指定springmvc的语言区域解析器,由它来确定使用哪个语言
4-2-1:配置语言区域解析器
<!--2) 指定语言区域解析器,由它来确定使用哪个语言 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
注1:必须叫localeResolver、必须叫localeResolver、必须叫localeResolver
4-2-2:语言解析器的类型
AcceptHeaderLocaleResolver/SessionLocleResolver/CookieLocaleResolver
①:AcceptHeaderLocaleResolver(基于操作系统):
Spring采用的默认区域解析器是AcceptHeaderLocaleResolver。它通过检验HTTP请求的accept-language头部来解析区域。
这个头部是由用户的web浏览器根据底层操作系统的区域设置进行设定。请注意,这个区域解析器无法改变用户的区域,
因为它无法修改用户操作系统的区域设置
②: SessionLocaleResolver(基于会话):
它通过检验用户会话中预置的属性来解析区域。如果该会话属性不存在,它会根据accept-language HTTP头部确定默认区域
③:CookieLocaleResolver(基于Cookie):
这个区域解析器所采用的Cookie可以通过cookieName和cookieMaxAge属性进行定制。

④ defaultLocale:默认的语言区域
⑤ cookieName:设置cookieName名称
⑥ cookieMaxAge:设置cookieName有效时间,单位秒
⑦ cookiePath:设置cookie可见的地址,默认是“/”即对网站所有地址都是可见的,如果设为其它地址,则只有该地址或其后的地址才可见
4-2-2: 配置国际化操作拦截器,如果采用基于(Session/Cookie)则必需配置:
讯享网 <!--3) 配置国际化操作拦截器--> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors>
4-2-3:通过标签输出内容,而非直接输出内容
springmvc的message标签输出:
页面头部导入即可
<%@ taglib prefix="t" uri="http://www.springframework.org/tags" %>
5、 后台代码获取国际化信息
讯享网 RequestContext requestContext = new RequestContext(request); String errorMsg = requestContext.getMessage("login.error.label"); System.out.println("errorMsg:" + errorMsg);
6、示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/i18n">English</a>
<a href="${pageContext.request.contextPath}/i18n?state=中文">中文</a>
欢迎来到首页
用户名:<input type="text">
密码:<input type="password">
使用国际化语言后
讯享网<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="t" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/i18n">English</a>
<a href="${pageContext.request.contextPath}/i18n?state=中文">中文</a>
<t:message code="yhzh.homePage"></t:message>
<t:message code="yhzh.label" ></t:message> :<input type="text">
<t:message code="yhmm.label" ></t:message> :<input type="password">
6-2:后台处理:
/ * 国际化 * @param state * @param session * @return */ @RequestMapping("/i18n") public String i18n(String state, HttpSession session){ if("中文".equals(state)){ //转换成中文 session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.CHINA); }else{ //转换成英文 session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.US); } return "i18n"; }
结果:默认英文

当转换成英文后:

7、注意:
注1:为什么在index.jsp使用<t:message code=“user_name”/>会报错
原因是在web.xml中配置的DispatcherServlet的url-pattern为“/”,不会匹配访问.jsp的url,
所以直接访问首页并不会经过DispatcherServlet,导致无法读取到资源文件
解决方案:首页转发到/WEB-INF/jsp/login.jsp即可
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/65460.html