專題閱讀:《SpringBoot 佈道系列》javascript
源碼下載:springboot-locale-i18nhtml
— Hey Man,Don't forget to Star or Fork . —前端
項目結構:java
默認解析器:LocaleResolver
用於設置當前會話的默認的國際化語言。jquery
默認攔截器:LocaleChangeInterceptor
指定切換國際化語言的參數名。例如?lang=zh_CN
表示讀取國際化文件messages_zh_CN.properties
。git
/** * 配置國際化語言 */ @Configuration public class LocaleConfig { /** * 默認解析器 其中locale表示默認語言 */ @Bean public LocaleResolver localeResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); localeResolver.setDefaultLocale(Locale.US); return localeResolver; } /** * 默認攔截器 其中lang表示切換語言的參數名 */ @Bean public WebMvcConfigurer localeInterceptor() { return new WebMvcConfigurer() { @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("lang"); registry.addInterceptor(localeInterceptor); } }; } }
首先在配置文件 application.yml
填寫國際化文件的相對路徑,表示讀取classpath:/static/i18n/messages_language_country.properties
。例如:github
spring:
messages:
basename: static/i18n/messages #相對路徑 開頭請勿添加斜槓
而後在 classpath:/static/i18n
目錄中添加以下國際化文件:web
默認文件:messages.properties
spring
#這裏填寫默認翻譯,內容能夠留空,但文件必須存在。
美式英語:messages_en_US.properties
編程
#這裏填寫英語翻譯。 user.title=User Login user.welcome=Welcome user.username=Username user.password=Password user.login=Sign In
中文簡體:messages_zh_CN.properties
#這裏填寫中文翻譯 user.title=用戶登錄 user.welcome=歡迎 user.username=登錄用戶 user.password=登錄密碼 user.login=登錄
中文繁體:messages_zh_TW.properties
#這裏填寫繁體翻譯 user.title=用戶登陸 user.welcome=歡迎 user.username=登陸用戶 user.password=登陸密碼 user.login=登陸
經過工具類的靜態方法MessageUtils.get("user.title")
快速獲取當前國際化的翻譯值。
/** * 國際化工具類 */ @Component public class MessageUtils{ private static MessageSource messageSource; public MessageUtils(MessageSource messageSource) { FastLocale.messageSource = messageSource; } /** * 獲取單個國際化翻譯值 */ public static String get(String msgKey) { try { return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale()); } catch (Exception e) { return msgKey; } }
首先在pom文件引入Thymeleaf
和Web
依賴,而後在頁面中只需經過th:xx="#{x.label}"
便可獲取對應的國際化翻譯值。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
例如:
<title th:text="#{user.title}">用戶登錄</title>
首先在pom文件引入jQuery
、jquery-properties-i18n
等依賴,而後在初始化後便可經過JS函數獲取對應國際化文件的內容。
<dependency><!--webjars版本定位器 用於省略版本號--> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> </dependency> <dependency><!--jQuery前端依賴--> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency> <dependency><!--jQuery國際化插件--> <groupId>org.webjars.bower</groupId> <artifactId>jquery-i18n-properties</artifactId> <version>1.2.7</version> </dependency>
例如:爲了提升可用性 這裏提供了獲取當前國際化語言和獲取國際化翻譯的方法。
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="#{user.title}">用戶登錄</title> <script th:src="@{/webjars/jquery/jquery.min.js}"></script> <script th:src="@{/webjars/jquery-i18n-properties/jquery.i18n.properties.min.js}"></script> <script th:inline="javascript"> //獲取應用路徑 var ROOT = [[${#servletContext.contextPath}]]; //獲取默認語言 var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]]; //初始化i18n插件 $.i18n.properties({ path: ROOT + '/i18n/',//這裏表示訪問路徑 name: 'messages',//文件名開頭 language: LANG_COUNTRY,//文件名語言 例如en_US mode: 'both'//默認值 }); //初始化i18n函數 function i18n(msgKey) { try { return $.i18n.prop(msgKey); } catch (e) { return msgKey; } } //獲取國際化翻譯值 console.log(i18n('user.title')); console.log(i18n('User Login')); </script> </head> <body> <div class="logo_box"> <select id="locale"> <option value="zh_CN">中文簡體</option> <option value="zh_TW">中文繁體</option> <option value="en_US">English</option> </select> <h3 th:text="#{user.welcome}">歡迎登錄</h3> <form> <div class="input_outer"> <span class="u_user"></span> <input id="username" name="username" class="text" type="text" th:placeholder="#{user.username}"> </div> <div class="input_outer"> <span class="us_uer"></span> <input id="password" name="password" class="text" type="password" th:placeholder="#{user.password}"> </div> <div class="mb2"> <a class="act-but submit" th:text="#{user.login}">登陸</a> </div> </form> </div> <script th:inline="javascript"> //選中語言 $("#locale").find('option[value="' + LANG_COUNTRY + '"]').attr('selected', true); //切換語言 $("#locale").change(function () { $.get(ROOT + '/?lang=' + $("#locale").val(), function () { location.reload(); }); }); </script> </body> </html>
關於i18n插件的更多配置項請查閱 jquery-properties-i18n 官方文檔。
不少新人配置好以後不懂得如何切換國際化語言,其實很簡單,因爲在前面已經配置了攔截器LocaleChangeInterceptor
,此時咱們只需在任意請求中附上語言參數lang
便可,固然也經過AJAX來快速切換。
例如:
默認英語:http://http://127.0.0.1:8080?lang=en_US
中文簡體:http://http://127.0.0.1:8080?lang=zh_CN
中文繁體:http://http://127.0.0.1:8080?lang=zh_TW
英文界面
中文界面
好用的框架隨處可見,高效的整合萬里挑一,關注做者,關注SpringBoot,讓Java編程更簡單!!