Sebastian Dusza
Sebastian Dusza

Reputation: 2528

Thymeleaf messages / translations in JSON / YAML files

I'm using Thymeleaf with Spring Boot 2.

Is it possible to provide message sources (translations) in YAML / JSON format instead of *.properties files ?

Upvotes: 2

Views: 714

Answers (1)

stacker
stacker

Reputation: 4485

yes, you can do it by extending the AbstractMessageSource class. here a sample you can use as a starting point:

@Component("messageSource")
public class JsonMessageSource extends AbstractMessageSource {

    private static final String DEFAULT_LOCALE_CODE = "en";
    @Override
    protected MessageFormat resolveCode(String key, Locale locale) {
        String message = resolveUsingJsonOrYaml(key,locale); //you have to implement this this
    if (message == null) {
        message = resolveUsingJsonOrYaml(key,DEFAULT_LOCALE_CODE);
    }
    return new MessageFormat(message, locale);
    }
}

Upvotes: 3

Related Questions