SpringBoot系統之i18n國際化語言集成教程
@html
本博客介紹一下SpringBoot集成i18n,實現系統語言國際化處理,ok,先建立一個SpringBoot項目,具體的參考個人博客專欄:SpringBoot系列博客專欄連接java
環境準備:git
項目集成:github
ok,要實現國際化語言,先要建立resource bundle文件:
在resources文件夾下面建立一個i18n的文件夾,其中:web
messages.loginBtnName=登陸~ messages.password=密碼~ messages.rememberMe=記住我~ messages.tip=請登陸~ messages.username=用戶名~
messages_zh_CN.properties:spring
messages.loginBtnName=登陸 messages.password=密碼 messages.rememberMe=記住我 messages.tip=請登陸 messages.username=用戶名
messages_en_US.properties:bootstrap
messages.loginBtnName=login messages.password=password messages.rememberMe=Remember me messages.tip=Please login in messages.username=userName
在項目的application.properties修改默認配置,讓SpringBoot的自動配置能讀取到resource bundle資源文件瀏覽器
## 配置i18n # 默認是i18n(中文/中國) spring.mvc.locale=zh_CN # 配置resource bundle資源文件的前綴名eg:i18n是文件夾名,messages是資源文件名,支持的符號有.號或者/ spring.messages.basename=i18n.messages # 設置緩存時間,2.2.1是s爲單位,以前版本纔是毫秒 spring.messages.cache-duration=1 # 設置資源文件編碼格式爲utf8 spring.messages.encoding=utf-8
注意要點:緩存
protected static class ResourceBundleCondition extends SpringBootCondition { //定義一個map緩存池 private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>(); @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages"); ConditionOutcome outcome = cache.get(basename);//緩存拿獲得,直接從緩存池讀取 if (outcome == null) {//緩存拿不到,從新讀取 outcome = getMatchOutcomeForBasename(context, basename); cache.put(basename, outcome); } return outcome; } private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) { ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle"); for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) { for (Resource resource : getResources(context.getClassLoader(), name)) { if (resource.exists()) { //匹配resource bundle資源 return ConditionOutcome.match(message.found("bundle").items(resource)); } } } return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll()); } //解析資源文件 private Resource[] getResources(ClassLoader classLoader, String name) { String target = name.replace('.', '/');//spring.messages.basename參數值的點號換成斜杆 try { return new PathMatchingResourcePatternResolver(classLoader) .getResources("classpath*:" + target + ".properties"); } catch (Exception ex) { return NO_RESOURCES; } } }
SpringBoot默認採用AcceptHeaderLocaleResolver類做爲默認LocaleResolver,LocaleResolver類的做用就是做爲i18n的分析器,獲取對應的i18n配置,固然也能夠自定義LocaleResolver類
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale; /** * <pre> * 自定義LocaleResolver類 * </pre> * @author nicky * <pre> * 修改記錄 * 修改後版本: 修改人: 修改日期: 2019年11月23日 修改內容: * </pre> */ public class CustomLocalResolver implements LocaleResolver { Logger LOG = LoggerFactory.getLogger(this.getClass()); @Nullable private Locale defaultLocale; public void setDefaultLocale(@Nullable Locale defaultLocale) { this.defaultLocale = defaultLocale; } @Nullable public Locale getDefaultLocale() { return this.defaultLocale; } @Override public Locale resolveLocale(HttpServletRequest request) { Locale defaultLocale = this.getDefaultLocale();//獲取application.properties默認的配置 if(defaultLocale != null && request.getHeader("Accept-Language") == null) { return defaultLocale;//http請求頭沒獲取到Accept-Language才採用默認配置 } else {//request.getHeader("Accept-Language")獲取獲得的狀況 Locale requestLocale = request.getLocale();//獲取request.getHeader("Accept-Language")的值 String localeFlag = request.getParameter("locale");//從URL獲取的locale值 //LOG.info("localeFlag:{}",localeFlag); //url連接有傳locale參數的狀況,eg:zh_CN if (!StringUtils.isEmpty(localeFlag)) { String[] split = localeFlag.split("_"); requestLocale = new Locale(split[0], split[1]); } //沒傳的狀況,默認返回request.getHeader("Accept-Language")的值 return requestLocale; } } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }
I18n仍是要繼承WebMvcConfigurer,注意,2.2.1版本纔是實現接口就能夠,以前1.+版本是要實現WebMvcConfigurerAdapter適配器類的
import com.example.springboot.i18n.component.CustomLocalResolver; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; /** * <pre> * I18nConfig配置類 * </pre> * <p> * <pre> * @author nicky.ma * 修改記錄 * 修改後版本: 修改人: 修改日期: 2019/11/24 11:15 修改內容: * </pre> */ //Configuration必須加上,否則不能加載到Spring容器 @Configuration //使WebMvcProperties配置類可用,這個能夠不加上,本博客例子才用 @EnableConfigurationProperties({ WebMvcProperties.class}) public class I18nConfig implements WebMvcConfigurer{ //裝載WebMvcProperties 屬性 @Autowired WebMvcProperties webMvcProperties; /** * 定義SessionLocaleResolver * @Author nicky.ma * @Date 2019/11/24 13:52 * @return org.springframework.web.servlet.LocaleResolver */ // @Bean // public LocaleResolver localeResolver() { // SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); // // set default locale // sessionLocaleResolver.setDefaultLocale(Locale.US); // return sessionLocaleResolver; // } /** * 定義CookieLocaleResolver * @Author nicky.ma * @Date 2019/11/24 13:51 * @return org.springframework.web.servlet.LocaleResolver */ // @Bean // public LocaleResolver localeResolver() { // CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); // cookieLocaleResolver.setCookieName("Language"); // cookieLocaleResolver.setCookieMaxAge(1000); // return cookieLocaleResolver; // } /** * 自定義LocalResolver * @Author nicky.ma * @Date 2019/11/24 13:45 * @return org.springframework.web.servlet.LocaleResolver */ @Bean public LocaleResolver localeResolver(){ CustomLocalResolver localResolver = new CustomLocalResolver(); localResolver.setDefaultLocale(webMvcProperties.getLocale()); return localResolver; } /** * 定義localeChangeInterceptor * @Author nicky.ma * @Date 2019/11/24 13:45 * @return org.springframework.web.servlet.i18n.LocaleChangeInterceptor */ @Bean public LocaleChangeInterceptor localeChangeInterceptor(){ LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); //默認的請求參數爲locale,eg: login?locale=zh_CN localeChangeInterceptor.setParamName(LocaleChangeInterceptor.DEFAULT_PARAM_NAME); return localeChangeInterceptor; } /** * 註冊攔截器 * @Author nicky.ma * @Date 2019/11/24 13:47 * @Param [registry] * @return void */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()).addPathPatterns("/**"); } }
注意要點:
@Bean public LocaleResolver localeResolver(){ CustomLocalResolver localResolver = new CustomLocalResolver(); localResolver.setDefaultLocale(webMvcProperties.getLocale()); return localResolver; }
原理:
跟一下源碼,點進LocaleChangeInterceptor類
DispatcherServlet是Spring一個很重要的分發器類,在DispatcherServlet的一個init方法裏找到這個LocaleResolver的init方法
這個IOC獲取的bean類名固定爲localeResolver,寫例子的時候,我就由於改了bean類名,致使一直報錯,跟了源碼才知道Bean類名要固定爲localeResolver
拋異常的時候,也是會獲取默認的LocaleResolver的
找到資源文件,確認,仍是默認爲AcceptHeaderLocaleResolver
配置了locale屬性的時候,仍是選用AcceptHeaderLocaleResolver做爲默認的LocaleResolver
spring.mvc.locale=zh_CN
WebMvcAutoConfiguration.localeResolver方法源碼,ConditionalOnMissingBean主鍵的意思是LocaleResolver沒有自定義的時候,才做用,ConditionalOnProperty的意思,有配了屬性才走這裏的邏輯
localeChangeInterceptor.setParamName("lang");
本博客的模板引擎採用Thymeleaf的,因此新增項目時候就要加上maven相關依賴,沒有的話,本身加上:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
ok,而後去找個bootstrap的登陸頁面,本博客已尚硅谷老師的例子爲例,進行拓展,引入靜態資源文件:
Thymeleaf的i18n支持是採用#符號的
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>SpringBoot i18n example</title> <!-- Bootstrap core CSS --> <link href="asserts/css/bootstrap.min.css" th:href="@{asserts/css/bootstrap.min.css}" rel="stylesheet"> <!-- Custom styles for this template --> <link href="asserts/css/signin.css" th:href="@{asserts/css/signin.css}" rel="stylesheet"> </head> <body class="text-center"> <form class="form-signin" action="dashboard.html"> <img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal" th:text="#{messages.tip}">Please sign in</h1> <label class="sr-only" th:text="#{messages.username}">Username</label> <input type="text" class="form-control" th:placeholder="#{messages.username}" required="" autofocus=""> <label class="sr-only" th:text="#{messages.password} ">Password</label> <input type="password" class="form-control" th:placeholder="#{messages.password}" required=""> <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember-me" > [[#{messages.rememberMe}]] </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{messages.loginBtnName}">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2019</p> <a class="btn btn-sm" th:href="@{/login(locale='zh_CN')} ">中文</a> <a class="btn btn-sm" th:href="@{/login(locale='en_US')} ">English</a> </form> </body> </html>
切換中文網頁:
切換英文網頁:
固然不點連接傳locale的方式也是能夠自動切換的,瀏覽器設置語言:
原理localeResolver類會獲取Accept language參數
附錄:
logging manual:SpringBoot官方手冊
example source:例子代碼下載連接