springboot中的国际化
1.在resources中创建i18n文件夹
以登陆为例,创建login.properites文件,然后可以创建login_en_US.properites或者login_zh_CN.properites文件,在里面配置各个字段的数据。
2.然后再application.yaml中添加配置
3.然后再HTML中使用thymeleaf模板引擎
<html lang="en" xmlns:th="http://www.thymeleaf.org">
一定要添加上面那句话,添加thymeleaf的命名空间
3.1thymeleaf的基本语法
· 简单表达式 (simple expressions)
${…} 变量表达式
*{…} 选择变量表达式
#{…} 消息表达式
@{…} 链接url表达式
· 字面量
‘one text’,‘another one!’,… 文本
0,34,3.0,12.3,… 数值
true false 布尔类型
null 空
one,sometext,main 文本字符
· 文本操作
+ 字符串连接
|The name is ${name}| 字符串连接
· 算术运算
+ , - , * , / , % 二元运算符
- 负号(一元运算符)
· 布尔操作
and,or 二元操作符
!,not 非(一元操作符)
· 关系操作符
> , < , >= , <= (gt , lt , ge , le)
== , != (eq, ne)
3.2thymeleaf应用注意事项
<link th:href="@{/css/css/bootstrap.min.css}" rel="stylesheet">
<img class="mb-4" th:src="@{/css/brand/bootstrap-logo.svg}" alt="" width="72" height="57">
<link th:href="@{/css/css/signin.css}" rel="stylesheet">
4.thymeleaf与国际化的结合使用
<h1 class="h3 mb-3 fw-normal" th:text="#{login.tip}">Please sign in</h1>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me" > [[#{login.remember}]]
</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit" >[[#{login.log}]]</button>
<!-- th:value="#{login.log}"不用这个-->
显示效果
中英文切换代码
<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>