PS: The original text was first published on WeChat public account: Jongxingzhi (jzman-blog)
The previous articles have tried interface development, Thymeleaf templates, common syntax and template layout. You can read the previous articles before reading this article:
Before reading this article, you can read the first few articles:
- Development of an interface in the Spring Boot series
- Getting started with Thymeleaf template of Spring Boot series
- Thymeleaf common syntax of Spring Boot series
- Thymeleaf template layout of Spring Boot series
The internationalization of the Spring Boot project is very simple to implement. In order to ensure the continuity of the Spring Boot series, the internationalization of the project is also a separate article. The main contents are as follows:
- Zone resolver
- Create internationalization profile
- Create configuration class
- Test effect
Zone resolver
Spring provides regional parser to identify the user area, in order to realize the internationalization of Web applications, regional parser must implement the LocalResolver
interface can be implemented to implement the interface to create your own parser area
AcceptHeaderLocaleResolver
: The default zone resolver, which resolves the zone through the accept-language parameter of the HTTP request;SessionLocaleResolver
: Valid in the current session, otherwise it will be restored to the default state;CookieLocaleResolver
:Valid within the validity period of the current cookie, otherwise it will be restored to the default state;FixedLocaleResolver
: The setting is fixed andLocal
it is not convenient to change dynamicallyLocal
;- Use
LocaleChangeInterceptor
interceptor resolve user area.
The following will LocaleChangeInterceptor
interceptors and SessionLocaleResolver
an example to internationalize Spring Boot project.
Create internationalization profile
Spring Boot supports internationalization, and it is more convenient to use. Create a Spring Boot project, create a folder named i18n under resources, and then right-click and select New->Resourc Bundle to create a configuration file corresponding to the language home The .properties file is as follows:
Application.properties then disposed in the configuration file in the file Message
path as follows:
# Message路径
spring.messages.basename=i18n.home
How to get the value in the home.properties property file, as follows:
mMessageSource.getMessage("home.title", null, LocaleContextHolder.getLocale());
Create configuration class
Use LocaleChangeInterceptor
interceptors and SessionLocaleResolver
parameters is provided corresponding to Local
, the following:
/**
* I18nConfig
*/
@Configuration
public class I18nConfig {
@Bean
public LocaleResolver localeResolver (){
return new SessionLocaleResolver();
}
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor changeInterceptor = new LocaleChangeInterceptor();
changeInterceptor.setParamName("lang");
registry.addInterceptor(changeInterceptor);
}
};
}
}
Case code
I18nController
as follows:
@Controller
public class I18nController {
@GetMapping(value = "/i18n")
public String il8n() {
return "i18n";
}
}
The template file is as follows:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot I18n Sample.</title>
<script>
/**
* 页面加载完成后调用
*/
onload = function() {
let href = window.location.toString();
let suffix = href.substring(href.length - 2, href.length);
let lang = document.getElementsByClassName("lang");
console.log("--suffix--" + suffix);
switch (suffix) {
case "CN":
setStatus(lang[0], lang[1], lang[2]);
break;
case "TW":
setStatus(lang[1], lang[0], lang[2]);
break;
case "US":
setStatus(lang[2], lang[1], lang[0]);
break;
default:
setStatus(lang[0], lang[1], lang[2]);
}
};
/**
* 设置语言标签状态
* @param show 已选择的语言标签
* @param def1 未选择的语言标签
* @param def2 未选择的语言标签
*/
function setStatus(show, def1, def2) {
show.style.color = "rgb(0,0,255)";
def1.style.color = "rgb(0,0,0)";
def2.style.color = "rgb(0,0,0)";
}
</script>
</head>
<body>
<h3 th:text="#{home.title}">default</h3>
<p style="font-size: 12px">
<a class="lang" href="i18n?lang=zh_CN"> 简体中文 </a>|
<a class="lang" href="i18n?lang=zh_TW"> 繁体中文 </a>|
<a class="lang" href="i18n?lang=en_US"> English</a>
</p>
</body>
</html>
Test effect
The operation effect is as follows:
You can follow the public account [Jianxingzhi] to exchange and learn, and reply to the keyword [Spring Boot] to obtain the source link of the corresponding case.