【发布时间】:2020-10-11 09:36:21
【问题描述】:
我想获取当前语言环境,但上下文总是返回默认语言环境。它适用于 MVC,但不适用于 WebFlux。
感谢您的帮助!
package co.example.demo.controller;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Locale;
@RestController
@RequestMapping("/hello")
public class HelloController {
private final MessageSource messageSource;
public HelloController(MessageSource messageSource) {
this.messageSource = messageSource;
}
@GetMapping
public String hello() {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage("hello", null, locale);
}
}
【问题讨论】:
-
您期望的语言环境是什么以及它返回的内容是什么?
-
@DhawalKapil 我有
message.properties和messages_fr.properties但 webflux 总是返回默认语言环境en而不是fr。用mvc没问题。 -
我认为这取决于当前的 JVM 语言环境。我不确定是否有任何与之相关的 Webflux/Web。
标签: java spring-boot locale spring-webflux