Spring Boot国际化
一、根据浏览器语言自动切换
1.设置properties编码(Settings 和 OtherSettings都需要设置)
2.在resources下新建文件夹i18n
3.在建几个properties文件(文件命名)
注:如果使用的是IDEA会自动生成一个国际化文件夹
新建一个国际化文件
随便打开一个文件点击左下角的Resource Bundle
添加字段
给每一个语言文件输入值
4.页面绑定国际化内容(使用的是模板#{}方式)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
<link rel="stylesheet" href="/css/index/index.css" type="text/css" />
</head>
<body>
<div class="language">
<a>中文</a>
<span>|</span>
<a>English</a>
</div>
<div class="box">
<h1 th:text="#{login.tip}">用户登录</h1>
<form action="/login">
<p>error</p>
<ul>
<li>
<span th:text="#{login.uname}">用户:</span>
<input type="text"/>
</li>
<li>
<span th:text="#{login.upwd}">密码:</span>
<input type="text"/>
</li>
<li>
<input type="checkbox"/>[[#{login.remember}]]
</li>
<li>
<input type="button" value="提交" th:value="#{login.btn}"/>
</li>
</ul>
</form>
</div>
</body>
</html>
5.查看绑定后的结果
二、点击链接进行切换
1.添加链接请求参数
<div class="language">
<a th:href="@{/index.html(l='zh_CN')}">中文</a>
<span>|</span>
<a th:href="@{/index.html(l='en_US')}">English</a>
</div>
2.实现LocaleResolver类
package cn.luoruiyuan.login.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) {
String l=request.getParameter("l");
Locale locale=null;
if(!StringUtils.isEmpty(l) && l.indexOf("_")!=-1){
String[] split=l.split("_");
locale=new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
3.将这个类添加到配置类中
package cn.luoruiyuan.login.config;
import cn.luoruiyuan.login.component.MyLocaleResolver;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootConfiguration
public class MyConfig {
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
@Bean
public WebMvcConfigurer myConfiguration(){
WebMvcConfigurer webMvcConfig=new WebMvcConfigurer(){
@Override
//拦截器
public void addInterceptors(InterceptorRegistry registry) {
//addPathPatterns拦截路径
//excludePathPatterns不拦截路径
//registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");
//多个拦截器就添加多个
//registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");
}
@Override
//视图映射器
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/hello请求来到view
registry.addViewController("/") .setViewName("index");
registry.addViewController("/index.html") .setViewName("index");
registry.addViewController("/login.html") .setViewName("index");
}
};
return webMvcConfig;
}
}
4.点击链接查看结果
赞(1)
赏