I18n in Spring boot + Thymeleaf

前端 未结 1 1409
星月不相逢
星月不相逢 2021-02-13 02:26

I\'m trying to make a multilanguage application using Spring boot and Thymeleaf.

I made few properties files to save the different messages but I\'m only able to display

相关标签:
1条回答
  • 2021-02-13 02:43

    Your application should extends WebMvcConfigurerAdapter

    @SpringBootApplication
    public class NerveNetApplication extends WebMvcConfigurerAdapter {
    
        public static void main(String[] args) {
            SpringApplication.run(NerveNetApplication.class, args);
        }
    
        @Bean
        public LocaleResolver localeResolver() {
            return new CookieLocaleResolver();
        }
    
        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor() {
            LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
            lci.setParamName("lang");
            return lci;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeChangeInterceptor());
        }
    }
    

    Then on browser you can switch language with param lang Example: http://localhost:1111/?lang=kh which messages_kh.properites will store the content of Khmer language.

    0 讨论(0)
提交回复
热议问题