• SpringBoot整合国际化功能


    (1)、编写国际化配置文件

      在resources下新建i18n文件夹,并新建以下文件

      ①index.properties

       1 username=username

      ②index_en_US.properties

       1 username=username 

      ③index_zh_CN.properties

       1 username=用户名 

    (2)、使用ResourceBundleMessageSource管理国际化资源文件

    *SpringBoot已经自动配置了管理国际化资源文件的组件

    (3)在配置文件中指定国际化资源文件的文件夹及基础文件

     1 #指定国际化资源文件的文件夹及基础文件 2 spring.messages.basename=i18n/index 

    (4)* 编写自定义的Locale区域解析器

     1 package cn.coreqi.config;
     2 
     3 import org.springframework.util.StringUtils;
     4 import org.springframework.web.servlet.LocaleResolver;
     5 
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 import java.util.Locale;
     9 
    10 /**
    11  * SpringBoot默认的Locale解析器是根据请求头的区域信息进行解析的(浏览器语言)
    12  * 使用自定义的Locale解析器对url的区域信息进行解析达到点击切换区域效果
    13  * 一旦我们自定义的区域解析器注册到Spring容器中,则SpringBoot提供的将不自动注册
    14  */
    15 public class MyLocaleResolver implements LocaleResolver {
    16     @Override
    17     public Locale resolveLocale(HttpServletRequest httpServletRequest) {
    18         String l = httpServletRequest.getParameter("l");
    19         if(!StringUtils.isEmpty((l))){
    20             String [] s = l.split("_");
    21             return new Locale(s[0],s[1]);
    22         }
    23         return Locale.getDefault();
    24     }
    25 
    26     @Override
    27     public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    28 
    29     }
    30 }

    (5)注册我们自定义的区域解析器

     1 package cn.coreqi.config;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.web.servlet.LocaleResolver;
     6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
     7 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
     8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
     9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    10 
    11 /**
    12  * 扩展SpringMVC
    13  * SpringBoot2使用的Spring5,因此将WebMvcConfigurerAdapter改为WebMvcConfigurer
    14  * 使用WebMvcConfigurer扩展SpringMVC好处既保留了SpringBoot的自动配置,又能用到我们自己的配置
    15  */
    16 //@EnableWebMvc //如果我们需要全面接管SpringBoot中的SpringMVC配置则开启此注解,
    17                 //开启后,SpringMVC的自动配置将会失效。
    18 @Configuration
    19 public class WebConfig implements WebMvcConfigurer {
    20     @Override
    21     public void addViewControllers(ViewControllerRegistry registry) {
    22         //设置对“/”的请求映射到index
    23         //如果没有数据返回到页面,没有必要用控制器方法对请求进行映射
    24         registry.addViewController("/").setViewName("index");
    25     }
    26 
    27     //注册我们自定义的区域解析器,一旦将我们的区域解析器注册到Spring容器中则SpingBoot
    28     //默认提供的区域解析器将不会自动注册
    29     @Bean
    30     public LocaleResolver localeResolver(){
    31         return new MyLocaleResolver();
    32     }
    33 }

    (6)视图中引用国际化内容

     1 <!DOCTYPE html>
     2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Index首页</title>
     6 </head>
     7 <body>
     8 <h1 th:text="#{username}"></h1>
     9 </body>
    10 </html>

    (7)测试

  • 相关阅读:
    大数据处理系列之(二)系统过载保护
    大数据处理系列之(一)Java线程池使用
    js实现递归菜单无限层
    treeTable实现排序
    spring-dm 一个简单的实例
    Equinox OSGi服务器应用程序的配置步骤 (支持JSP页面)
    Spring DM 2.0 环境配置 解决Log4j问题
    Spring.DM web开发环境搭建
    STL容器的排序
    排序例子
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10324662.html
Copyright © 2020-2023  润新知