SpringBoot-7-国际化
本文是使用SpringBoot+Thymeleaf进行SpringBoot国际化讲解的,如果不是很了解Thymeleaf的可以,查看我之前的文章。码字不易希望大家可以关注我的公众号,你的关注是对我最大的支持谢谢,文章最后我的公众号二维码,也可搜索springboot葵花宝典进行关注,回复:springboot,可以获取一些博主搜集的SpringBoot学习资料。
国际化简介
SpringBoot国际化,是我们所有做国际性网站都应该考虑的问题,SpringBoo对国际化有着强劲的支持。国际化,也叫i18n,为什么叫i18n呢?这是因为国际化的英文单词是internationalization ,i和n之间包含了18个单词。本文通过介绍一个Springboot案例介绍其国际化。
1. SpringBoot国际化的三种方式
Springboot国际化存在三种使用方式:
-
AcceptHeaderLocaleResolver (默认解析器,通过请求头的 Accept-Language 字段来判断当前请求所属的环境的,进而给出合适的响应)
-
SessionLocaleResolver
-
CookieLocaleResolver
默认AcceptHeaderLocaleResolver实现国际化
在默认情况下,国际化文件夹直接放在srcmainresources
文件夹下,我们创建三个测试文件,如图:
-
我们在创建messages文件的时候,直接创建在
srcmainresources
目录下,这个时候IDEA会多显示出一个Resource Bundle,这个大家不用管,千万别手动去创建这个目录。 -
messages.properties 这个是默认的配置,其他的则是不同语言环境下的配置,en_US 是英语(美国),zh_CN 是中文简体。
-
在不设置的情况下中文不能正常显示。
设置方式idea的settings 中输入File Encoding,然后全部设置为UTF-8就可以了。
设置后结果:
在这三个创建和的文件中,分别写入以下内容:
messages_en_US.properties
name=Name
age=Age
email=Email
phone=Phone
messages_zh_CN.properties
name=姓名
age=年龄
email=邮箱
phone=电话
注:在 Spring 中需要配置的 MessageSource 现在不用配置了,Spring Boot 会通过org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration
自动帮我们配置一个 MessageSource 实例。
创建一个TestController,内容如下:
@Controller
public 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
@AllArgsConstructor
public class Student {
private String name;
private int age;
private String email;
private String phone;
}
在srcmainresourcestemplates
目录下创建一个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-CN
http://localhost:8080/?lang=en-US
结果如下:
其他自定义
如果messages不想存放在srcmainresources
目录下,可以使用以下进行修改,将文件保存在srcmainresourcesi18n、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
支持,您的关注是我坚持的动力!
原创不易,转载请注明出处,感谢支持!如果本文对您有用,欢迎转发分享!
原文始发于微信公众号(springboot葵花宝典):SpringBoot-7-国际化
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/184446.html