Spring Boot 国际化步骤

本文详细介绍了如何在项目中创建和配置国际化资源文件,包括不同语言版本的配置文件设置,以及如何在Thymeleaf模板中使用#{...}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •  创建一个国际化配置文件夹

  • 在文件夹中直接创建国际化配置文件login.properties

  • 创建中文配置文件

  • 此时idea识别出文件夹为国际化文件,我们可通过如下方式添加其他国家语言的配置文件

  • 添加完配置文件,点开其中一个配置相关国际化参数

  • 将配置文件路径配置到application.properties中

注意:idea 2018版本不能将路径配置为spring.messages.basename=i18n.login,否则会出现如下情况

  • 使用thymeleaf中的  #{......}  将其取出即可
<form class="form-signin" action="dashboard.html">
		<img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{/asserts/img/bootstrap-solid.svg}" alt=""
			 width="72" height="72">
		<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
		<label class="sr-only" th:text="#{login.username}">Username</label>
		<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required=""
			   autofocus="">
		<label class="sr-only" th:text="#{login.password}">Password</label>
		<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}"
			   required="">
		<div class="checkbox mb-3">
			<label>
				<input type="checkbox" value="remember-me"> [[#{login.remenber}]]
			</label>
		</div>
		<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
		<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
		<a class="btn btn-sm">中文</a>
		<a class="btn btn-sm">English</a>
	</form>
  •  验证结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值