Просмотр исходного кода

fix 修复 gateway webflux 国际化不生效问题

疯狂的狮子li 4 лет назад
Родитель
Сommit
7726fa9773

+ 3 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java

@@ -1,11 +1,13 @@
 package com.ruoyi.common.core.exception;
 
+import com.ruoyi.common.core.exception.user.UserException;
+
 /**
  * 验证码错误异常类
  *
  * @author Lion Li
  */
-public class CaptchaException extends RuntimeException {
+public class CaptchaException extends UserException {
     private static final long serialVersionUID = 1L;
 
     public CaptchaException() {

+ 0 - 0
ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages.properties → ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages.properties


+ 0 - 0
ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_en_US.properties → ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_en_US.properties


+ 0 - 0
ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_zh_CN.properties → ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_zh_CN.properties


+ 21 - 0
ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/I18nConfig.java

@@ -0,0 +1,21 @@
+package com.ruoyi.gateway.config;
+
+import com.ruoyi.gateway.resolver.I18nLocaleResolver;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
+import org.springframework.web.server.i18n.LocaleContextResolver;
+
+/**
+ * webflux 国际化解析器
+ *
+ * @author Lion Li
+ */
+@Configuration
+public class I18nConfig extends DelegatingWebFluxConfiguration {
+
+    @Override
+    protected LocaleContextResolver createLocaleContextResolver() {
+        return new I18nLocaleResolver();
+    }
+
+}

+ 32 - 0
ruoyi-gateway/src/main/java/com/ruoyi/gateway/resolver/I18nLocaleResolver.java

@@ -0,0 +1,32 @@
+package com.ruoyi.gateway.resolver;
+
+import org.springframework.context.i18n.LocaleContext;
+import org.springframework.context.i18n.SimpleLocaleContext;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.i18n.LocaleContextResolver;
+
+import java.util.Locale;
+
+/**
+ * 获取请求头国际化信息
+ *
+ * @author Lion Li
+ */
+public class I18nLocaleResolver implements LocaleContextResolver {
+
+    @Override
+    public LocaleContext resolveLocaleContext(ServerWebExchange exchange) {
+        String language = exchange.getRequest().getHeaders().getFirst("content-language");
+        Locale locale = Locale.getDefault();
+        if (language != null && language.length() > 0) {
+            String[] split = language.split("_");
+            locale = new Locale(split[0], split[1]);
+        }
+        return new SimpleLocaleContext(locale);
+    }
+
+    @Override
+    public void setLocaleContext(ServerWebExchange exchange, LocaleContext localeContext) {
+
+    }
+}