Springboot国际化i18n的实现

软件实现国际化,需具备以下两个特征

  1. 对于程序中固定使用的文本元素,例如菜单栏、导航条等中使用的文本元素、或错误提示信息,状态信息等,需要根据来访者的地区和国家,选择不同语言的文本为之服务。
  2. 对于程序动态产生的数据,例如(日期,货币等),软件应能根据当前所在的国家或地区的文化习惯进行显示

步骤

  1. 确定IDEA中File Encodings中都是utf-8的编码方式
  2. 创建不同地区的文件
  3. 在页面中修改中英文的属性
  4. 创建MyLocaleResolver 继承LocaleResolver解析国际化
  5. 在WebMvcConfigurer中使自定义国际化生效

使用IDEA来实现i18n

当点击English的时候,页面显示的是英文页面
在这里插入图片描述
在这里插入图片描述

  1. 要先确定IDEA中File Encodings中都是utf-8的编码方式,不然会出现乱码的状况在这里插入图片描述
  2. 在resorces文件中创建i18n的文件夹,在i18n中创建三个properties文件,分别是login.properties、login_en_US.properties(英文)、login_zh_CN.properties(中文)
    在这里插入图片描述
    在页面中显示的中英文分别有五个
  • 登录按钮
  • 密码
  • 记住我
  • 请登录
  • 用户名
    这五个部分要实现中英转换,在IDEA中有一个Resource Bundle,这个非常方便
    在这里插入图片描述
    login_en_US.properties
    在这里插入图片描述
    login_zh_CN.properties
    在这里插入图片描述
  1. 在页面中修改中英文代表的属性
<!doctype html>
<html lang="en-US" xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta 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="">
    <link rel="icon" th:href="@{/img/favicon.ico}">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
  </head>

  <body class="text-center">
    <form class="form-signin" th:action="@{/user/login}">
      <img class="mb-4" th:src="@{/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>
      <!--如果msg为空则不显示-->
      <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
      <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus>
      <input type="password" name="password" class="form-control" th:placeholder="#{login.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">[[#{login.btn}]]</button>
      <p class="mt-5 mb-3 text-muted">&copy; 2017-2018</p>
      <a class="btn btn-sm" th:href="@{/index.html(language='zh_CH')}">中文</a>
      <a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
    </form>
  </body>
</html>
  1. 创建MyLocaleResolver 继承LocaleResolver解析国际化
package com.an.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//国际化
public class MyLocaleResolver implements LocaleResolver {

    //解析国际化请求
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String language = httpServletRequest.getParameter("language");
        Locale locale = Locale.getDefault();//如果没有设置国际化及使用默认
        //如果请求的连接携带了国际化的参数,先判断是否为空,如果没有将地区和国家分开
        if (!StringUtils.isEmpty(language)){
            String[] split = language.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
  1. 在WebMvcConfigurer中使自定义国际化生效,创建MyWebMvcConfigurer,继承WebMvcConfigurer

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //自定义国际化生效
    @Bean
    public LocaleResolver localeResolver(){
        return new  MyLocaleResolver();
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值