springboot配置国际化功能
在resources文件夹下创建i18n文件夹
 

 会自动创建message.properties,message_en_US.properties和message_zh_CN.properties配置文件
点击Resource Bundle添加数据
 
 
修改application.yml
 messages:   # 配置国际化绑定的文件 不绑定会乱码
    encoding: utf-8
    basename: i18n.message
 
导入thymeleaf和web启动类jar包
 		>
            >org.springframework.boot >
            >spring-boot-starter-thymeleaf >
         >
        >
            >org.springframework.boot >
            >spring-boot-starter-web >
         >
 
新建MyLocalResolver编写自定义的Locale区域解析器
/**
 * @ClassName: MyLocalResolver
 * @Description: 编写自定义的LocalResolver
 * SpringBoot默认的Locale解析器是根据请求头的区域信息进行解析的(浏览器语言)
 *  使用自定义的Locale解析器对url的区域信息进行解析达到点击切换区域效果
 *  一旦我们自定义的区域解析器注册到Spring容器中,则SpringBoot提供的将不自动注册
 * @Author: ZH
 * @Date 2021/10/19  11:54
 * @Version 1.0
 */
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest req) {
        // 获取l后面的参数
        String l = req.getParameter("l");
        Locale locale = Locale.getDefault();
        //没有l使用默认配置,有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) {
    }
}
 
注册我们自定义的区域解析器
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResolver();
    }
}
 
编写hello.html引入国际化内容
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hellotitle>
head>
<body>
    <p th:text="#{message.name}">sdap>
    <p th:text="#{message.age}">123p>
body>
html>
 
编写控制器返回hello.html
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
 
运行
 
 
如果字符乱码修改File Encodings
 