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)测试
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10324662.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
分类:
Spring Boot
如果文章内容对您有所帮助,欢迎赞赏.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!