- 在resources下创建如下配置文件admin、before、common
- 配置属性
- adminMessages.properties
- adminMessages_en_US.properties
- adminMessages_zh_CN.properties
- 在application.properties中添加如下配置
spring.messages.basename=i18n/admin/adminMessages,i18n/before/beforeMessages,i18n/common/commonMessages
4. 创建LocaleConfig配置类
package com.gh.firstdemo;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
/**
* @author gaohan
* @version 1.0
* @date 2020/7/25 21:54
*/
@Configuration // 标识当前类为配置类
@EnableAutoConfiguration // TODO 不明该注解意思
public class LocaleConfig implements WebMvcConfigurer {
/**
* 根据用户本次会话过程中的语义设定语言区域
* (如用户进入首页前选择的语言种类)
* @return
*/
@Bean // 表明这个方法需要交给Spring进行管理
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.CHINA);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 选择语言的参数名
lci.setParamName("locale");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
- 创建html页面
admin.html
<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<span th:text="#{admin}"></span><br/>
<a th:href="@{/i18n/first}" th:text="#{return}"></a>
</body>
</html>
before.html
<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<span th:text="#{before}"></span><br>
<a th:href="@{/i18n/first}" th:text="#{return}"></a>
</body>
</html>
first.html
<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<a th:text="#{chinese.key}" th:href="@{/i18n/first(locale='zh_CN')}"></a>
<a th:text="#{english.key}" th:href="@{/i18n/first(locale='en_US')}"></a>
<br>
<a th:text="#{test.admin}" th:href="@{/i18n/admin}"></a>
<a th:text="#{test.before}" th:href="@{/i18n/before}"></a>
</body>
</html>
- 创建Controller
注意:框框处不能有斜杠,否则项目打包成jar运行时会访问不到页面
package com.gh.firstdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author gaohan
* @version 1.0
* @date 2020/7/25 22:11
*/
@Controller
@RequestMapping("i18n")
public class I18nTestController {
@RequestMapping("/first")
public String index(){
return "i18n/first";
}
@RequestMapping("/admin")
public String admin(){
return "i18n/admin";
}
@RequestMapping("/before")
public String before(){
return "i18n/before";
}
}
- 运行测试
项目demo源码如下:
https://github.com/YUNZE-GH/FirstSpringBootDemo.git