抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:本文介绍了Spring MVC中的国际化功能。

环境

Windows 10 企业版 LTSC 21H2
Java 1.8
Tomcat 8.5.50
Maven 3.6.3
Spring 5.3.31

1 概述

国际化(Internationalization,简称i18n)是指应用程序能够适应不同语言和地区的需求,根据用户的语言环境显示相应的文本和格式。Spring MVC提供了完善的国际化支持,使应用程序能够轻松实现多语言支持。

2 资源文件

src/main/resources目录下创建国际化资源文件:

  • messages.properties:默认语言资源文件
  • messages_zh_CN.properties:中文资源文件
  • messages_en_US.properties:英文资源文件

3 简单使用

3.1 配置消息源

3.1.1 半注解配置

spring-mvc.xml配置文件中配置消息源:

spring-mvc.xml
1
2
3
4
5
6
7
<!-- 配置消息源 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 设置资源文件名 -->
<property name="basename" value="messages"/>
<!-- 设置默认编码 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>

3.1.2 全注解配置

在配置类中配置消息源:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class WebConfig implements WebMvcConfigurer {
// 配置消息源
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// 设置资源文件名
messageSource.setBasename("messages");
// 设置默认编码
messageSource.setDefaultEncoding("UTF-8");
// 返回消息源
return messageSource;
}
// ...
}

3.2 配置区域解析器

Spring MVC提供了多种区域解析器:

  • AcceptHeaderLocaleResolver:基于请求头的区域解析器(默认)
  • SessionLocaleResolver:基于会话的区域解析器
  • CookieLocaleResolver:基于Cookie的区域解析器

3.2.1 半注解配置

spring-mvc.xml配置文件中配置区域解析器:

spring-mvc.xml
1
2
3
4
5
<!-- 配置区域解析器 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
<!-- 设置默认区域 -->
<property name="defaultLocale" value="zh_CN"/>
</bean>

3.2.2 全注解配置

在配置类中配置区域解析器:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class WebConfig implements WebMvcConfigurer {
// 配置区域解析器
@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
// 设置默认区域
localeResolver.setDefaultLocale(Locale.CHINA);
// 返回区域解析器
return localeResolver;
}
// ...
}

3.3 使用国际化

3.3.1 在控制器中使用

在控制器中使用国际化:

java
1
2
3
4
5
6
7
8
9
10
11
@Controller
public class DemoController {
@Resource
private MessageSource messageSource;
@GetMapping("/demo")
public String demo(Model model, Locale locale) {
String msg = messageSource.getMessage("msg", null, locale);
model.addAttribute("msg", msg);
return "success";
}
}

在JSP中显示国际化消息:

jsp
1
<p>${msg}</p>

通过设置Accept-Language请求头来切换语言:

  • 设置zh-CN切换为中文
  • 设置en-US切换为英文

3.3.2 在JSP中使用

在JSP页面通过taglib标签引入标签库:

jsp
1
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

在JSP中使用国际化:

jsp
1
<p><fmt:message key="msg"/></p>

4 动态切换

4.1 配置区域解析器

使用SessionLocaleResolver基于会话的区域解析器。

4.2 配置语言切换拦截器

4.2.1 半注解配置

spring-mvc.xml配置文件中配置语言切换拦截器:

spring-mvc.xml
1
2
3
4
5
6
7
<!-- 配置语言切换拦截器 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 设置语言切换参数名 -->
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>

4.2.2 全注解配置

在配置类中配置语言切换拦截器:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class WebConfig implements WebMvcConfigurer {
// 配置语言切换拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
// 设置语言切换参数名
interceptor.setParamName("lang");
// 设置拦截器
registry.addInterceptor(interceptor);
}
// ...
}

4.3 通过URL参数切换

通过设置URL参数来切换语言:

  • 设置?lang=zh-CN切换为中文
  • 设置?lang=en-US切换为英文

评论