Q:

how to set defaultLocale programmatically in spring boot

I am following this guide for spring internationalization it implements LocalResolver like this 
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale. US);
    return sessionLocaleResolver;
}

but I want to set defaultLocal by getting user language information in database and set it how can I do that?  thanks for help

A:

I think you want to set the Locale for the current session, not the default Locale. 
Assuming there is an existing session (i.e. after user login):
Autowire LocaleResolver, HttpServletRequest and HttpServletResponse and use the LocaleResolver.setLocale method: 
    Locale userLocale = getLocaleByUsername(username); //load from DB
    localeResolver.setLocale(httpServletRequest, httpServletResponse, userLocale);

This will set the Locale for the current session. 

A:

One standard approach you can try is using HttpHeaders. ACCEPT_LANGUAGE header.  I assume you are storing supported locales in DB , so for conviniece move it to properties file , as number of records wont be many.  Then try my approach like this 
public Locale resolveLocale(HttpServletRequest request) {
    String header = request.getHeader(HttpHeaders. ACCEPT_LANGUAGE);
    List<Locale. LanguageRange> ranges =  Locale. LanguageRange.parse(header);
    return findMatchingLocale(ranges);
}

public Locale findMatchingLocale(List<Locale. LanguageRange> lang) {
    Locale best = Locale.lookup(lang, supportedLocale); // you can get supported from properties file , we maintaining list of supported locale in properties file
    String country = findCountry(lang);
    return new Locale(best.getLanguage(), country);
}

public String findCountry(List<Locale. LanguageRange> ranges) {
    Locale first = Locale.forLanguageTag(ranges.get(0).getRange());
    first.getISO3Country();
    return first.getCountry();
}

