1.在ruoyi-common模块下添加子模块ruoyi-common-i18n

2.先看下国际化包的pom.xml

因为后续考虑会将国际化语言配置文件内容加载到redis中去,所以此处预留引入了redis相关的依赖,

注意这是属于子模块,不作为单独项目运行,独立运行的模块都是基于SpringMVC相关的拦截来处理。如自定义验证表演,和自定义标签,都需要响应的处理。

所以这里会引入spring-boot-starter-web依赖,虽然不会重复依赖,为了使结构清晰,在引用国际化模块时,我会排除国际化包引入的spring-boot-starter-web依赖。后续会截图说明。

3.工程目录结构

4.因为ruoyi-springCloud版本是基于Nacos读取配置文件,所以在在配置类编写时,读取国际化配置文件,可以相对比较灵活。以下是介绍下比较关键的几个类

4.1 I18nUtil,国际化工具类,主要用于根据key和语言进行国际化语言转换

package com.ruoyi.common.i18n.utils;import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TokenUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.Locale;@Component
public class I18nUtil {/*** 国际化语言前缀*/private static String localeAttributeName = Constants.LOCALE_SESSION_ATTRIBUTE_NAME;private static MessageSource messageSource;private static RedisService redisService;/*** 构建读取配置对象用于注入* @param messageSource*/public I18nUtil(MessageSource messageSource,RedisService redisService) {I18nUtil.messageSource = messageSource;I18nUtil.redisService=redisService;}/*** 获取配置文件中key所对应的国际化语言* @param msgKey* @return*/public static String getMessage(String msgKey) {return getMessage(msgKey, null);}/*** 获取配置文件中key所对应的国际化信息,存在占位符通过args数组进行数据填充* @param msgKey* @param args* @return*/public static String getMessage(String msgKey,Object[] args) {try {return messageSource.getMessage(msgKey, args, getLocaleFromRedis());} catch (Exception e) {return msgKey;}}/*** 从redis中获取当前语言* @return*/public static Locale getLocaleFromRedis(){String token ="";try {token = TokenUtil.getToken(getRequest());} catch (Exception e) {return LocaleContextHolder.getLocale();}String lan=redisService.getCacheObject(localeAttributeName + token);Locale locale=null;if(StringUtils.isNotBlank(lan)){String[] lans=lan.split("_");locale = new Locale(lans[0], lans[1]);}else{locale=LocaleContextHolder.getLocale();//获取浏览器请求}return locale;}/*** 获取请求属性* @return*/public static ServletRequestAttributes getRequestAttributes(){RequestAttributes attributes = RequestContextHolder.getRequestAttributes();return (ServletRequestAttributes) attributes;}/*** 获取请求request* @return*/public static HttpServletRequest getRequest(){return getRequestAttributes().getRequest();}}

4.2 国际化包配置类

package com.ruoyi.common.i18n.configure;import com.ruoyi.common.i18n.interceptor.I18nLocaleChangeInterceptor;
import com.ruoyi.common.i18n.messagesource.RedisMessageSource;
import com.ruoyi.common.i18n.resolver.RedisLocaleResolver;
import com.ruoyi.common.i18n.utils.I18nUtil;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;import java.util.List;
import java.util.Locale;/*** 国际化配置*/
@Configuration
@EnableCaching
public class LocaleConfig{/*** 配置需要进行加载的配置文件*/@Value("#{'${i18n_msg.baseName}'.split(',')}")private List<String> basenames;@Autowiredprivate RedisService redisService;/*** 添加注入配置** @return*/@Beanpublic I18nUtil i18nUtil() {return new I18nUtil(messageSource(),redisService);}/*** 设置配置国际化配置文件所在位置** @return*/@Beanpublic RedisMessageSource messageSource() {// Locale.setDefault(Locale.CHINESE);RedisMessageSource source = new RedisMessageSource();source.setBasenames(basenames.toArray(new String[basenames.size()]));// name of the resource bundle// source.setUseCodeAsDefaultMessage(true);source.setDefaultEncoding("UTF-8");return source;}/*** 验证标签** @return* @throws Exception*/@Beanpublic Validator getValidator() {LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();validator.setValidationMessageSource(messageSource());return validator;}/*** 默认解析器 其中locale表示默认语言*/@Bean(name = "langLocaleResolver")public LocaleResolver localeResolver() {RedisLocaleResolver localeResolver = new RedisLocaleResolver();localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);return localeResolver;}/* ** 默认拦截器 其中lang表示切换语言的参数名*/@Beanpublic WebMvcConfigurer localeInterceptor() {return new WebMvcConfigurer() {@Overridepublic void addInterceptors(InterceptorRegistry registry) {I18nLocaleChangeInterceptor localeInterceptor = new I18nLocaleChangeInterceptor(localeResolver());localeInterceptor.setParamName("lang");registry.addInterceptor(localeInterceptor);}};}}

其实比较关键就是这两个,所以重点代码都贴出来哈哈。

5.system模块引用pom说明,将18n国际化依赖的包排除掉,只是为了看起来更合理点,也可以不排除

具体调用示例,正常转换只需要调用I18nUtil.getMessage(key)即可。重点说下自定义校验标签,若@NotBlank @Length,因为在国际化包中配置类中在Validator中将国际化注入进去

因为ruoyi-system模块中主要校验都是通过GlobalExceptionHandler进行处理,此处讲述如何配置和处理

这里配置的时候,可以加上{}也可以不用,因为加了{}就会被自动解析成国际化后的语音,灵活性比较没那么高,我这里选择的是直接写key在上面

然后在全局异常处,统一处理如下

自此独立模块的国际化模块已经差不多完毕了。

以下是踩到的坑,

1.gateway模块时基于springwebFlux实现的所以在gateway中国际化切记不要饮用国际化包,直接在gateway模块中单独再实现一遍就好

2.语音切换的时候再i18n模块中的拦截器中,语音切换时,记得重写语言设置的类默认的那个类设置国际化语音时是直接抛出异常的

3.因为我这边已经加入了Redis支持。后续完善时再来补文章

记录基于若依SpringCloud版本的I8n国际化子模块开发相关推荐

