2
votes

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.

2

2 Answers

7
votes

You probably only have to add the resource folder to the classpath of your project.

Try to do the following operation:

Right click on the project name in the Package Explorer view ---> Properties ---> Java Build Path ---> stay in the Source tab (opened by default) ---> click on Folder ---> the list of all the folders inside your project should now appear ---> tick on the folder that contains the message properties file

Rebuild the project and try to run.

1
votes

Although it has been 2 years since the question asked, but I've got the same problem and found a solution.

If you want to keep your i18n properties file somewhere outside the webcontent folder, you should modify the file org.eclipse.resources.prefs in the .setting folder. and do like this:

eclipse.preferences.version=1
encoding//WebContent/WEB-INF/i18n/property_en.properties=UTF-8
encoding//WebContent/WEB-INF/i18n/property_fr.properties=UTF-8

In the bean property, set the messageSource like you did before. but add :

<property name="defaultEncoding" value="UTF-8" />

Hope it works.