1:首先,在resources目录下创建i18n包,然后创建login.properties
2:在index.html中:
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zn_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
3:创建MyLocalResolver类在config中
仿照国际化接口重写方法!implements LocaleResolver
package com.example.springbootweb.config;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* @author ${范涛之}
* @Description
* @create 2021-09-19 13:35
*/
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数链接
String language = request.getParameter("l");
Locale locale = Locale.getDefault(); //如果没有就是用默认的
//如果请求的链接携带了国际化的参数
if (!StringUtils.isEmpty(language)){
//zn_CN
String split[] = language.split("_");
//国家_地区
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}