  1. gradle 指定springcloud 版本_SpringCloud微服务架构开发实战:实现服务注册与发现

    实现服务的注册与发现 在前面分别用Eureka Server和Eureka Client来搭建了一台注册服务器,以及多个Eureka Client客户端.Eureka Client在启动后,就会将自己 ...

  2. SpringCloud 微服务工具集 SpringCloud 版本: Hoxton SR6

    SpringCloud 微服务工具集v1.1 SpringCloud版本: Hoxton SR6 SpringBoot版本: 2.2.x --2.3.x 1.什么是微服务 官网: https://ww ...

  3. SpringCloud教程- 服务消费者(Feign)(SpringCloud版本Finchley)

    文章目录 一.Feign简介 二. 环境准备 三.创建基于Feign服务 定义启动类 pom文件配置 配置文件application.yml 定义一个feign接口 定义一个controller 前言 ...

  4. SpringCloud教程-服务的注册与发现Eureka(SpringCloud版本Finchley)

    文章目录 SpringCloud简介 创建注册中心(基于Eureka) 创建maven工程 创建maven子工程 eureka-server服务端pom文件 eureka-server服务端配置文件 ...

  5. SpringCloud版本Hoxton SR5 --- 第三讲:Ribbon 、Ribbon与Feign配合使用

    传送门:SpringCloud版本Hoxton SR5 --- 第一讲:认识 先看Ribbon.Fegin可以完成的功能,或者说他在项目中的定位和作用. 上篇文章主要讲:功能和作用都是用大白话,主要是 ...

  6. SpringCloud版本Hoxton SR5 --- 第一讲:认识

    什么是SpringCloud ?我用好理解的方式作比喻: 这里我们拿京东的网站做解释,京东不一定这么搞得,但是思想是一致的.首先SpringCloud一种微服务架构的实现:首先京东支持14亿人访问,这 ...

  7. SpringCloud版本Hoxton SR5 --- 第八讲:Sleuth 分布式链路跟踪 整合Zipkin + Elasticsearch持久化

    传送门:SpringCloud版本Hoxton SR5 --- 第一讲:认识 先看Sleuth.Zipkin.Elasticsearch 可以完成的功能,或者说他在项目中的定位和作用. Sleuth比 ...

  8. 基于yolov5的Android版本目标检测app开发(部署安卓手机)

    基于yolov5的Android版本目标检测app开发(部署安卓手机) 0.项目开发需求 (1)开发app部署到安卓手机 (2)支持VOC数据集上所有的目标检测 1.开发环境搭建 windows10+ ...

  9. 基于Cobbler实现多版本系统批量部署

    前言 运维自动化在生产环境中占据着举足轻重的地位,尤其是面对几百台,几千台甚至几万台的服务器时,仅仅是安装操作系统,如果不通过自动化来完成,根本是不可想象的.记得前面我们探究了基于PXE实现系统全自动 ...

最新文章

  1. 深度学习系列作业1----by 吴恩达
  2. JS如何调用CSS样式表
  3. 由Android 65K方法数限制引发的思考
  4. Spaly_Tree 模版
  5. BLE-NRF51822教程1-常用概念
  6. wordList01
  7. html页面 wordpress,WordPress纯代码实现前端页面HTML完美压缩
  8. 二分查找:在有序数组中搜索大于等于x的数的最小下标
  9. WAMP的多站点配置
  10. 23.6. Functions
  11. 网络操作系统和分布式系统区别简介
  12. AXD 查看register笔记
  13. ubuntu14.04彻底卸载ibus安装fcitx拼音输入法
  14. QuickChm出现的“不支持此接口”错误解决
  15. 色差仪确保番茄酱色彩一致性
  16. python代码示例大全 下载-python基础代码大全
  17. 外盘资管分仓软件(如智星、金管家、信管家等)和内盘(融行等)分仓软件的区别?
  18. JM中的一些问题总结
  19. BI数据分析从业者从零开始学习财务知识?有哪些入门书籍推荐
  20. 【实践】信息流推荐算法实践 深入

热门文章

  1. 【Linux】用户信息
  2. java计算机毕业设计智能化车辆管理综合信息平台源码+mysql数据库+系统+部署+lw文档
  3. 微信小程序——修改field输入框内文字颜色和背景图透明度调整
  4. 花旗私人银行发布2019年展望报告
  5. 恒玄BES调试笔记-BES2500如何区分左右耳
  6. 2023年,想要靠做软件测试获得高薪,我还有机会吗?
  7. 体验了一下火爆全球的 ChatGPT,我震惊了
  8. x230可以装win10吗_联想x230i笔记本U盘安装win10系统的操作教程
  9. 使用UIImagePickerController从IPhone照片库或照相机获取图像
  10. 合并排序merge sort