javascript
Spring Boot国际化支持
文章目錄
- 添加Maven支持
- LocaleResolver
- LocaleChangeInterceptor
- 定義Message Sources
- Controller文件
- html文件
- 運行應用程序
Spring Boot國際化支持
國際化支持應該是所有的做國際化網站都需要考慮的一個問題,Spring Boot為國際化提供了強有力的支持,本文將會通過一個例子來講解Spring Boot的國際化。
添加Maven支持
Spring Boot本身就支持國際化,我們這里添加一個模板支持來通過頁面來展示,我們這里添加thymeleaf模板:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>LocaleResolver
我們需要為系統指定一個默認的LocaleResolver:
@Bean public LocaleResolver localeResolver() {SessionLocaleResolver slr = new SessionLocaleResolver();slr.setDefaultLocale(Locale.US);return slr; }上面的例子中我們自定義了一個SessionLocaleResolver,并且指定了默認的Locale。
LocaleChangeInterceptor
接下來,我們定義一個LocaleChangeInterceptor來接收Locale的變動。這里我們通過lang參數來接收。
@Bean public LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor lci = new LocaleChangeInterceptor();lci.setParamName("lang");return lci; }當然,我們需要將這個Interceptor注冊到SpringMVC中:
@Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor()); }定義Message Sources
默認情況下,Spring Boot會在src/main/resources查找message文件,默認的message文件是messages.properties,如果指定了某種語言,那么就是messages_XX.properties,其中XX是Local code。
messages.properties是key value的格式,如果在對應的local文件中沒找到相應的key,則會在默認的messages.properties中查找。
我們默認定義英語的messages.properties如下:
greeting=Hello! Welcome to our website! lang.change=Change the language lang.eng=English lang.fr=French同時我們定義一個法語的message文件messages_fr.properties :
greeting=Bonjour! Bienvenue sur notre site! lang.change=Changez la langue lang.eng=Anglais lang.fr=FrancaisController文件
我們定義一個跳轉的controller文件:
@Controller public class PageController {@GetMapping("/international")public String getInternationalPage() {return "international";} }html文件
相應的html文件如下:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head><meta charset="ISO-8859-1" /><title>Home</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script>$(document).ready(function() {$("#locales").change(function () {var selectedOption = $('#locales').val();if (selectedOption != ''){window.location.replace('international?lang=' + selectedOption);}});});</script> </head> <body> <h1 th:text="#{greeting}"></h1><br /><br /> <span th:text="#{lang.change}"></span>: <select id="locales"><option value=""></option><option value="en" th:text="#{lang.eng}"></option><option value="fr" th:text="#{lang.fr}"></option> </select> </body> </html>運行應用程序
好了,接下來我們可以運行了。
如果我們訪問http://localhost:8080/international?lang=en , 則會讀取默認的英語資源:
通過切換到法語環境:http://localhost:8080/international?lang=fr, 我們可以看到:
環境已經切換過來了。
本文的例子可以參考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-Internationalization
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多教程請參考 flydean的博客
總結
以上是生活随笔為你收集整理的Spring Boot国际化支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot中使用@JsonC
- 下一篇: Spring Boot devtool的