gpt4 book ai didi

java - ResourceBundleMessageSource 中的嵌套属性

转载 作者:行者123 更新时间:2023-12-04 22:48:26 25 4
gpt4 key购买 nike

如果可以通过 Spring 自动解析嵌套属性,我需要帮助。我正在使用 Spring 4.2.4、Java 8 并且属性文件看起来像-

# messages.properties
company.name=XYZ
welcome.message=Dear Customers of ${company.name}
about.company=${company.name} is blah blah

我们有一个自定义的本地化实现来从 i18 的资源包中获取字符串,如下定义 -
public String get(Locale locale, String key, Object... arguments) {
String pattern = this.getString(locale, key);
MessageFormat formatter = new MessageFormat(pattern, locale);
StringBuffer buffer = new StringBuffer();
formatter.format(arguments, buffer, null);
return buffer.toString();
}

我希望能够像 get(locale, "welcome.message") 一样使用这种方法,并希望将其呈现为 XYZ 的尊敬的客户。

目前它在 MessageFormat 中无法正常工作。有没有办法让 spring 自动解决这个问题。

最佳答案

您现在可以使用花括号(没有符号 $)。
它适用于 Spring 5(针对 5.3.6 和 5.3.7 进行了测试):

# messages.properties
company.name=XYZ
welcome.message=Dear Customers of {company.name}
about.company={company.name} is blah blah

关于java - ResourceBundleMessageSource 中的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com