第十三章:SpringBoot_Web——(国际化配置)

总目录:SpringBoot学习教程


我们在访问一个页面的时候,一般可以切换中文和English二个显示界面。

我们这里做一个简单的配置,并配置一下,文章最后提供源码。

1.我在pom中导入一些依赖,web依赖,(thymeleaf,bootstrap,jquery这三个都是在测试页面中使用的)。

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--Jquery-->
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>jquery</artifactId>
			<version>3.3.1</version>
		</dependency>
		<!--bootstrap-->
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>bootstrap</artifactId>
			<version>4.0.0-2</version>
		</dependency>
		<!--thymeleaf-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.编写国际化配置文件

 ①在resources下面创建一个文件夹 命名 i18n ,文件夹下面创建properties文件.

那个Resource Bundle 'login'是他自动识别创建的

看一下里面的内容,这个在编写的时候,可以点击文件下面的这个选项,加快配置.

我配置一个汉语的一个英语的,还有一个默认的吗,三个文件。

#login.properties中的内容
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
#login_en_US.properties
login.btn=Sign in
login.password=Password
login.remember=Remember me
login.tip=Please sign in
login.username=UserName
#login_zh_CN.properties
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名

3.创建一个类,主要是让浏览器默认去加载我们指定的界面。

package com.example.springbootresufullcrud.config;

import com.example.springbootresufullcrud.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.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Bean
    public  WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter=  new WebMvcConfigurerAdapter(){
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/login.html").setViewName("login");
            }
        };
        return adapter;
    }
    @Bean
    public LocaleResolver localeResolver(){
        return  new MyLocaleResolver();
    }

}

4.点击页面切换国家化的主要配置类。

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.util.StringUtils;
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 = 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) {

    }
}

5.我们的login.html主页面的编写,我们使用的Templates模板引擎,所以页面放在了templates下面了。

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-2/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>
	<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
	<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
</form>

</body>


</html>

可以在最下面看到,我们使用th:href="@{/login.html(l='zh_CN')}"来切换国际化。

还有input中的th:placeholder="#{login.password}"这个配置,来显示中文或英文,使用zh_CN显示中文,en_US显示英文。

还有一些静态资源文件我放在了项目的static下面了,可以下载项目查看。

访问localhost:8080




项目地址:https://pan.baidu.com/s/1qLmjDl3q3CfjDaDJO4TGCQ





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值