novice
novice

Reputation: 51

Nested property in ResourceBundleMessageSource

I wanted help if its possible to auto resolve by Spring the nested property. I am using Spring 4.2.4, Java 8 and the property file looks like-

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

We have a custom localization implementation to get the String from Resource bundle for i18 as defined below-

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

I want to be able to use this method like get(locale, "welcome.message") and expect to render it as Dear Customers of XYZ.

Currently its not working failing in MessageFormat. Is there a way to allow spring auto resolve this.

Upvotes: 5

Views: 1004

Answers (2)

aenigmatista
aenigmatista

Reputation: 11

You can now use curly braces (without symbol $). It work for Spring 5 (tested for 5.3.6 and 5.3.7):

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

Upvotes: 1

user2669657
user2669657

Reputation: 575

I do this with MessageSource in my Controller like this

@Inject
private MessageSource message;

protected String getMessage(String key, Object[] arguments) {
    return message.getMessage(key, arguments, LocaleContextHolder.getLocale());
}

Upvotes: 0

Related Questions