Spring Boot 国际化设置

1、对你需要国际化的页面,抽取国际化的信息,编写配置文件,这里我以简单的登录页做国际化为范例。

1)在resource下新建目录,i18n

2)根据需要国际化的页面,新建porperties配置文件,

en:语言

US:国家代码

3)书写配置,进入任意国际化配置文件中,

点到Resource Bundle视图,点上面的+号,配置需要国际化的属性值,比如我要配置用户名,如下:

第一个是默认显示。

配置完后如下:

2、配置指定读取国际化配置文件的路径(默认会去根路径下寻找),在application.properties中配置 spring.messages.basename读取路径

3、在页面用“#{}”获取国际化配置文件中的值

<!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 th:href="@{asserts/css/bootstrap.min.css}" rel="stylesheet">
      <!-- Custom styles for this template -->
      <link th:href="@{asserts/css/signin.css}" rel="stylesheet">
   </head>

   <body class="text-center">
      <form class="form-signin" action="dashboard.html">
         <img class="mb-4" 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" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
         <label class="sr-only" th:text="#{login.password}">Password</label>
         <input type="password" class="form-control" th:placeholder="#{login.password}" 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>
         <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>

4、配置区域信息解析器,点击切换中英文效果,参数方式为 语言_国家代码方式

th:href="@{/login.html(l='zh_CN')}";

package com.boot.component;

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;

/**
 * @ClassName MyLocaleResolver
 * @Description TODO 通过实现LocaleResolver中
 * @Author shanzz
 * @Date 2019/1/3 11:15
 * @Version 1.0
 **/
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
       String l = httpServletRequest.getParameter("l");
       Locale locale = Locale.getDefault();//默认语言
       if(!StringUtils.isEmpty(l)){
            String[] split =l.split("_");
           locale = new Locale(split[0],split[1]);//split[0]语言,
       }
        return locale;
    }

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

    }
}

 5、将MyLocaleResolver自定义的区域信息解析器添加在容器中,让系统去使用我们自己配置的解析器。

package com.boot.config;

import com.boot.component.MyLocaleResolver;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName WebMvcConfig
 * @Description TODO
 * @Author shanzz
 * @Date 2019/1/2 17:16
 * @Version 1.0
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
    }

    //添加区域信息解析器
    @Bean
    public LocaleResolver  localeResolver(){
        return new MyLocaleResolver();
    }
}

6、访问页面,查看结果

参考:尚硅谷SpringBoot(全集)系列视频,该视频SpringBoot版本为1.x相对于目前2.x有些地方有所不同,不过学习中可以举一反三,通过发现不同去学习总结,文章中有不足之处请予以指正。

猜你喜欢

转载自blog.csdn.net/szzzgyn/article/details/85695327