In applicationContext.xml I have defined MessageSource like this:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/i18n/messages</value>
</list>
</property>
</bean>
I also need to load all the localized messages so I have made my own class for it:
public class ErpMessageSource extends ResourceBundleMessageSource {
public Map<String, String> getMessages(String basename, Locale locale) {
Set<String> keys = getKeys(basename, locale);
Map<String, String> m = new HashMap<String, String>();
for (String key : keys) {
try {
m.put(key, getMessage(key, null, locale));
} catch (Exception e) {
System.err.println(key + " - " + e.getLocalizedMessage());
}
}
return m;
}
private Set<String> getKeys(String basename, Locale locale) {
ResourceBundle bundle = getResourceBundle(basename, locale);
return bundle.keySet();
}
}
I have 2 problems with this:
Problem 1: My message files are located under WEB-INF/i18n directory. It contains only 2 files: messages_en.properties and messages_hr.properties.
If I try to run the above code I'm getting warning: "ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale 'some locale'", followed by NullPointerException.
If I move messages files to WEB-INF/classes folder problem disappears however I get 2nd problem.
Problem 2: If I run the code above while messages are under WEB-INF/classes directory I'm getting exception: "No message found under code 'some.code' for locale 'some locale'" even thou all keys are loaded correctly.
Question is: how to avoid problem no.1 by keeping messages inside WEB-INF/i18n folder and I need a solution for 2nd problem because I really have no idea what's going on there.