什么是国际化
国际化(Internationalization)是指为了使应用程序适应多种语言和文化而设计的一种技术,也称为“本地化”(Localization),因i和n中间有18个字母,也简称为i18n。它是一种将应用程序的语言和文化参数与其源代码分离的过程,从而简化了多语言支持的实现。具体实现时,不同语言的资源文件被保存在不同的文件中,并根据用户的语言环境动态加载相应的文件。这样,就可以实现将一个应用程序适应不同国家和地区的需要。
改造步骤
Spring Boot提供了一种方便的方式来实现应用程序的国际化支持。在Spring Boot中,我们可以使用ResourceBundleMessageSource来加载资源文件,并通过注解在应用程序中对不同语言进行翻译。下面是实现国际化的详细步骤。
环境改造
1. 项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. 配置文件
配置文件中,指定国际化的参数,thmeleaf的配置信息
application.yml
spring:
messages:
basename: i18n/messages/messages
encoding: UTF-8
fallbackToSystemLocale: false
3. 国际化信息文件
上面的配置 spring.messages.basename 指定国际化配置文件的目录与前缀,取值为i18n/messages/messages
所以在资源目录下,新建文件 i18n/messages,国际化文件名为 messages-xxx.properties,项目结果如

说明
注意spring.messages.basename 这个配置的取值为国际化文件的目录 + 文件名前缀,比如上面若少了最后一层的messages,会提示取不到配置
其次在IDEA中,选中国家化文件之后,点击下方的Resource Bundle,可以进入如上图中更友好的编辑框,支持一次修改多个语言的信息
国际化支持
前面是国际化的基本配置,那么如何根据前面配置中的key,获取不同语言的value呢?
1. MessageSource
在SpringBoot中主要借助MessageSource来获取不同语言的value信息
如一个最基本的封装
@Slf4j
public class MsgUtil {
private static MessageSource messageSource;
public static void init(MessageSource messageSource) {
MsgUtil.messageSource = messageSource;
}
/**
* 获取单个国际化翻译值
*/
public static String get(String msgKey) {
try {
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
log.error("获取国际化翻译值【" + msgKey + "】异常:" + e.getMessage(), e);
return msgKey;
}
}
}
2. SpringBootApplication
在启动类处,对MsgUtil的MessageSource进行初始化
@Controller
@SpringBootApplication
public class Application {
public Application(MessageSource messageSource) {
MsgUtil.init(messageSource);
}
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
3. Cookies方式缓存国际化信息
Spring 默认设置当前语言是通过请求头的Accept-Language来配置当前的环境 ,然后语言与配置文件去做匹配 比如传入的语言是en,就会使用messages_en.properties
我们可以考虑借助拦截器来实现统一的Local设置,通过自定义参数传入语言环境(通过param的language值进行设置)
@Configuration
public class AutoConfig implements WebMvcConfigurer {
/**
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
// 也可以换成 SessionLocalResolver, 区别在于国际化的应用范围
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return localeResolver;
}
/**
* 根据请求参数,来设置本地化
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry) {
interceptorRegistry.addInterceptor(localeChangeInterceptor());
}
}
上面的实例中,采用的是CookieLocaleResolver,因此会在cookie中缓存语言信息,一次修改,后续都会生效
4.参数校验国际化
有时,我们会用到validation校验框架,对参数进行校验,针对这部分也需要进行国际化处理。
-
添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
-
校验国际化配置(注意该
Validator非Spring的)
import javax.validation.Validator;
@Bean
public Validator getValidator(MessageSource messageSource) {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource);
return bean;
}
-
全局异常处理
@RestControllerAdvice
@Configuration(proxyBeanMethods = false)
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BindException.class)
public String exceptionHandle(Exception e) {
//可根据情况对错误信息进行处理
}
}
-
校验类
@Data
public class User {
@NotEmpty(message = "{email.notempty}")
private String email;
}
email.notempty为对应国际化信息文件中的key