目前还没有开箱即用的支持,但是您可以轻松地提出一个自定义组件,将Locale
解析委托给 aLocaleResolver
并将解析调用委托给 a MessageSource
:
public class LocalizedMessageSource {
private final MessageSource messageSource;
private final LocaleResolver localeResolver;
public LocalizedMessageSource(MessageSource messageSource,
LocaleResolver localeResolver) {
Assert.notNull(messageSource);
Assert.notNull(localeResolver);
this.messageSource = messageSource;
this.localeResolver = localeResolver;
}
public String getMessage(MessageSourceResolvable resolvable) {
return messageSource.getMessage(resolvable, getLocale());
public String getMessage(String code, Object[] args) {
return messageSource.getMessage(code, args, getLocale());
getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
return messageSource.getMessage(code, args, defaultMessage, getLocale());
}
private Locale getLocale() {
return localeResolver.resolveLocale(getCurrentRequest());
}
private HttpServletRequest getCurrentRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes()).getRequest();
}
}
如果您将该组件注入您的 Spring MVC 控制器,您可以简单地解析消息,而无需Locale
通过控制器方法签名进行管道传输。我打开了一张JIRA 票,建议在 Spring 代码库中添加增强功能。