一、application.properties中指明國際化文件所在路徑和文件前綴css
二、登陸頁面用#{}的形式從國際化配置文件中取值html
三、編寫國際化文件java
----------------------------------若想實現中英文切換,繼續下面部分----------------web
四、點擊中英文切換按鈕時指定訪問路徑並傳遞參數spring
五、定義本身的LocaleResolver,用於根據傳遞的參數返回LocaleResolverbootstrap
六、在@Configuration標註的配置類中以@Bean的形式將本身定義的LocaleResolver以組件的形式加入到容器中app
具體實現代碼以下:ide
一、application.properties中指明國際化文件所在路徑和文件前綴svg
spring.messages.basename=i18n.login
二、登陸頁面用#{}的形式從國際化配置文件中取值測試
login.html中的代碼以下
<!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" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.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" 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" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<!--要寫成th:href="@{/index(l='zh_CN')}",而不能寫成th:href="@{/login.html(l='zh_CN')}",
由於直接定位到靜態資源的話是不會走本身定義的區域解析器的,
另外thymeleaf模板引擎是用中括號中放key=value的形式傳參數-->
<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>
</form>
</body>
</html>
三、編寫國際化文件
i18n/login.properties和i18n/login_zh_CN.properties配置的內容相同,內容以下:
login.username=用戶名
login.password=密碼
login.remember=記住我
login.btn=登陸
login.tip=提示
i18n/login_en_US.properties文件的內容以下:
login.username=username
login.password=password
login.remember=remember me
login.btn=sign in
login.tip=tip
----------------------------------若想實現中英文切換,繼續下面部分----------------
四、點擊中英文切換按鈕時指定訪問路徑並傳遞參數
login.html中涉及到中英文切換的代碼以下:
!--要寫成th:href="@{/index(l='zh_CN')}",而不能寫成th:href="@{/login.html(l='zh_CN')}",
由於直接定位到靜態資源的話是不會走本身定義的區域解析器的,
另外thymeleaf模板引擎是用中括號中放key=value的形式傳參數-->
<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>
五、定義本身的LocaleResolver,用於根據傳遞的參數返回LocaleResolver
package com.myself.component;
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) {
Locale locale = Locale.getDefault();
String l = request.getParameter("l");
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) {
}
}
六、在@Configuration標註的配置類中以@Bean的形式將本身定義的LocaleResolver以組件的形式加入到容器中
package com.myself.config;
import com.myself.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.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("login");
}
//使用WebMvcConfigurerAdapter來擴展SpringMVC的功能
//全部的WebMvcConfigurerAdapter都會生效
//注意要寫在標有@Configuration的類中,要在方法上標上@Bean註解
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/helloIndex").setViewName("login");
registry.addViewController("/index").setViewName("login");
registry.addViewController("/").setViewName("login");
}
};
return webMvcConfigurerAdapter;
}
//定義區域解析器,解析國際化中英文切換
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
中文測試以下:
英文測試以下:
如有理解不到之處,望指正!