java 在 Spring Controller 中获取语言环境的优雅方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33049674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:07:43  来源:igfitidea点击:

Elegant way to get Locale in Spring Controller

javaspring-mvclocalespring-3

提问by yetimoner

I'm looking for a cleaner way (in Spring 3.2) to get the current locale than explicitly calling LocaleContextHolder.getLocale() at the start of each Controller method. It has to be compatible with Java annotation, as I'm not using the XML config. Here's what I'm doing currently.

我正在寻找一种更简洁的方式(在 Spring 3.2 中)来获取当前语言环境,而不是在每个 Controller 方法开始时显式调用 LocaleContextHolder.getLocale() 。它必须与 Java 注释兼容,因为我没有使用 XML 配置。这就是我目前正在做的事情。

@Controller
public class WifeController {
    @Autowired
    private MessageSource msgSrc;

    @RequestMapping(value = "/wife/mood")
    public String readWife(Model model, @RequestParam("whatImDoing") String iAm) {
        Locale loc = LocaleContextHolder.getLocale();
        if(iAm.equals("playingXbox")) {
            model.addAttribute( "statusTitle", msgSrc.getMessage("mood.angry", null, loc) );
            model.addAttribute( "statusDetail", msgSrc.getMessage("mood.angry.xboxdiatribe", null, loc) );
        }
        return "moodResult";
    }
}

回答by Federico Peralta Schaffner

In Spring 3.2 reference docs, section 17.3.3, Supported method argument types:

Spring 3.2 参考文档中,第 17.3.3 节,支持的方法参数类型:

The following are the supported method arguments:

  • Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest.

    (...)

  • java.util.Localefor the current request locale, determined by the most specific locale resolver available, in effect, the configured LocaleResolver in a Servlet environment.

以下是支持的方法参数:

  • 请求或响应对象(Servlet API)。选择任何特定的请求或响应类型,例如 ServletRequest 或 HttpServletRequest。

    (...)

  • java.util.Locale用于当前请求的语言环境,由可用的最具体的语言环境解析器确定,实际上是 Servlet 环境中配置的 LocaleResolver。

So all you'd need to do is receive an instance of Localeas an argument in every method:

所以你需要做的就是Locale在每个方法中接收一个作为参数的实例:

@RequestMapping(value = "/wife/mood")
public String readWife(Model model, @RequestParam("whatImDoing") String iAm, Locale loc) {
    if (iAm.equals("playingXbox")) {
        model.addAttribute("statusTitle", msgSrc.getMessage("mood.angry", null, loc));
        model.addAttribute("statusDetail", msgSrc.getMessage("mood.angry.xboxdiatribe", null, loc));
    }
    return "moodResult";
}

回答by yglodt

As an alternative, you can also autowire the HttpServletRequest

作为替代方案,您还可以自动装配 HttpServletRequest

@Autowired
private HttpServletRequest request;

and then use request.getLocale()everywhere in your Controller.

然后request.getLocale()在您的控制器中随处使用。