Spring boot Internationalization ?lang=en has no effect

时间:2017-08-04 12:53:01

标签: java spring spring-mvc spring-boot internationalization

I'm currently working on a Web-App using Spring boot (including spring security) and thymeleaf. At the moment i'm trying to integrate internationalization support for the languages english and german as a start.

For the basics I've followed this Tutorial and tried to get their example to work. Now if I go to Localhost:8443/international and choose one of the languages the URL gets built correctly to .../international?lang=en. Thymeleaf even reads the fields in the .propperties file marked as default. But I can't get it to actually switch the language no matter what I do.

Code:

@Configuration
@EnableWebMvc
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
    registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");

}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}

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



}

Like this I assume it's taking the default messages.propperties. However if I put the LocaleResolver Bean into my

public class Application extends SpringBootServletInitializer 

class where the main method is, it takes whatever language is set as default Locale there.

From where I am at right now I conclude that my .propperties files are fine and can be read but something with the LocaleChangeInterceptor does not work propperly. I went into debug mode but any breakpoints in the WebConfig class did not trigger at all.

One assumption of mine would be Spring security messing something up, such that the ?lang request can't be resolved. (Tried both logged-in and logged-out).

Would be really glad if anyone has some idea on how to resolve the issue, thanks for every reply in advance!

My Application class:

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@ComponentScan(basePackages = { "my.company.controller", "my.company.Services", "java.lang.String","my.company.Services.Security" })
@EnableConfigurationProperties(my.company.Services.Storage.StorageProperties.class)
public class Application extends SpringBootServletInitializer {

@Autowired
private UserRepository repository;

@Autowired
private SecUserDetailsService userDetailService;


public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner init(StorageService storageService) {
    return (args) -> {

        repository.deleteAll();

        userDetailService.addUser("bob", "ross", "admin");
        userDetailService.addUser("1", "1", "superuser");
        userDetailService.addUser("2", "2", "admin");

        System.out.println("All users currently in DB:");
        System.out.println("-------------------------------");
        for (User user1 : repository.findAll()) {
            System.out.println(user1);
        }
        System.out.println();

        // storageService.deleteAll();

        try {
            storageService.init();
        } catch (StorageException e) {
            System.out.println("Ordner schon vorhanden");
        }
    };

//If i add this here french gets picked as default language, changing does still not work
@Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.FRENCH);
        return slr;
    }

}

1 个答案:

答案 0 :(得分:1)

尝试删除2注释。 @ComponentScan会自动扫描组件。我猜你的WebConfig类没有加载。

相关问题