Search code examples
javaspringlocalizationresourcebundle

cannot resolve spring message code using messageSource


I am trying to hook up a messageSource in spring to use for my application. Its not working, gives this error:

org.springframework.context.NoSuchMessageException: No message found under code 'validation_required' for locale 'en'.

my applicationContext.xml contains this def for messageSource:

   <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:messages</value>
            </list>
        </property>
    </bean>

my messages properties file lives in:

/WEB-INF/classes/messages/messages_en_US.properties

Finally, the call i made that generates the error is:

String message = messageSource.getMessage("validation_required", null, Locale.ENGLISH);

Can anyone help me at this hour?


Solution

  • The problem is with the way you have defined the resource bundle and the locale you are specifying (They doesn't match with the resource bundle's search order. Either rename your bundle to "messages_en.properties" or invoke the "getMessage(...)" with new Locale("en","US"). I prefer the first option.