一、页面国际化
1、在pom.xml引入国际化需要的依赖
<!--国际化相关依赖 begin -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--国际化相关依赖 end -->
2、在success.jsp里面添加jstl的fmt
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
3、在springmvc.xml里面国际化资源文件
<!-- 配置国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
4、src/main/java新建国际化资源文件
i18n.properties
i18n.username=Username
i18n.password=Password
i18n_zh_CN.properties
i18n.username=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801
i18n_en_US.properties
i18n.username=Username
i18n.password=Password
5、在success.jsp页面通过jstl的标签获取值
<!--国际化 begin -->
<p><b>国际化 begin</b></p>
<fmt:message key="i18n.username"></fmt:message>
<br>
<fmt:message key="i18n.password"></fmt:message>
<p><b>国际化 end</b></p>
<!--国际化 end -->
二、java代码国际化(依赖于i18n_zh_CN.properties和i18n_en_US.properties)
1、注入ResourceBundleMessageSource
@Autowired
private ResourceBundleMessageSource messageSource;
2、 通过getMessage方法获取国际化资源
@RequestMapping("/i18n")
public String testI18n(Locale locale){
String val = messageSource.getMessage("i18n.user", null, locale);
System.out.println(val);
return "i18n";
}
三、点击超链接切换语言进行页面国际化(全局国际化)
(依赖于一的fmt标签、i18n_zh_CN.properties和i18n_en_US.properties)
原理如下:
1、LocaleChanceInterceptor拦截器获取local对象
2、 SessionLocalResolver解析器把local对象放到session里面
3、应用程序从session里面获取local对象进行国际化
1、在jsp页面设置语言切换链接
<a href="i18n?locale=zh_CH">中文</a>
<br><br>
<a href="i18n?locale=en_US">英文</a>
2、在springmvc.xml文件里面配置SessionLocaleResolver和LocaleChangeInterceptor
<!-- 配置 SessionLocalResolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<mvc:interceptors>
<!-- 配置 LocaleChanceInterceptor -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
https://www.cnblogs.com/leeSmall/category/1093236.html