ResourceBundleMessageSource 中的嵌套属性

nov*_*ice 5 java spring-mvc internationalization

如果可以通过 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
Run Code Online (Sandbox Code Playgroud)

我们有一个自定义的本地化实现来从 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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

小智 0

我在控制器中使用MessageSource执行此操作,如下所示

@Inject
private MessageSource message;

protected String getMessage(String key, Object[] arguments) {
    return message.getMessage(key, arguments, LocaleContextHolder.getLocale());
}
Run Code Online (Sandbox Code Playgroud)