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.
–
–
–
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.