SpringBoot整合国际化功能
SpringBoot 功能 整合 国际化
2023-09-11 14:18:14 时间
(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)测试
相关文章
- SpringBoot系列之注解@Component VS @Bean(四)
- springboot下实现邮件发送功能
- 让SpringBoot不需要Controller、Service、DAO、Mapper
- 简单性能测试:springboot-2.x vs actix-web-4.x benchmark
- springboot项目引入了doris数据,建表模式如何选?
- SpringBoot开发Restful风格的接口实现CRUD功能
- SpringBoot---WebMvcConfigurer详解
- 《SpringBoot篇》01.Springboot超详细入门(基础篇)
- 狂神说springboot整合富文本编辑器editor.md
- SpringBoot整合Druid(阿里巴巴)数据源
- SpringBoot整合日志
- springboot中@Configuration的用法
- vue实战入门后台篇二:springboot+mybatis实现网站后台-登录鉴权功能实现
- SpringBoot+Mybaits搭建通用管理系统实例十二:系统操作日志功能实现
- SpringBoot+Mybaits搭建通用管理系统实例七:访问接口Api层功能实现
- SpringBoot+Mybaits搭建通用管理系统实例十一:数据缓存功能实现
- SpringBoot 整合 SpringMvc 流程以及原理
- SpringBoot嵌入pentaho-kettle工具实现数据trans转换和job任务手动执行
- springboot 发布tomcat(zip包)
- SpringBoot项目启动遇到的问题记录
- Springboot 实现WebMvcConfigurer接口来拓展SpringMvc的功能