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文件夹下,我们创建三个测试文件,如图:

图片

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

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

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

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

图片

设置后结果:

图片

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

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";    }}

创建一个实体类

@Data@AllArgsConstructorpublic class Student {    private String name;    private int age;    private String email;    private String phone;}

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支持,您的关注是我坚持的动力!

图片

原创不易,转载请注明出处,感谢支持!如果本文对您有用,欢迎转发分享!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值