SpringBoot国际化
国际化的基本原理
在spring程序中,国际化主要是通过ResourceBundleMessageSource这个类来实现的
springBoot通过MessageSourceAutoConfiguration为我们自动配置好了管理国际化资源文件的组件
@Bean
public MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils
.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
if (properties.getEncoding() != null) {
messageSource.setDefaultEncoding(properties.getEncoding().name());
}
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
Duration cacheDuration = properties.getCacheDuration();
if (cacheDuration != null) {
messageSource.setCacheMillis(cacheDuration.toMillis());
}
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
}
点击查看MessageSourceProperties
MessageSourceProperties properties
发现basename默认为message
public class MessageSourceProperties {
private String basename = "messages";
我们需要直接配置语言格式
在resources文件夹中新建一个i18n文件夹
login.properties
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
login_en_US.properties
login.btn=sign in
login.password=password
login.remember=RememberMe
login.tip=Please sign in
login.username=username
login_zh_CN.properties
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
在application.properties配置文件中配置basename
spring.messages.basename=i18n.login
上面说了国际化支持主要是通过ResourceBundleMessageSource实现的,这里对它做出实例化操作
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils
.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
我们到现在配置好了我们自己编写的不同语言配置文件,接下来我们要做的是怎么切换成我们需要的语言版本
国际化语言的切换主要是因为有一个区域信息解析器在其作用(会根据locale返回的国家和语言就像:zh_CN,来判定执行哪个配置文件)
@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME)
public LocaleResolver localeResolver() {
if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.webProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.webProperties.getLocale());
return localeResolver;
}
如果我们配置了LocaleResolver,那默认的LocalResolver就会失效,否则就使用spring默认的
我们编写一个localeResolver类去重写LocaleResolver的方法(注意类的名字一定要为localeResolver,源码中有定义)
那就等于我们自己有自己的LocaleResolver,spring只会执行我们的方法
在配置类中编写自己的LocaleResolver
package com.Google.config;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.Location;
import java.util.Locale;
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("l");
Locale locale = Locale.getDefault();//如果没有获取到就使用默认的
//如果请求不为空
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");
//语言,国家
locale=new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
配置到Spring中,让Spring托管
package com.Google.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override//默认页面为首页
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
@Bean//注册国际化组件
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}