如何构建Spring Restful支持json数据多语言?

时间:2016-08-18 06:57:34

标签: java json spring rest

{{1}}

1 个答案:

答案 0 :(得分:0)

这似乎有您要查找的详细信息:http://www.ekiras.com/2016/01/springboot-internalization-and-i18n.html

配置:

@Bean
public LocaleResolver localeResolver() {
 SessionLocaleResolver slr = new SessionLocaleResolver();
 slr.setDefaultLocale(Locale.US); // Set default Locale as US
 return slr;
}

@Bean
public ResourceBundleMessageSource messageSource() {
 ResourceBundleMessageSource source = new ResourceBundleMessageSource();
 source.setBasenames("i18n/messages");  // name of the resource bundle 
 source.setUseCodeAsDefaultMessage(true);
 return source;
}

Controller(注意参数中的Locale):

@RestController
public class HomeController {

   @Autowired
   private MessageSource messageSource;

   @RequestMapping("/msg")
   public String msg(@RequestHeader("Accept-Language") Locale locale){
      return messageSource.getMessage("msg",null,locale);
   }
}