區域解析器的使用,中英文切換,thymeleaf實現國際化
阿新 • • 發佈:2019-01-14
效果圖:中英文切換
定義兩個相同的前臺頁面index1、index2,在其中一個頁面點中文,那麼另一個頁面重新整理一下也會顯示中文,在當前頁面點英文,另一個頁面重新整理一下也會同步成英文。
實現程式碼:
controller:
@Controller @RequestMapping("/test") public class TestInterceptorForwordCon { @Resource private UserService userService; @GetMapping("/test3") public String test3(Model model, HttpSession session, HttpServletRequest request){ ManagerUser user1 = userService.selectUserById(1); model.addAttribute("user1",user1); return "jsps/index2"; } @GetMapping("/test4") public String test4(Model model, HttpSession session, HttpServletRequest request){ ManagerUser user1 = userService.selectUserById(4); model.addAttribute("user1",user1); return "jsps/index1"; } @GetMapping("/test2") public String test2(String lang, HttpServletRequest request, HttpServletResponse response){ //獲取區域解析器 LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); //根據lang引數,設定區域解析器的語言 if(lang.equals("zh")){ localeResolver.setLocale(request,response, Locale.CHINA); }else if(lang.equals("en")){ localeResolver.setLocale(request,response, Locale.US); } String referer = request.getHeader("referer"); return "redirect:"+referer; } }
application.yml:
#熱部署 spring: devtools: restart: enabled: true #配置springMvc檢視解析器 mvc: view: prefix: /jsps/ suffix: .jsp #配置資料來源 datasource: url: jdbc:mysql://localhost:3306/witkey username: root password: root driver-class-name: com.mysql.jdbc.Driver #thymeleaf快取關閉,讓它實時進行更新 thymeleaf: cache: false #國際化語言預設讀取的配置檔案路徑,預設讀取以messages開頭的檔案 messages: basename: messag/messages #mybatis相關配置 mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.example.demo6.pojo configuration: map-underscore-to-camel-case: true #jsp熱部署 server: jsp-servlet: init-parameters: development: true #列印mybatis的sql語句 logging: level: com.example.demo6.mapper: debug
index1.html、index2.html:
springboot專案裡建立的html檔案是html5檔案,需要自己手動在<meta>標籤上加上結束符/,變成<meta/>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <a th:href="@{/test/test2(lang=zh)}">中文</a>|<a th:href="@{/test/test2(lang=en)}">EngLish</a> <hr/> <div th:object="${user1}" style="border: 2px solid red;width: 500px;height: 300px;text-align: center;"> <h3><span th:text="#{userName}"></span><span th:text="*{userName}"></span></h3> <h3><span th:text="*{id}"></span></h3> <h3><span th:text="#{phone}"></span><span th:text="*{phone}"></span></h3> <h3><span th:text="#{password}"></span><span th:text="*{password}"></span></h3> <h3><span th:text="*{dept}"></span></h3> </div> </body> </html>
messages.properties檔案裡可以為空
messages_en_US.properties檔案裡內容如下:(我就隨便定義了三個)
userName=mrsChen:
password=chen123:
phone=17667448304:
messages_zh_CN.properties檔案裡也一樣,不過這後面的中文需要轉換,Unicode中英文轉換,網址:http://tool.chinaz.com/tools/unicode.aspx
userName=\u7528\u6237\u540d:
password=\u5bc6\u7801:
phone=\u7535\u8bdd:
例如:使用者名稱的轉換(符號也可以進行轉換)
定義一個配置類MessagesConfiguration:這裡面可以設定預設語言,如果使用者在瀏覽器上對其他語言進行了選擇,那麼就會讀取controller裡面的設定,對預設語言進行覆蓋;
package com.example.demo6.configuration;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@SpringBootConfiguration
public class MessagesConfiguration {
//區域解析器
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver slr = new SessionLocaleResolver();
//設定預設語言
slr.setDefaultLocale(Locale.US);
return slr;
}
}
這樣,這個語言之間的轉換就可以使用了,這裡只講解了中英文之間的轉換,如果想轉換成其他語言,方法也類似,over,over!