How to display messages in Thymeleaf and Spring Boot?
Asked Answered
X

6

22

I created a Spring Boot web application that uses Thymeleaf as the template engine. I configured the MessageSource to look for messages in a subfolder:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);

    return messageSource;
}

In this folder I created the file messages_de.properties with the content ticket.type.BUG=Fehler. In my template I try to display the text like this:

<p th:text="#{ticket.type.BUG}">BUG</p>

But when the page is rendered, I get the following:

<p>??ticket.type.BUG_de_DE??</p>

What am I missing? Do I have to configure any additional beans?

P.S.:

On the 'server side' I am able to obtain the message using MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY).

Xantho answered 9/4, 2015 at 7:17 Comment(0)
X
29

Because I am using Spring Boot, the MessageSource is configured with a MessageSourceAutoConfiguration. These settings can be easily changed in the application.properties file. In my case I had to add the following to this file:

spring.messages.basename=i18n/messages
Xantho answered 9/4, 2015 at 10:40 Comment(2)
This is clever. I though spring.messages.basename only refers to the prefix for each file. Didn't know it included the folders.Atonic
I have the same problem and has this configuration,but this does not work for me..Cecum
L
4

And add this to your application.properties file

#messages
spring.messages.basename=i18n/messages

and store the file n the correct folder as specified above.

you don't need messageSource bean

Lory answered 9/4, 2015 at 15:42 Comment(1)
Sorry. I din't see that. Btw next time you can select that as an answer, so it will be visible for us clearly. And you don't have to down-voting an effort on helping.Lory
P
3

The way I resolved the i18n messaging was to define the MessagesSource bean like you. Additionally I had to override the getValidator() method of the WebMvcConfigurerAdapter.

@Override
public Validator getValidator() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource( messageSource() );
    return validator;
}

After this, it worked for me.

Pruchno answered 9/4, 2015 at 9:31 Comment(1)
This does not solve it for me and I don't know why it should. This configuration has something to do with JSR-303 validation which I don't use.Xantho
R
1

Is there a file messages.properties (no _de) present to allow fallback? Does it work? Is your browser set to locale DE_de ?

Retch answered 9/4, 2015 at 8:25 Comment(1)
Yes (english). No. Yes.Xantho
T
0

The message source be relative to classpath:

messageSource.setBasename("  classpath:i18n/messages");

Here is a tutorial I referenced for thymeleaf and spring = http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

Textualist answered 9/4, 2015 at 7:29 Comment(1)
I don't think so. I have updated my question. When I prepend 'classpath:' I am not able to obtain messages in the Java code, too.Xantho
A
0

I encountered a similar problem. In my case faraj's method worked. But I wondered why I couldn't do it with the bean configuration. And my code was:

@Bean
public MessageSource getMessageResource()  {
    ResourceBundleMessageSource messageResource= new ResourceBundleMessageSource();
    messageResource.setBasename("localization/messages");
    messageResource.setDefaultEncoding("UTF-8");
    return messageResource;
}

Then it occurred to me that the method name would be the bean name by default. so my bean name was getMessageResource. I thought that this naming might be a problem in configuration, so I named the bean myself. Like this:

@Bean("messageSource")
public MessageSource getMessageResource()  {
    ResourceBundleMessageSource messageResource= new ResourceBundleMessageSource();
    messageResource.setBasename("localization/messages");
    messageResource.setDefaultEncoding("UTF-8");
    return messageResource;
}

So it was solved without making any other changes and without any configuration to the properties file.

I am attaching the whole class as an example:

@Configuration
public class LocaleConfig implements WebMvcConfigurer {

    @Bean
    public LocaleResolver localeResolver() {
        return new CookieLocaleResolver();
    }

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

    @Bean("messageSource")
    public MessageSource getMessageResource()  {
        ResourceBundleMessageSource messageResource= new ResourceBundleMessageSource();
        messageResource.setBasename("localization/messages");
        messageResource.setDefaultEncoding("UTF-8");
        return messageResource;
    }

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

}

My resource file structure: structure

Thanks.

Arlenaarlene answered 24/8, 2023 at 22:36 Comment(1)
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewDowitcher

© 2022 - 2024 — McMap. All rights reserved.