Description
There might be an error in the Thymeleaf-Spring docs that prevents localized text from appearing in templates.
I encountered the problem only since upgrading to Spring 5, and I wonder how it could work before.
The problem is that, when following the docs for the ViewResolver bean creation, the MessageSource is not injected in the TemplateEngine so a default is created that ignores my customization.
This is what I think is wrong:
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
The templateEngine() call returns a new instance of SpringTemplateEngine that doesn't get a chance of being injected with the MessageSource.
What I'm using now:
@Bean
public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
This will not create a new instance of SpringTemplateEngine but reuse the @bean that Spring has injected with the MessageSource.
The possibly wrong code is shown twice in https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html