SpringBoot-7-国际化

简介: SpringBoot-7-国际化

SpringBoot-7-国际化


本文是使用SpringBoot+Thymeleaf进行SpringBoot国际化讲解的,如果不是很了解Thymeleaf的可以,查看我之前的文章。码字不易希望大家可以关注我的公众号,你的关注是对我最大的支持谢谢,文章最后我的公众号二维码,也可搜索springboot葵花宝典进行关注,回复:springboot,可以获取一些博主搜集的SpringBoot学习资料。


SpringBoot-5-页面展示Thymeleaf

SpringBoot-6-模板Thymeleaf常用标签



国际化简介


SpringBoot国际化,是我们所有做国际性网站都应该考虑的问题,SpringBoo对国际化有着强劲的支持。国际化,也叫i18n,为什么叫i18n呢?这是因为国际化的英文单词是internationalization ,i和n之间包含了18个单词。本文通过介绍一个Springboot案例介绍其国际化。


1. SpringBoot国际化的三种方式



Springboot国际化存在三种使用方式:


AcceptHeaderLocaleResolver (默认解析器,通过请求头的 Accept-Language 字段来判断当前请求所属的环境的,进而给出合适的响应)


SessionLocaleResolver


CookieLocaleResolver


默认AcceptHeaderLocaleResolver实现国际化

在默认情况下,国际化文件夹直接放在src\main\resources文件夹下,我们创建三个测试文件,如图:


b0a5584502c475df0b98e7054225cc3c.png


我们在创建messages文件的时候,直接创建在src\main\resources目录下,这个时候IDEA会多显示出一个Resource Bundle,这个大家不用管,千万别手动去创建这个目录。


messages.properties 这个是默认的配置,其他的则是不同语言环境下的配置,en_US 是英语(美国),zh_CN 是中文简体。


在不设置的情况下中文不能正常显示。


设置方式idea的settings 中输入File Encoding,然后全部设置为UTF-8就可以了。


f3ce7a8e7d78f26da666c5ef648fa591.png


设置后结果:



8ee2fe76bdcb9f6171d5ad496da1ab2f.png


在这三个创建和的文件中,分别写入以下内容:

messages_en_US.properties

name=Nameage=Ageemail=Emailphone=Phone


messages_zh_CN.properties

name=姓名age=年龄email=邮箱phone=电话


注:在 Spring 中需要配置的 MessageSource 现在不用配置了,Spring Boot 会通过org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration 自动帮我们配置一个 MessageSource 实例。


创建一个TestController,内容如下:

@Controllerpublic class TestController {    @GetMapping("/")    public String index(Model model) {        Student stu1 = new Student("张三", 20, "1155@qq.com", "13333835901");        Student stu2 = new Student("李四", 21, "1154@qq.com", "13333835902");        Student stu3 = new Student("王五", 22, "1153@qq.com", "13333835903");        Student stu4 = new Student("小芳", 23, "1156@qq.com", "13333835904");        ArrayList<Student> stus = new ArrayList<>();        stus.add(stu1);        stus.add(stu2);        stus.add(stu3);        stus.add(stu4);        model.addAttribute("stus", stus);        return "index";    }}


创建一个实体类

创建一个实体类
@Da


src\main\resources\templates目录下创建一个index.html,内容如下

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"      xmlns:th="http://www.thymeleaf.org"><head >    <meta charset="UTF-8">    <title>index</title>    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">    <link rel="stylesheet" th:href="@{/css/dashboard.css}">    <script th:src="@{/js/jquery-3.3.1.min.js}"></script>    <script th:src="@{/js/bootstrap.min.js}"></script></head><body><div>    <table border="1" cellspacing="0">        <tr>            <th th:text="#{name}"></th>            <th th:text="#{age}"></th>            <th th:text="#{email}"></th>            <th th:text="#{phone}"></th>        </tr>        <tr th:each="stu:${stus}">            <td th:text="${stu.name}"></td>            <td th:text="${stu.age}"></td>            <td th:text="${stu.email}"></td>            <td th:text="${stu.phone}"></td>        </tr>    </table></div></body></html>



