update 使用全局国际化过滤器 GlobalI18nFilter 优化 Gateway 国际化处理
parent
e1c789b947
commit
d3ded4162e
@ -1,21 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.ruoyi.gateway.filter;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
import org.springframework.context.i18n.SimpleLocaleContext;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局国际化处理
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class GlobalI18nFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
LocaleContextHolder.setLocaleContext(new SimpleLocaleContext(locale), true);
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,32 +0,0 @@
|
|||||||
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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue