cannot resolve spring message code using messageSource
Asked Answered
P

4

4

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?

Pompon answered 31/12, 2010 at 5:14 Comment(0)
A
4

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.

Ascetic answered 31/12, 2010 at 6:3 Comment(1)
Link is now brokenSwisher
B
5

It seems like your path is not correct. since you have your bundle under /WEB-INF/classes/messages/messages_en_US.properties, your basename setting should look like: classpath:messages/messages (basename in this case means path and properties file prefix).

Basket answered 31/12, 2010 at 16:17 Comment(0)
A
4

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.

Ascetic answered 31/12, 2010 at 6:3 Comment(1)
Link is now brokenSwisher
R
3

I use following bean and it is working fine without specifying the path to the file:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" abstract="false"
      scope="singleton" lazy-init="default">
    <property name="basename" value="messages"/>
</bean>

Although the files I use are simply called "messages_en.properties" and "messages_es.properties" well you get the idea.

When you call

    messageSource.getMessage("validation_required", null, null);

do you get an exception? Try to use this file name messages_en.properties or messages_us_en.properties

Riles answered 31/12, 2010 at 16:39 Comment(0)
G
0

Try this look at the comment for getting the string

package yours;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 *  
 * Permet la recuperation des String du LocaleContextHolder hors des jsp 
 * Les langues sont placées dans main/ressources/messagesSources.properties 
 * 
 * Example : new MSG("recuperation_name_invalid").value()
 * 
 */
@Configurable
public class MSG
{

    private String key;

    @Resource(name = "messageSource")
    private MessageSource messageSource;

    public MSG(String key)
    {
        super();
        this.key = key;
    }

    public String value()
    {
        Locale locale = LocaleContextHolder.getLocale();

        return messageSource.getMessage(key, new Object[0], locale);
    }

    @Override
    public String toString()
    {
        return value();
    }


}
Goosefoot answered 20/8, 2014 at 8:17 Comment(1)
This is very inefficient, instantiating the whole MSG object everytime you need a string (with the @Resource part)Woodbridge

© 2022 - 2024 — McMap. All rights reserved.