Search code examples
springthymeleafresourcebundle

How to get values from message bundle?


In my application configuration class, I have the following entry:

    @Bean(name = "messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver(){
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(new Locale("pt_BR"));
        return resolver;
    } 

The message_pt_BR.properties is located according to the attached image.

enter image description here

I'm trying to access an entry on this with a <span th:text="#{app.name}"></span> file and all I get is:

??app.name_pt_br??

What am I missing here?


Solution

  • Your message bundle name should be

    messages_pt_BR.properties
    

    It's case sensitive.

    It's OK without default message bundle(messages.properties). Mine works fine with only _en and _zh_TW.

    And it should be ok that you put your resource bundle everywhere if you give the correct path to set basename.