SpringBoot4-Web4-国际化
国际化
- 编辑国际化配置文件
- 使用ResourceBundleMessageSource管理国际化资源文件
- 在页面使用fmt:message取出国际化内容 ### 创建resources/i18n 然后创建login_zh_CN.properties 选择Resouerce Bundle SpringBoot自动创建了管理国际化资源文件的组件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MessageSourceAutoConfiguration {
private static final Resource[] NO_RESOURCES = {};
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
}
public MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils
.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
if (properties.getEncoding() != null) {
messageSource.setDefaultEncoding(properties.getEncoding().name());
}
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
Duration cacheDuration = properties.getCacheDuration();
if (cacheDuration != null) {
messageSource.setCacheMillis(cacheDuration.toMillis());
}
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/**
* Configuration properties for Message Source.
*
* @author Stephane Nicoll
* @author Kedar Joshi
* @since 2.0.0
* /
public class MessageSourceProperties {
/**
* Comma-separated list of basenames (essentially a fully-qualified classpath
* location), each following the ResourceBundle convention with relaxed support for
* slash based locations. If it doesn't contain a package qualifier (such as
* "org.mypackage"), it will be resolved from the classpath root.
*/
private String basename = "messages";### thymeleaf 取国际化信息 使用#{}1
spring.messages.basename = i18n.login
### 解决乱码 setting - editor - fileEncoding - utf8 - 自动转阿斯克码 ### 测试 在浏览器中选择浏览器默认的语言就可以了,即他可以根据浏览器的语言信息设置语言了 ### 如何实现点按钮实现不同语言呢 #### 国际化原理 locale: LocaleResolver 根据请求头的区域信息来进行国际化1
<h1 class="..." th:text="#{login.tip}">Please sign in</h1>
1
2
3
4
5
6
7
8
9
10
11
12
public LocaleResolver localeResolver() {
if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}先写个链接把区域信息加上去1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Locale resolveLocale(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale();
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale;
}
Locale requestLocale = request.getLocale();
List<Locale> supportedLocales = getSupportedLocales();
if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
return requestLocale;
}
Locale supportedLocale = findSupportedLocale(request, supportedLocales);
if (supportedLocale != null) {
return supportedLocale;
}
return (defaultLocale != null ? defaultLocale : requestLocale);
}然后自己实现区域信息解析器1
2<a class="..." th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="..." th:href="@{/index.html(l='en_US')}">English</a>1
2
3
4
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package com.wsx.springboothelloworld.component;
import org.springframework.cglib.core.Local;
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 {
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
if(!StringUtils.isEmpty(l)) {
String[] split = l.split("_");
return new Locale(split[0], split[1]);
}
return Locale.getDefault();
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
处理post 1
2
3
4
5
6
7
8
9
10
11
12package com.wsx.springboothelloworld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class LoginController {
public String login(){
return "dashborad";
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.wsx.springboothelloworld.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class LoginController {
// @RequestMapping(value="",method= RequestMethod.POST)
public String login( String username,
{ String password)
return "dashborad";
}
}1
spring.thymeleaf.cache=false
做一个判断来决定标签是否生效 1
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
表单重复提交问题。需要用重定向、视图、拦截器解决,重定向加视图能确保没有重复提交,但是会导致直接跳过登陆的问题,
拦截器
创建拦截器 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package com.wsx.springboothelloworld.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object loginUser = request.getSession().getAttribute("loginUser");
if (loginUser == null) {
request.setAttribute("msg","没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
} else {
return true;
}
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class LoginController {
// @RequestMapping(value="",method= RequestMethod.POST)
public String login( String username,
String password,
Map<String, Object> map,
HttpSession httpSession) {
if (password.endsWith("123456")) {
httpSession.setAttribute("loginUser", username);
return "dashborad";
} else {
return "404";
}
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
return new WebMvcConfigurerAdapter() {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/wsx2").setViewName("templates_hello");
registry.addViewController("/wsx3").setViewName("templates_hello");
}
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
//
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/user/login");
}
};
}