2017-10-02 41 views
5

tôi sau this dẫn cho quốc tế hóa mùa xuân nó thực hiện LocalResolver như thế nàycách thiết defaultLocale lập trình trong khởi động mùa xuân

@Bean 
public LocaleResolver localeResolver() { 
    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); 
    sessionLocaleResolver.setDefaultLocale(Locale.US); 
    return sessionLocaleResolver; 
} 

nhưng tôi muốn thiết lập defaultLocal bằng cách nhận thông tin ngôn ngữ sử dụng trong cơ sở dữ liệu và thiết lập nó như thế nào tôi có thể làm việc đó đi? cảm ơn sự giúp đỡ

+0

Bạn don' t. 'DefaultLocale' là cho toàn bộ ứng dụng KHÔNG cho mỗi người dùng. –

+1

Có cách nào khác tôi có thể làm theo để xác định ngôn ngữ ứng dụng theo sở thích của người dùng – gokhanbirincii

+1

Đó là toàn bộ ý tưởng của trình phân giải cục bộ ... Nếu bạn muốn sử dụng các tùy chọn, hãy sử dụng triển khai 'LocaleResolver' của riêng bạn. –

Trả lời

1

Tôi nghĩ bạn muốn đặt Ngôn ngữ cho phiên hiện tại, không phải là Ngôn ngữ mặc định. Giả sử có một session đang tồn tại (ví dụ: sau khi người dùng đăng nhập):

Autowire LocaleResolver, HttpServletRequestHttpServletResponse và sử dụng các phương pháp LocaleResolver.setLocale:

Locale userLocale = getLocaleByUsername(username); //load from DB 
    localeResolver.setLocale(httpServletRequest, httpServletResponse, userLocale); 

này sẽ thiết lập Locale cho phiên hiện tại.

2

Một cách tiếp cận tiêu chuẩn bạn có thể thử là sử dụng tiêu đề HttpHeaders.ACCEPT_LANGUAGE. Tôi giả sử bạn đang lưu trữ các ngôn ngữ được hỗ trợ trong DB, vì vậy đối với conviniece di chuyển nó vào tệp thuộc tính, vì số lượng bản ghi sẽ không nhiều. Sau đó, thử cách tiếp cận của tôi như thế này

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(); 
} 
0

Nếu bạn sử dụng bảo mật mùa xuân, có thể giải pháp này sẽ giúp bạn.

Quốc tế hóa cấu hình:

@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()); 
    } 

} 

Xuân An config:

@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(); 
    } 

} 

Bộ điều khiển:

@Controller 
public class LoginController { 

    @RequestMapping("/loginSuccess") 
    public String loginSuccess(){ 

     User user = getUserFromDatabase; 

     return "redirect:/home?lang="+user.getLanguage(); 
    } 

} 
Các vấn đề liên quan