摘要:本文介绍了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.xml1 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 全注解配置
在配置类中配置消息源:
java1 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.xml1 2 3 4 5
| <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"> <property name="defaultLocale" value="zh_CN"/> </bean>
|
3.2.2 全注解配置
在配置类中配置区域解析器:
java1 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 在控制器中使用
在控制器中使用国际化:
java1 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
通过设置Accept-Language请求头来切换语言:
- 设置
zh-CN切换为中文
- 设置
en-US切换为英文
3.3.2 在JSP中使用
在JSP页面通过taglib标签引入标签库:
jsp1
| <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
在JSP中使用国际化:
jsp1
| <p><fmt:message key="msg"/></p>
|
4 动态切换
4.1 配置区域解析器
使用SessionLocaleResolver基于会话的区域解析器。
4.2 配置语言切换拦截器
4.2.1 半注解配置
在spring-mvc.xml配置文件中配置语言切换拦截器:
spring-mvc.xml1 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 全注解配置
在配置类中配置语言切换拦截器:
java1 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切换为英文
条