From 7726fa9773c7fcf35884378d08e8d88f22354b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Mon, 21 Feb 2022 19:36:27 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20gateway=20webflux=20?= =?UTF-8?q?=E5=9B=BD=E9=99=85=E5=8C=96=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/exception/CaptchaException.java | 4 ++- .../main/resources/i18n/messages.properties | 0 .../resources/i18n/messages_en_US.properties | 0 .../resources/i18n/messages_zh_CN.properties | 0 .../com/ruoyi/gateway/config/I18nConfig.java | 21 ++++++++++++ .../gateway/resolver/I18nLocaleResolver.java | 32 +++++++++++++++++++ 6 files changed, 56 insertions(+), 1 deletion(-) rename ruoyi-common/{ruoyi-common-web => ruoyi-common-core}/src/main/resources/i18n/messages.properties (100%) rename ruoyi-common/{ruoyi-common-web => ruoyi-common-core}/src/main/resources/i18n/messages_en_US.properties (100%) rename ruoyi-common/{ruoyi-common-web => ruoyi-common-core}/src/main/resources/i18n/messages_zh_CN.properties (100%) create mode 100644 ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/I18nConfig.java create mode 100644 ruoyi-gateway/src/main/java/com/ruoyi/gateway/resolver/I18nLocaleResolver.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java index 9cca4111..dbe832c0 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java +++ b/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() { diff --git a/ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages.properties b/ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages.properties similarity index 100% rename from ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages.properties rename to ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages.properties diff --git a/ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_en_US.properties b/ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_en_US.properties similarity index 100% rename from ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_en_US.properties rename to ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_en_US.properties diff --git a/ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_zh_CN.properties b/ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_zh_CN.properties similarity index 100% rename from ruoyi-common/ruoyi-common-web/src/main/resources/i18n/messages_zh_CN.properties rename to ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_zh_CN.properties diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/I18nConfig.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/I18nConfig.java new file mode 100644 index 00000000..032327c1 --- /dev/null +++ b/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(); + } + +} diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/resolver/I18nLocaleResolver.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/resolver/I18nLocaleResolver.java new file mode 100644 index 00000000..ec5dd280 --- /dev/null +++ b/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) { + + } +}