I have a spring application that I want users to be able to change the preferred locale. Currently users can change the locale for the current session but I want to be able
You can implement LocaleResolver interface to bind users Locale to database. Sample implementation "ApplicationLocaleResolver.java" is below
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import net.app.locale.service.UserService;
@Configuration
public class ApplicationLocaleResolver extends SessionLocaleResolver {
@Autowired
UserService userService;
@Override
public Locale resolveLocale(HttpServletRequest request) {
SecurityContext securityContext = SecurityContextHolder.getContext();
String userName = securityContext.getAuthentication().getName();
String localeOption = userService.getUsersPreferedLocaleOption(userName);
Locale userLocale = Locale.forLanguageTag(localeOption);
return userLocale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
super.setLocale(request, response, locale);
SecurityContext securityContext = SecurityContextHolder.getContext();
String userName = securityContext.getAuthentication().getName();
userService.saveUsersPreferedLocaleOption(userName, locale);
}
}
I assume your userService has a method that save users local to db. I prefer userService.saveUsersPreferedLocaleOption(userName, locale);
You can change it.
Then you can replace localeResolver bean definition as below.
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
return new ApplicationLocaleResolver();
}