如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n? [英] How to configure i18n in Spring boot 2 + Webflux + Thymeleaf?

查看:23
本文介绍了如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了一个基于 Spring boot 2 + Webflux 的新项目.在升级 spring boot 版本并将 spring-boot-starter-web 替换为 spring-boot-starter-webflux 类如

I just start a new project based on Spring boot 2 + Webflux. On upgrading version of spring boot and replace spring-boot-starter-web with spring-boot-starter-webflux classes like

  • WebMvcConfigurerAdapter
  • LocaleResolver
  • LocaleChangeInterceptor
  • WebMvcConfigurerAdapter
  • LocaleResolver
  • LocaleChangeInterceptor

不见了.现在如何配置 defaultLocale 和拦截器来更改语言?

are missing. How now can I configure defaultLocale, and interceptor to change the language?

推荐答案

只需添加一个 WebFilter 即可根据查询参数的值设置 Accept-Language 标头.以下示例从 http://localhost:8080/examples?language=es 等 URI 上的 language 查询参数获取语言:

Just add a WebFilter that sets the Accept-Language header from the value of a query parameter. The following example gets the language from the language query parameter on URIs like http://localhost:8080/examples?language=es:

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
import reactor.core.publisher.Mono;

import static org.springframework.util.StringUtils.isEmpty;

@Component
public class LanguageQueryParameterWebFilter implements WebFilter {

    private final ApplicationContext applicationContext;

    private HttpWebHandlerAdapter httpWebHandlerAdapter;

    public LanguageQueryParameterWebFilter(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void loadHttpHandler() {
        this.httpWebHandlerAdapter = applicationContext.getBean(HttpWebHandlerAdapter.class);
    }

    @Override
    public Mono<Void> filter(final ServerWebExchange exchange, final WebFilterChain chain) {
        final ServerHttpRequest request = exchange.getRequest();
        final MultiValueMap<String, String> queryParams = request.getQueryParams();
        final String languageValue = queryParams.getFirst("language");

        final ServerWebExchange localizedExchange = getServerWebExchange(languageValue, exchange);
        return chain.filter(localizedExchange);
    }

    private ServerWebExchange getServerWebExchange(final String languageValue, final ServerWebExchange exchange) {
        return isEmpty(languageValue)
                ? exchange
                : getLocalizedServerWebExchange(languageValue, exchange);
    }

    private ServerWebExchange getLocalizedServerWebExchange(final String languageValue, final ServerWebExchange exchange) {
        final ServerHttpRequest httpRequest = exchange.getRequest()
                .mutate()
                .headers(httpHeaders -> httpHeaders.set("Accept-Language", languageValue))
                .build();

        return new DefaultServerWebExchange(httpRequest, exchange.getResponse(),
                httpWebHandlerAdapter.getSessionManager(), httpWebHandlerAdapter.getCodecConfigurer(),
                httpWebHandlerAdapter.getLocaleContextResolver());
    }
}

它使用 @EventListener(ApplicationReadyEvent.class) 以避免循环依赖.

It uses @EventListener(ApplicationReadyEvent.class) in order to avoid cyclic dependencies.

请随意测试并提供有关此 POC 的反馈.

Feel free to test it and provide feedback on this POC.

这篇关于如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