使用POSTMAN,分别在请求的头中设置 Accept-Language 为 zh-CN、en-US,查看结果如下:

自定义切换Local保存方式



有时候我们感觉,将切换参数放入到请求头比较麻烦不方便,这个时候我们就可以进行自定义解析,SessionLocaleResolver 将客户端的 Locale 保存到 HttpSession 对象中,CookieLocaleResolver 将LOCAL保存到Cookie中。


无论是哪一种自定义的LocalResolver都需要定义LocaleChangeInterceptor,,用于拦截请求中 key 为 lang 的参数(不配置的话是 locale),这个参数则指定了当前的环境信息。

    @Override    public void addInterceptors(InterceptorRegistry registry) {        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();        interceptor.setParamName("lang");        registry.addInterceptor(interceptor);    }


如果使用SessionLocaleResolver

    @Bean    LocaleResolver localeResolver() {        SessionLocaleResolver localeResolver = new SessionLocaleResolver();        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);        return localeResolver;    }


如果使用SessionLocaleResolver

    @Bean    public LocaleResolver localeResolver() {        SessionLocaleResolver slr = new SessionLocaleResolver();        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);        return slr;    }


配置完成后启动分别访问

http://localhost:8080/?lang=zh-CNhttp://localhost:8080/?lang=en-US



结果如下:

其他自定义


如果messages不想存放在src\main\resources\目录下,可以使用以下进行修改,将文件保存在src\main\resources\i18n、messages下:

spring:  thymeleaf:    cache: false  messages:    basename: i18n/messages

其他编码

spring:  messages:    cache-duration: 1800    encoding: UTF-8    fallback-to-system-locale: true

spring.messages.cache-duration 表示文件的缓存失效时间


fallback-to-system-locale 表示如果该属性为 true,则会默认查找当前系统对应的资源文件,否则就返回 null,返回 null 之后,最终又会调用到系统默认的 messages.properties 文件。


如果您觉得本文不错,欢迎Star支持,您的关注是我坚持的动力!

目录
相关文章
|
9月前
|
Java Spring 容器
Spring Boot入门(十五) 之 国际化操作(页面的中英文相互切换)
Spring Boot入门(十五) 之 国际化操作(页面的中英文相互切换)
355 0
|
5月前
|
Java 容器
SpringBoot下资源国际化应用实践
SpringBoot下资源国际化应用实践
54 0
|
7月前
|
Java
19 SpringBoot国际化
19 SpringBoot国际化
28 0
|
7月前
|
自然语言处理 Java 物联网
TDengine 资深研发整理:基于 SpringBoot 多语言实现 API 返回消息国际化
为了帮助开发者更好地进行 SpringBoot 的开发,避免开发盲点,我们将 TDengine 资深研发所做的内部分享——《SpringBoot 多语言支持方案》进行了相关整理,给到有需要的开发者参考。
99 1
|
9月前
|
Java
SpringBoot实现国际化
SpringMVC国际化步骤 1. 编写国际化配置文件 2. 使用ResourceBundleMessageSource管理国际化资源文件 3. 在 页面使用fmt:message取出国际化内容 以上是SpringMvc步骤,SpringBoot更简单了,直接帮我们配置好了。
|
9月前
|
前端开发 Java
【SpringBoot】国际化配置
【SpringBoot】国际化配置
160 0
【SpringBoot】国际化配置
|
9月前
|
缓存 Java Spring
SpringBoot 实战:国际化组件 MessageSource 执行逻辑与源码
本章我们一起看下 ResourceBundleMessageSource 和 ReloadableResourceBundleMessageSource 的执行逻辑。SpringBoot 的 MessageSource 组件有很多抽象化,源码看起来比较分散,所以本文会通过流程图的方式进行讲解。
|
11月前
|
前端开发 Java Spring
Spring Boot 国际化
1.准备国际化文件 建三个配置文件 第一个负责显示初始化的值 第二个映射第一种语言 第二个映射第二种语言 建好三个properties后IDEA会自动识别放到一个bundle下
68 0
|
11月前
|
前端开发 Java 开发者
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化(下)
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化(下)
105 0
|
11月前
|
移动开发 前端开发 JavaScript
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化
62 0