SpringBoot 国际化配置,SpringBoot Locale 国际化
一、效果如下
二、SpringBoot 国际化配置
1、创建国际化配置文件(3个)
默认状态:login.properties
-
login.btn=Sign in~
-
login.username=Username~
-
login.password=Password~
-
login.tip=Please sign in~
-
login.remember=Remember me~
显示中文:login_zh_CN.properties
-
login.btn=登录
-
login.username=用户名
-
login.password=密码
-
login.tip=请登录
-
login.remember=记住我
显示英文:login_en_US.properties
-
login.btn=Sign in
-
login.username=Username
-
login.password=Password
-
login.tip=Please sign in
-
login.remember=Remember me
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性
-
#表示放在classpath的i18n文件夹,文件前缀为mess
-
spring.messages.basename=i18n.login
2、自定义国际化语言解析器
-
package com.atguigu.springboot.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 httpServletRequest) {
-
//获取Http请求传过来的“l”参数
-
String l = httpServletRequest.getParameter("l");
-
Locale locale = Locale.getDefault();
-
//如果l为空则用浏览器默认语言
-
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) {
-
-
}
-
}
3、把国际化语言解析器放到Spring容器中:
这里创建了一个自定义的配置类:MyMvcConfig,继承WebMvcConfigurerAdapter,可以扩展SpringMvc的功能,包括拦截器,转换器等
-
package com.atguigu.springboot.config;
-
-
import com.atguigu.springboot.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 LocaleResolver localeResolver() {
-
return new MyLocaleResolver();
-
}
-
-
}
4、页面显示及切换国际化操作:
这里用到thymeleaf模板引擎,使用#{}获取我们之前配置的国际化文件里面的字段,如:th:text="#{login.username}"
-
<!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>尚硅谷</title>
-
<!-- Bootstrap core CSS -->
-
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.1.3/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" th: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="@{/index.html(l='zh_CN')}">中文</a>
-
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
-
</form>
-
-
</body>
-
-
</html>
特别注意:
①、要看一下是否配置了:spring.messages.basename
-
#表示放在classpath的i18n文件夹,文件前缀为mess
-
spring.messages.basename=i18n.login
②、国际化配置文件的名称是否写对?规则:fileName_语言代码(zh)_国家代码(CN).properties
如:login_zh_CN.properties
③、th:href="@{/index.html(l='zh_CN')中的“l”对应 String l = httpServletRequest.getParameter("l");
文章来源: blog.csdn.net,作者:轻狂书生FS,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/LookForDream_/article/details/85245086
- 点赞
- 收藏
- 关注作者
评论(0)