JSON / YAML 文件中的 Thymeleaf 消息/翻译

Posted

技术标签:

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

我在 Spring Boot 2 中使用 Thymeleaf。

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

【问题讨论】:

【参考方案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);
    

【讨论】:

谢谢。这很有帮助。

以上是关于JSON / YAML 文件中的 Thymeleaf 消息/翻译的主要内容,如果未能解决你的问题,请参考以下文章