国际化处理 springboot2.3.1

国际化处理
springboot已经为我们自动配置了
我们去看看MessageSourceAutoConfiguration
找到如图所示的位置,或者双击Shift搜索MessageSourceAutoConfiguration
在这里插入图片描述
在这里插入图片描述
可以看到是使用spring.message进行配置,具体有哪些配置可以去看MessageSourceProperties

MessageSourceProperties这个位置跟MessageSourceAutoConfiguration是同目录下的。如第一张图框选的位置。
映入眼帘就可以看到MessageSourceAutoConfiguration中一个@Bean注解的MessaheSouce

在这里插入图片描述
可以看到在容器配置了一个MessaheSouce 里面ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource()

ResourceBundleMessageSource: 提供springmvcz中提供国际化功能的类。这个类的作用就是读取资源属性文件(.properties),然后根据.properties文件的名称信息(也就是本地化信息),匹配当前系统的国际名和语言信息(也可以程序指定),然后获取相应的properties文件的内容。
然后可以看到messaheSouce setBasenames,setDefaultEncoding,setFallbackToSystemLocale等属性,这些属性在MessageSourceProperties都是一以对应的,也就是去读取配置文件的信息。
所以现在知道springboot已经帮我们配置好了。
那怎么进行国际化处理呢
Resources下创建一个目录为i18n,创建你需要的国际化配置文件
在这里插入图片描述
打开配置文件,下面显示了Resource Bundle,点击
在这里插入图片描述
点击左上方**+**号,设置一个名字(也就是properties配置文件中你想要的Key值)
在这里插入图片描述

在这里插入图片描述
然后切换到Text界面
在这里插入图片描述
可以看到配置的属性了,
在这里插入图片描述
接下来去application.properties或者application.yml中配置basename,让springboot去查找具体目录下的配置文件。
在这里插入图片描述
那么就可以使用了。
这是login.html使用了ThymeLeaf使用#{}获取国际化属性@{}获取链接${}获取变量数据
@{/login.html(l='zh_CN')}等同于@{/login.html?l=zh_CN}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Signin Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.5.0/css/bootstrap.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>

<body class="text-center">
<form class="form-signin" action="dashboard.html">
    <img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{/asserts/img/bootstrap-solid.png}" alt=""
         width="120" 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" name="Username" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
    <label class="sr-only" th:text="#{login.password}">Password</label>
    <input type="password" name="Password" th:placeholder="#{login.password}" class="form-control" placeholder="Password" required="">
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"/> [[#{login.remember}]]
        </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>
    <!--第一种thymeLeaf写法-->
    <a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
    <!--第二种传统写法-->
    <a class="btn btn-sm" th:href="@{/login.html?l=en_US}">English</a>
</form>

</body>

</html>

运行起来之后结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值