Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams @Bean public LocaleResolver localeResolver(){ CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setDefaultLocale(new Locale("en", "US")); return localeResolver; @RequestMapping(value = "/{lang}", method = RequestMethod.GET) public String changeSessionLanguage(HttpServletRequest request, HttpServletResponse response, @PathVariable("lang") String lang) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); Locale locale = LocaleContextHolder.getLocale(); if ("zh".equals(lang)) { localeResolver.setLocale(request, response, new Locale("zh", "CN")); } else if ("jp".equals(lang)) { localeResolver.setLocale(request, response, new Locale("ja", "JP")); } else { localeResolver.setLocale(request, response, new Locale("en", "US")); return "redirect:/";

And it works good.

But in Spring security UserDetailsService source, I can't get locale as this:

Locale locale = LocaleContextHolder.getLocale();

It always show my browser language no matter I had changed the locale to what.

How can I get the locale correctly?

Thanks!

UPDATE

I changed code, move LocaleResolver to Configuration class and add RequestContextListener like bellow:

@Configuration
@WebListener
public class Config extends RequestContextListener{
    @Bean
    public RequestContextListener requestContextListener(){
    return new RequestContextListener();
    @Bean
    public LocaleResolver localeResolver(){
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(new Locale("en", "US"));
    return localeResolver;

But it also can't work.

Move the bean definition to a @Configuration annotated classes instead of inside a @Controller. You would also need to register the RequestContextListener to have it take effect (not sure if either the RequestContextListener or the RequestContextFilter is already registered by Spring Boot). – M. Deinum Oct 16, 2017 at 5:56 For starters don't extend RequestContextListener and remove @WebListener, also you might want to add this to your class annotated with @SpringBootAPplication. – M. Deinum Oct 16, 2017 at 8:32 @M.Deinum Thanks your answer, but it also can't work. The locale is always same with browser no matter I changed it to what locale in my Controller. Maybe I must get the locale in other ways? instead of get it use LocaleContextHolder? – fish Oct 17, 2017 at 7:53

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.