Kite的学习历程之SpringBoot中的国际化

Kite学习框架的第十六天

1. SpringBoot国际化

首先看一下我们的登录页面
Kite的学习历程之SpringBoot中的国际化_第1张图片
需求:
当我们点击下方的中文或者英文,切换中文和英文页面

1.1 首先创建国际化配置文件

Kite的学习历程之SpringBoot中的国际化_第2张图片
首先创建一个默认的login.properties
然后创建
英文:login_en_US.properties
中文:login_zh_CN.properties
然后点击下面的:Resource Bundle
在这里插入图片描述
进行创建
Kite的学习历程之SpringBoot中的国际化_第3张图片
这时我们可以同时配置三个properties文件

1.2 在login.html页面通过thymelead添加国际化信息

页面全部代码:
主要的国际化代码有:
国际化使用#{} 这个结构进行引入

<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<input type="text" class="form-control"
				   th:placeholder="#{login.username}"
				   required="" autofocus="">
<input type="password" class="form-control" 0
				   th:placeholder="#{login.password}" required="">
<input type="checkbox" value="remember-me" >[[#{login.remember}]]
<!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:ref="@{/webjars/bootstrap/4.1.2/css/bootstrap.css}" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link href="asserts/css/signin.css"
			  th:ref="@{asserts/css/signin.css}" rel="stylesheet">
	</head>

	<body class="text-center">
		<form class="form-signin" action="dashboard.html">
			<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" 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" ></label>
			<input type="text" class="form-control"
				   th:placeholder="#{login.username}"
				   required="" autofocus="">
			<label class="sr-only" >Password</label>
			<input type="password" class="form-control" 0
				   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>
			<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
			<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
		</form>

	</body>
</html>

1.3 实现下面的点击切换

在这里插入图片描述通过改变请求头中的信息
中文

	<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>

英文:

	<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

1.4 创建自己的MyLocalResolver

国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);
这个方法返回了区域信息对象

package cn.kitey.springboot.component;

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;

/**
 * 可以连接上携带区域信息
 */
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        //l为空的时候返回默认值
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            //获取一个数组,通过 _ 分隔符分隔
            String[] split = l.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

1.5 在MyMvcConfig配置类中将获取的localer传入SpringBoot容器中

    //将我们获取的国际化信息传入容器
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }


这时点击就会实现国际化
Kite的学习历程之SpringBoot中的国际化_第4张图片
Kite的学习历程之SpringBoot中的国际化_第5张图片

这里可以注意一下地址栏中请求信息!

加油偶!

你可能感兴趣的:(每天的学习笔记)