Thymeleaf 配置国际化页面

1.在依赖中加入Thymeleaf SpringWeb

2.在resoruces下创建i18n文件夹,配置国际化翻译配置

在i18n包下新建login.properties,等语言文件

.

编写相关配置

3.在application.properties中添加国际化文件的基础名 

4.自定义配置类,以用于定制解析国际化功能

@Configuration
public class MyLocalResovel implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l =request.getParameter("1");
//        String l =request.getParameter("language");
        String header =request.getHeader("Accept-Language");
        Locale locale=null;

        if (!StringUtils.isEmpty(l)){
//            _分割    由 login_zh_CN 决定
            String[] split =l.split("_");
            locale =new Locale(split[0],split[1]);
        }else {
            String[] splits =header.split(",");
            String[] split =splits[0].split("-");
            locale =new Locale(split[0],split[1]);
        }
        return locale;
    }
    //覆盖系统配置
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResovel();
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

5.在html页面实现功能,并启动mian方法所在类

<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">
    <title>用户登录界面</title>
    <link th:href="@{/login/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/login/css/signin.css}" rel="stylesheet">
</head>
<!--保持正常的框架,以便于连接-->
<body class="text-center">
<from class="form-signin">
    <img class="mb-4" th:src="@{/login/img/login.jpg}" width="72 " height="72">
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">请登录</h1>
    <input type="text" class="form-control"
           th:placeholder="#{login.username}" required="" autofocus="">
    <input type="text" class="form-control"
           th:placeholder="#{login.password}" required="" >
<div class="checkbox mb-3">
    <label>
        <input type="checkbox" value="remember-me">[[#{login.rememberme}]]
<!--        同login.properties rememberme-->
    </label>
</div>
    <botton class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</botton>
    <p class="mt-5 mb-3 text-muted " >@<span th:text="${currentYear}">2000</span>-
        <span th:text="${currentYear}+1">2004</span> </p>
    <a class="btn btn-sm "th:href="@{/toLoginPage(1='zh_CN')}">中文</a>
    <a class="btn btn-sm "th:href="@{/toLoginPage(1='en_US')}">English</a>
<!--    <a class="btn btn-sm "th:href="@{/toLoginPage(language='zh_CN')}">中文</a>-->
<!--    <a class="btn btn-sm "th:href="@{/toLoginPage(language='en_US')}">English</a>-->
</from>
</body>

</html>

即可实现Thymeleaf 配置的国际化页面

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值