Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf + Spring (not Boot) - how to show messages from messageSource

I'm having trouble setting up my Spring MVC (not using Boot, because I started it prior to discovering Spring Initializr) with Thymeleaf to show messages from my resource bundle.

Main configuration class for the app is:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.m.h")
public class HrmConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setPrefix("/WEB-INF/html/");
        templateResolver.setSuffix(".html");

        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.addDialect(new SpringSecurityDialect());
        engine.addDialect(new LayoutDialect(new GroupingStrategy()));
        engine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        viewResolver.setCache(false);

        return viewResolver;
    }

    @Bean(name = "messageSource")
    public MessageSource messageSource() {

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/i18n/messages");
        messageSource.setFallbackToSystemLocale(false);
        messageSource.setCacheSeconds(0);
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    }

    @Bean(name="localeResolver")
    public LocaleResolver localeResolver(){
        SessionLocaleResolver  resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(new Locale("pt", "BR"));
        return resolver;
    } 

    @Bean
    public UrlTemplateResolver urlTemplateResolver() {
        return new UrlTemplateResolver();
    }

    // other beans and configs
}

The general view of the application is:

enter image description here

And the messages_pt_BR.properties file is:

#messages
spring.messages.basename=i18n/messages

app.name=APPLICATION NAME

But, when I try to show the APPLICATION NAME somewhere on the page, with this:

<p th:text="#{app.name}">app.name</p>

I get only

??app.name_pt_BR??

I have browsed a number of possible solutions and tutorials, including:

I18n in Spring boot + Thymeleaf http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example How to display messages in Thymeleaf and Spring Boot?

Still I cannot get it working.

Browser and OS are set to locale en_US.

How to get tihs solved?

like image 213
gtludwig Avatar asked Apr 20 '16 18:04

gtludwig


1 Answers

Working with Spring Boot here, but I was able to reproduce your issue and it's tied to the ReloadableResourceBundleMessageSource once I changed it to ResourceBundleMessageSource it worked like it should.

@Bean(name = "messageSource")
public MessageSource messageSource() {

    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("/i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);
    messageSource.setDefaultEncoding("UTF-8");

    return messageSource;
}

Update:

Found that you need to provide full pathing for the ReloadableReourceBundleMessageSource so if your /il8n directory is within your classpath you need to set your basename to classpath:/il8n/messages

see my post here: Why does Thymeleaf International only works with ResourceBundleMessageSource

like image 133
ndrone Avatar answered Sep 21 '22 15:09

ndrone