【问题标题】:Thymeleaf messages / translations in JSON / YAML filesJSON / YAML 文件中的 Thymeleaf 消息/翻译
【发布时间】:2020-10-16 10:25:50
【问题描述】:

我在 Spring Boot 2 中使用 Thymeleaf。

是否可以提供 YAML / JSON 格式的消息源(翻译)而不是 *.properties 文件?

【问题讨论】:

    标签: json spring-boot internationalization yaml thymeleaf


    【解决方案1】:

    是的,您可以通过扩展 AbstractMessageSource 类来实现。这里有一个示例,您可以将其用作起点:

    @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);
        }
    }
    

    【讨论】:

    • 谢谢。这很有帮助。
    猜你喜欢
    • 2016-05-24
    • 2021-11-24
    • 2012-06-29
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多