[项目实战(一)]
[项目实战(二)]
[项目实践(四)]
[项目实践(五)]
[项目实践(六)]
[项目实践(七)]
[项目实践(八)]
[项目实践(九)]
通俗地讲,国际化就是实现网页语言的切换
一、配置首页的国际化
(1)国际化涉及语言,就得要考虑编码,故应使IDEA编码格式为utf-8
(2)在resources目录下创建i18n(internationalization)目录,并在此目录下创建下列文件
注意“Resource Bundle ‘login’”目录是系统自动生成的
(3)配置登录元素国际化
点击“Resource Bundle”按钮进入可视化界面
4)在application.yaml文件中新增国际化的相关配置
spring:
thymeleaf:
cache: false
messages:
basename: i18n.login
二、修改前端页面
修改index.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 th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/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>
<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" value="remember-me"> [[#{login.Remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">
[[#{login.btn}]]
</button>
<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>
</form>
</body>
</html>
2)启动项目成功后,在浏览器中输入
http://localhost:8080/index.html
三、新建国际化解析器配置类
(1)新建国际化解析器配置类
package com.xuyuan.springboot03web.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;
/**
* @className: MyLocalResolver
* @description: 国际化解析器配置类
*/
public class MyLocaleResolver implements LocaleResolver {
//解析请求
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数
String language = request.getParameter("l");
//如果没有就使用默认的
Locale locale = Locale.getDefault();
//请求链接携带了国际化参数
if (!StringUtils.isEmpty(language)) {
//格式:zh_CN
String[] splits = language.split("_");
//国家 地区
locale = new Locale(splits[0], splits[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
2)在MVC配置类里面将定义的国际化组件放入ioc容器中
package com.xuyuan.springboot03web.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 MyMvcConfig 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();
}
}
(3)再次启动项目成功后,在浏览器中输入
http://localhost:8080/index.html
四、总结
关于实现国际化需要注意的地方有:
对于前端界面的每一个元素都需要创建国际化配置项,并且要在springboot默认的配置文件里指明国际化配置项文件位置
需要创建一个国际化配置类,并且要在之前建立好的MVC配置类里面将国际化组件放入ioc容器中