以登录界面为例,
1、准备工作,使用IDEA创建一个springboot工程,pom.xml中导入thymeleaf
2、在resources中创建一个i18n文件夹,然后依次创建下面三个文件
login.properties login_zh_CN.properties login_zh_CN.properties
3、检查IDEA的配置,在Setting中搜索file encoding

需要按照如图所示配置,改完以后记得Other Setting也要同样修改
打开任意一个properties,找到界面中的Resources Bundle

按照如上图的格式修改
打开application.properties,添加
spring.messages.basename=i18n.login
在login.html中
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<form class="form-signin">
<h1 th:text="#{login.tip}">Please sign in</h1>
<input type="text" placeholder="Username" th:placeholder="#{login.username}" >
<input type="password" placeholder="Password" th:placeholder="#{login.password}" >
<button type="submit" th:text="#{login.sign}">Sign in</button>
</form>
运行工程,在浏览器(我用的Chrome)选择英语(美国)或者中文,即可切换语言
本文详细介绍如何在SpringBoot项目中实现国际化,包括创建工程、配置文件、修改properties文件及在页面中应用多语言显示。
9499

被折叠的 条评论
为什么被折叠?



