公共Mono模块

本文介绍了如何通过公共Mono模块使非继承Mono的类具备开启协程和Update更新的能力,旨在实现对Update逻辑的统一管理。

作用:让没有继承Mono的类可以开启协程,可以Update更新,统一管理Update

public class MonoController : MonoBehaviour {
    private event UnityAction updateEvent;

    void Start() {
        DontDestroyOnLoad(this.gameObject);
    }

    void Update() {
        if (updateEvent != null)
            updateEvent();
    }

    // 给外部提供的添加帧更新事件的函数
    public void AddUpdateListener(UnityAction fun) {
        updateEvent += fun;
    }

    // 给外部提供的删除帧更新事件的函数
    public void RemoveUpdateListener(UnityAction fun) {
        updateEvent -= fun;
    }
}


// -----------------------------------------------------
public class MonoMgr : BaseManager<MonoMgr> {
    private MonoController controller;

    public MonoMgr() {
        // 保证了MonoController对象的唯一性
        GameObject obj = new GameObject("MonoController");
        controller = obj.AddComponent<MonoController>();
    }    

    public void AddUpdateListener(UnityAction fun) {
        controller.AddUpdateLis
<think> 我们计划在ruoyi-cloud中新增一个国际化公共模块,命名为`ruoyi-i18n`,该模块将负责管理多语言资源文件、提供国际化工具类,并集成到其他微服务中。以下是具体步骤: ### 1. 创建国际化公共模块 在`ruoyi-modules`目录下新建模块`ruoyi-i18n`,参考其他模块的结构(如`ruoyi-job`)[^2]: - 复制`ruoyi-job`的`pom.xml`依赖、启动类模板、资源文件(banner.txt, bootstrap.yml, logback.xml)到新模块 - 修改`pom.xml`中的`artifactId`为`ruoyi-i18n`,并添加必要的依赖: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 若依公共核心模块 --> <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-common-core</artifactId> <version>${ruoyi.version}</version> </dependency> </dependencies> ``` ### 2. 配置多语言资源文件 在`ruoyi-i18n`的`resources`目录下创建: ``` resources/ └── i18n/ ├── messages.properties ├── messages_zh_CN.properties └── messages_en_US.properties ``` 示例内容: - `messages.properties` (默认): ```properties operation.success=Operation successful ``` - `messages_zh_CN.properties`: ```properties operation.success=操作成功 ``` - `messages_en_US.properties`: ```properties operation.success=Operation successful ``` ### 3. 编写国际化工具类 在`ruoyi-i18n`模块中创建工具类`I18nUtils`,用于从Spring的`MessageSource`获取消息: ```java package com.ruoyi.i18n.utils; import com.ruoyi.common.utils.spring.SpringUtils; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; public class I18nUtils { private static final MessageSource messageSource = SpringUtils.getBean(MessageSource.class); public static String get(String key, Object... args) { return messageSource.getMessage(key, args, LocaleContextHolder.getLocale()); } } ``` ### 4. 配置自动装载 在`ruoyi-i18n`的`src/main/resources/META-INF`下创建`spring.factories`,使配置自动生效: ```properties org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ruoyi.i18n.config.I18nAutoConfiguration ``` 创建自动配置类`I18nAutoConfiguration`: ```java package com.ruoyi.i18n.config; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ResourceBundleMessageSource; @AutoConfiguration public class I18nAutoConfiguration { @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("i18n/messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } } ``` ### 5. 其他微服务引入国际化模块 在需要国际化的微服务(如`ruoyi-system`)的`pom.xml`中添加依赖: ```xml <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-i18n</artifactId> <version>${ruoyi.version}</version> </dependency> ``` ### 6. 网关传递语言参数 修改网关模块`ruoyi-gateway`,在全局过滤器中解析语言参数并放入请求头: ```java // 在全局过滤器GatewayFilter中增加: String lang = exchange.getRequest().getQueryParams().getFirst("lang"); if (lang != null) { exchange = exchange.mutate().request( exchange.getRequest().mutate() .header("Accept-Language", lang) .build() ).build(); } ``` ### 7. 验证国际化功能 在业务代码中使用: ```java @RestController public class TestController { @GetMapping("/test") public String test() { return I18nUtils.get("operation.success"); } } ``` 通过URL参数切换语言:`http://localhost:8080/test?lang=en_US` 应返回英文,`?lang=zh_CN`返回中文。 ### 注意事项 1. **资源文件编码**:确保所有`.properties`文件使用UTF-8编码[^1]。 2. **模块依赖**:确保`ruoyi-i18n`模块被其他微服务正确引入。 3. **语言解析器**:各微服务需配置`LocaleResolver`和拦截器(参考问题中的初始回答)[^3]。 --- ### § 相关问题 § 1. 如何动态更新国际化资源而不重启服务? 2. 微服务架构下如何统一管理多语言资源文件? 3. RuoYi-Cloud 中如何实现前后端分离的国际化? 4. 国际化功能对系统性能有何影响? 5. 如何扩展支持数据库存储的多语言配置? </think> 在 RuoYi-Cloud Spring Boot 3 中新增国际化公共模块的完整实现步骤如下: ### 一、创建国际化公共模块 在 `ruoyi-modules` 目录下新建模块 `ruoyi-i18n`: ```bash ruoyi-modules/ └── ruoyi-i18n/ ├── src/main/java/com/ruoyi/i18n └── pom.xml ``` 模块 `pom.xml` 配置(参考 ruoyi-job 模块结构[^2]): ```xml <dependencies> <!-- 公共核心依赖 --> <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-common-core</artifactId> </dependency> <!-- Spring Boot 3 基础依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 国际化支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> ``` ### 二、配置资源文件 在 `resources` 目录创建多语言文件: ``` resources/ └── i18n/ ├── messages.properties # 默认 ├── messages_zh_CN.properties # 中文 └── messages_en_US.properties # 英文 ``` 示例内容 (`messages_zh_CN.properties`): ```properties user.login.success=登录成功 system.error=系统错误 operation.success=操作成功 ``` ### 三、创建核心工具类 `I18nUtils.java`: ```java package com.ruoyi.i18n.utils; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import com.ruoyi.common.utils.spring.SpringUtils; public class I18nUtils { private static final MessageSource messageSource = SpringUtils.getBean(MessageSource.class); public static String get(String key, Object... params) { return messageSource.getMessage(key, params, LocaleContextHolder.getLocale()); } } ``` ### 四、配置自动装载 `I18nAutoConfiguration.java`: ```java package com.ruoyi.i18n.config; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ResourceBundleMessageSource; @AutoConfiguration public class I18nAutoConfiguration { @Bean public MessageSource messageSource() { ResourceBundleMessageSource source = new ResourceBundleMessageSource(); source.setBasename("i18n/messages"); // 资源文件路径 source.setDefaultEncoding("UTF-8"); // 编码格式 source.setUseCodeAsDefaultMessage(true); return source; } } ``` ### 五、在 `ruoyi-gateway` 添加语言拦截 修改网关过滤器传递语言参数: ```java public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 从请求头获取语言参数 String lang = exchange.getRequest().getHeaders().getFirst("Accept-Language"); if (lang == null) { lang = "zh_CN"; // 默认中文 } // 添加到下游请求 ServerHttpRequest mutatedRequest = exchange.getRequest().mutate() .header("Accept-Language", lang) .build(); return chain.filter(exchange.mutate().request(mutatedRequest).build()); } ``` ### 六、其他微服务集成 在需要国际化的微服务(如 `ruoyi-system`)中添加依赖: ```xml <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-i18n</artifactId> <version>${ruoyi.version}</version> </dependency> ``` ### 七、使用示例 在业务代码中调用: ```java @RestController public class LoginController { @PostMapping("/login") public AjaxResult login() { // 返回 "登录成功" 或 "Login successful" return AjaxResult.success(I18nUtils.get("user.login.success")); } } ``` ### 八、Nacos 配置更新 在 `ruoyi-i18n` 的 `bootstrap.yml` 中添加: ```yaml spring: messages: basename: i18n/messages encoding: UTF-8 cache-duration: 3600s # 缓存1小时 ``` ### 九、验证方式 1. 通过 Postman 测试带语言头的请求: ```http GET /system/config/list Accept-Language: en_US ``` 2. 检查响应消息是否为英文 3. 切换语言头为 `zh_CN` 验证中文显示 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值