一、什么是国际化:
国际化是设计软件应用的过程中应用被使用与不同语言和地区
国际化通常采用多属性文件的方式解决,每个属性文件保存一种语言的文字信息,
不同语言的用户看到的是不同的内容
二、springmvc.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ssm.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 存储区域设置信息 SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中 从session判断用户语言defaultLocale
:默认语言 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh_CN" />
</bean>
<!-- 国际化资源文件 messageSource配置的是国际化资源文件的路径, classpath:messages指的是classpath路径下的
messages_zh_CN.properties文件和messages_en_US.properties文件 设置“useCodeAsDefaultMessage”,默认为false,这样当Spring在ResourceBundle中找不到messageKey的话,就抛出NoSuchMessageException,
把它设置为True,则找不到不会抛出异常,而是使用messageKey作为返回值。 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="true" />
<property name="basenames">
<list>
<value>classpath:resource/message</value>
</list>
</property>
</bean>
<!--该拦截器通过名为”lang”的参数来拦截HTTP请求,使其重新设置页面的区域化信息 -->
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
</beans>
三、国际化语言属性文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rLqwmFkn-1573021959052)(http://note.youdao.com/yws/res/27768/9DA4E29193DB432BAD6D741492F70DC6)]
我的路径是这样,其他的都可以
- message_en_US.properties
TITLE = BEGIN TO START
USERNAME = UserName
PASSWORD = PassWord
LOGIN = Login
- message_zh_CN.properties
TITLE = 开始冒险之旅
USERNAME = 账号:
PASSWORD = 密码:
LOGIN = 登录
四、编写Controller
@Controller
public class NewsController {
@RequestMapping(value = "/getLogin.html")
public String getLogin(){
return "result";
}
}
五、前端编写并测试
- 文件位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u0AEv4jY-1573021959052)(http://note.youdao.com/yws/res/27779/1D3A48C8D33744218BB1A63B50B8491C)]
2. index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="getLogin.html?lang=zh_CN">中文</a>
<br />
<a href="getLogin.html?lang=en_US">英文</a>
</body>
</html>
- result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="login">
<h1><spring:message code="TITLE" /> </h1>
<form action="test.do" method="post">
<input type="text" name="name" placeholder=<spring:message code="USERNAME" /> required="required" value="" />
<input type="password" name="password" placeholder=<spring:message code="PASSWORD" /> required="required" value="" />
<button type="submit" class="btn btn-primary btn-block btn-large"><spring:message code="LOGIN" /> </button>
</form>
</div>
</body>
</html>
- 测试