【发布时间】:2020-10-16 10:25:50
【问题描述】:
我在 Spring Boot 2 中使用 Thymeleaf。
是否可以提供 YAML / JSON 格式的消息源(翻译)而不是 *.properties 文件?
【问题讨论】:
标签: json spring-boot internationalization yaml thymeleaf
我在 Spring Boot 2 中使用 Thymeleaf。
是否可以提供 YAML / JSON 格式的消息源(翻译)而不是 *.properties 文件?
【问题讨论】:
标签: json spring-boot internationalization yaml thymeleaf
是的,您可以通过扩展 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);
}
}
【讨论】: