1、首先设置号idea的字符格式
2、在resources下创建login.properties、login_zh_CN.properties、login_en_US.properties
3、静态资源设置
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.PleaseSignIn}">Please sign in</h1>
<input type="text" class="form-control" th:placeholder="#{login.Username}" required="" autofocus="">
<input type="password" class="form-control" th:placeholder="#{login.Password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox"> [[#{login.RememberMe}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.SignIn}]]</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">English</a>
</form>
</body>
4、application.yaml配置路径
spring:
messages:
basename: i18n.login
5、处理的组件类!
MyLocaleResolver
package com.wu.config;
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 lang= request.getParameter("lang");
Locale locale = Locale.getDefault();//如果没用就使用默认的
if(!StringUtils.isEmpty(lang))
{
String[] sp = lang.split("_");
locale=new Locale(sp[0],sp[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
6、最后在自己的配置文件中注册
package com.wu.config;
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;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
@Bean
public LocaleResolver localeResolver()
{
return new MyLocaleResolver();
}
}