一、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();
}
}
中文测试以下:
英文测试以下:
如有理解不到之处,望指正!