Internationalization with thymeleaf and Spring

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Internationalization with thymeleaf and Spring

Eric Francis
I'm basically trying to add thymeleaf to this example: http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/

This is my app config:
        @Bean
        public SessionLocaleResolver localeResolver() {
                SessionLocaleResolver localeResolver = new SessionLocaleResolver();
                Locale defaultLocale = new Locale("en");
                localeResolver.setDefaultLocale(defaultLocale);
                return localeResolver;
        }
       
        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor() {
                LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
                localeChangeInterceptor.setParamName("language");
                return localeChangeInterceptor;
        }
       
        @Bean
        public ControllerClassNameHandlerMapping handlerMapping() {
                ControllerClassNameHandlerMapping ccnhm = new ControllerClassNameHandlerMapping();
                //I am not sure if I am doing this right
                Object[] interceptors = new Object[1];
                interceptors[0] = localeChangeInterceptor();
                ccnhm.setInterceptors(interceptors);
                return ccnhm;
        }
       
        @Bean
        public ResourceBundleMessageSource messageSource() {
                ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
                messageSource.setBasename("/com/thymeleaf/poc/resources/Messages");
                return messageSource;
        }

I have a Messages.properties file and a Messages_zh_CN.properties;
I know my Messages.properties is working and the rest of my thymeleaf app is working pre internationalization attempt (localeResolver, localeChangeInterceptor, and handlerMapping Beans).

I'm expecting thymeleaf to return the correct message with regard to locale.

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Internationalization with thymeleaf and Spring

Eric Francis
Got this to work with this bean config:

@Configuration
@EnableWebMvc
@ComponentScan("com.thymeleaf.poc.controller")
public class ThymeleafSpringJavaConfig extends WebMvcConfigurerAdapter {

        static final Logger logger = LoggerFactory.getLogger(ThymeleafSpringJavaConfig.class);

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(localeChangeInterceptor());
        }

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

        @Bean
        public CookieLocaleResolver localeResolver() {
                CookieLocaleResolver localeResolver = new CookieLocaleResolver();
                Locale defaultLocale = new Locale("en");
                localeResolver.setDefaultLocale(defaultLocale);
                return localeResolver;
        }

        @Bean
        public ResourceBundleMessageSource messageSource() {
                ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
                messageSource.setBasename("/com/thymeleaf/poc/resources/Messages");
                return messageSource;
        }....