SpingBoot+Thymeleaf 實現頁面國際化語言切換
阿新 • • 發佈:2019-01-27
直接進入主題了哈
1)、編寫國際化配置檔案
a、在IDEA的resources資料夾下建立il8n,在il8n下建立login資料夾;
b、在login檔案下建立login.properties、login_zh_CN.properties、login_en_US.properties;
login.properties:代表預設顯示的語種,login_zh_CN.properties:切換為頁面顯示中文,login_en_US.properties:切換為頁面顯示英文。
如果要顯示更多的語言的,則需要自己定義更多的properties檔案
c、建立檔案後選擇 login_zh_CN.properties、login_en_US.properties任意一個檔案,點選下方的Resource Bundle
2)、修改配置檔案-引入
在application.properties中新增
spring.messages.basename=i18n.login
3)、去頁面獲取國際化的值;
#{}中的內容表示國際化處理
<!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>Signin Template for Bootstrap</title> <!-- Bootstrap core CSS --> <link href="asserts/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="asserts/css/signin.css" rel="stylesheet"> </head> <body class="text-center"> <form class="form-signin" action="dashboard.html"> <img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1> <label class="sr-only">Username</label> <input type="text" class="form-control" th:placeholder="#{login.userName}" required="" autofocus=""> <label class="sr-only">Password</label> <input type="password" class="form-control" th:placeholder="#{login.password}" required=""> <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember"> [[#{login.remember}]] </label> </div> <button class="btn btn-lg btn-primary btn-block" th:text="#{login.btn}" type="submit">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a> </form> </body> </html>
5)、重寫 resolveLocale方法-自定義元件
package lhz.lx.demo.component; import org.springframework.boot.autoconfigure.web.WebMvcProperties; 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; public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { String l = request.getParameter("l"); Locale locale = Locale.getDefault(); if(!StringUtils.isEmpty(l)){ String[] split = l.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }
6)、新增自定義元件
package lhz.lx.demo.config;
import lhz.lx.demo.component.MyLocaleResolver;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}