How to set defaultLocale programmatically in spring boot

I follow this guide to internationalize spring 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 , getting the language information of the user in the database and asking it, how can I do this? thanks for the help

+5
source share
3 answers

I think you want to set Locale for the current session, and not for Locale by default. Assuming an existing session exists (i.e., after the user logs in):

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 install Locale for the current session.

+1
source

One standard approach you can try is to use the HttpHeaders.ACCEPT_LANGUAGE header. I assume that you store supported locales in the database, so for convice, move it to the properties file, as the number of records will not be much. 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(); } 
+2
source

If you use spring protection, perhaps this solution will help you.

Internationalization Configuration:

 @Configuration @EnableAutoConfiguration public class InternationalizationConfig extends WebMvcConfigurerAdapter { @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); slr.setDefaultLocale(new Locale("tr"));//Locale.forLanguageTag("tr"));// // slr.setDefaultLocale(Locale.forLanguageTag("tr")); return slr; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } } 

Spring Security Configuration:

 @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .anyRequest().authenticated() .and() .exceptionHandling().accessDeniedPage("/login") .and() .formLogin().loginPage("/index") .usernameParameter("username") .passwordParameter("password") .loginProcessingUrl("/j_spring_security_check") .failureUrl("/loginFail") .defaultSuccessUrl("/loginSuccess") .and() .logout().logoutUrl("/logout").logoutSuccessUrl("/index") ; http.headers().frameOptions().disable(); } } 

Controller:

 @Controller public class LoginController { @RequestMapping("/loginSuccess") public String loginSuccess(){ User user = getUserFromDatabase; return "redirect:/home?lang="+user.getLanguage(); } } 
0
source

Source: https://habr.com/ru/post/1272273/


All Articles