在哔哩哔哩一位up主视频中学到的!
国际化
原理
通过Properties文件配置,配置完后通过Thymeleaf中的#{}
来取代原来的静态资源。例如:
spring:# 关闭模板引擎的缓存thymeleaf:cache: falsemessages:encoding: UTF-8## 配置国际化绑定的文件 不绑定会乱码basename: i18n.login
语言环境解析器接口
package org.springframework.web.servlet;import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;public interface LocaleResolver {//解析语言环境Locale resolveLocale(HttpServletRequest var1);void setLocale(HttpServletRequest var1, @Nullable HttpServletResponse var2, @Nullable Locale var3);
}
在SpringMvc自动配置类中语言环境解析器代码如下
public class WebMvcAutoConfiguration {//**语言环境解析器**public LocaleResolver localeResolver() {if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver(this.mvcProperties.getLocale());} else { // AcceptHeaderLocaleResolver 默认的**语言环境解析器**AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();localeResolver.setDefaultLocale(this.mvcProperties.getLocale());return localeResolver;}}
}
默认使用的语言环境解析器
package org.springframework.web.servlet.i18n;import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;public class AcceptHeaderLocaleResolver implements LocaleResolver {private final List<Locale> supportedLocales = new ArrayList(4);@Nullableprivate Locale defaultLocale;public AcceptHeaderLocaleResolver() {}public void setSupportedLocales(List<Locale> locales) {this.supportedLocales.clear();this.supportedLocales.addAll(locales);}public List<Locale> getSupportedLocales() {return this.supportedLocales;}public void setDefaultLocale(@Nullable Locale defaultLocale) {this.defaultLocale = defaultLocale;}@Nullablepublic Locale getDefaultLocale() {return this.defaultLocale;}//!!!public Locale resolveLocale(HttpServletRequest request) {Locale defaultLocale = this.getDefaultLocale();if (defaultLocale != null && request.getHeader("Accept-Language") == null) {return defaultLocale;} else {Locale requestLocale = request.getLocale();List<Locale> supportedLocales = this.getSupportedLocales();if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) {Locale supportedLocale = this.findSupportedLocale(request, supportedLocales);if (supportedLocale != null) {return supportedLocale;} else {return defaultLocale != null ? defaultLocale : requestLocale;}} else {return requestLocale;}}}@Nullableprivate Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) {Enumeration<Locale> requestLocales = request.getLocales();Locale languageMatch = null;Locale locale;label38:do {while(requestLocales.hasMoreElements()) {locale = (Locale)requestLocales.nextElement();if (supportedLocales.contains(locale)) {continue label38;}if (languageMatch == null) {Iterator var6 = supportedLocales.iterator();while(var6.hasNext()) {Locale candidate = (Locale)var6.next();if (!StringUtils.hasLength(candidate.getCountry()) && candidate.getLanguage().equals(locale.getLanguage())) {languageMatch = candidate;break;}}}}return languageMatch;} while(languageMatch != null && !languageMatch.getLanguage().equals(locale.getLanguage()));return locale;}//!!!public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {throw new UnsupportedOperationException("Cannot change HTTP accept header - use a different locale resolution strategy");}
}
自定义语言环境解析器
我们自定义语言环境解析器,然后在引入到WebMvc配置类中,来替代原有的语言环境解析器。
我们只需要实现LocaleResolver
接口即可
/*** 自定义 语言环境解析器*/
public class MyLocaleResolver implements LocaleResolver {//解析请求@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {//获取请求中的语言参数Locale locale = null;final String language = httpServletRequest.getParameter("Language"); //例如 zh_CN en_USSystem.out.println("------->"+language);if(!StringUtils.isEmpty(language)){ // return target == null || target.length() == 0;final String[] s = language.split("_");// s[0]:国家 s[1]:地区locale = new Locale(s[0],s[1]);return locale;}locale = Locale.getDefault(); // 如果没有请求参数就是默认的return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {}
}
将自定义的语言环境解析器,加入SpringMvc自动配置类中
在自动配置类中需要将自定义的MyLocaleResolver
类取代WebMvcAutoConfiguration
中对应的方法
/*** 定义一个WebMvcConfigurer 的实现类,可以重写SpringMvc的配置* 在这个类中 定义的组件,然后将它交给springboot,springboot就会自动装配*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {//自定义国际化组件 重写了 WebMvcAutoConfiguration 类中的 localeResolver 方法 @Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}}