Announcement Announcement Module
Collapse
No announcement yet.
Cannot resolve codes from messageSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot resolve codes from messageSource

    I have a simple messageSource with two basenames defined as
    shown below.

    Code:
     <bean 
           id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
         >
          <property name="dictionaries">
            <props>
               <prop key="csTest1">classpath:messages/Test1MessageDictionary</prop>
               <prop key="csTest2">classpath:messages/Test2MessageDictionary</prop>
             
            </props>
          </property>
          
          <property name="useCodeAsDefaultMessage">
            <value>true</value>
          </property>
        </bean>
    This message source does not resolve any codes
    given in the basename files (comes back with the code
    due to "useCodeAsDefaultMessage"):

    Code:
    ResourceBundleMessageSource ms = (org.springframework.context.support.ResourceBundleMessageSource)
    		       applicationContext.getBean("messageSource");
    		
    		final String ss = ms.getMessage(code, null, Locale.US);
    		System.out.println(ss);
    I have basename files with and without the _en_US extension:

    classpath:

    messages/
    Test1MessageDictionary.properties
    Test1MessageDictionary_en_US.properties
    Test2MessageDictionary.properties
    Test2MessageDictionary_en_US.properties


    I looked into the implementation of resolveCode in ResourceBundleMessageSource. Since this uses the ClassLoader to load the bundles, I tried to enable log4j debugs on ResourceBundleMessageSource
    but of no avail:

    Code:
    log4j.logger.org.springframework.context.support.ResourceBundleMessageSource=DEBUG, STDOUT
    log4j.logger.org.springframework.core.io.ResourceLoader=DEBUG, STDOUT
    
    #
    # I am not sure the following makes sense;  but it can't hurt
    log4j.logger.java.lang.ClassLoader=DEBUG, STDOUT
    log4j.logger.java.util.ResourceBundle=DEBUG, STDOUT

    I am unable to find how it attempts to load the basenames.

    Please help!

    /U

  • #2
    I can't see any property of a ResourceBundleMessageSource called "dictionaries", so I'm suspicious that your context isn't being loaded or isn't actually defined as listed in the original post. Normally you would set "basenames".

    Also the log4j configuration looks odd, did you mean to set

    Code:
    log4j.category.org.springframework.context.support.ResourceBundleMessageSource=DEBUG
    ?

    Comment


    • #3
      Fixed typo in my quote

      Dave,

      Thanks for spotting the typo. Unfortunately, the typo was in
      my post, not in my implementation.

      I subslcassed ResourceBundleMessageSource so I could set
      a collection of basenames as properties. I found it easier
      to define property injectors.

      Here is my version of ResourceBundleMessageSource.
      Pl view the bean configuration above with this bean
      definition:


      Code:
      public class MyMessageSource extends ResourceBundleMessageSource {
      
      	private Properties dictionary_list;
      	
      	/**
      	 * @return the dictionary_list
      	 */
      	public final Properties getDictionaries() {
      		return dictionary_list;
      	}
      
      	/**
      	 * @param dictionary_list the dictionary_list to set
      	 */
      	@Required
      	public synchronized void setDictionaries(final Properties dictionary_list) {
      		this.dictionary_list = dictionary_list;
      	}
      
      	/**
      	 * 
      	 */
      	public MyMessageSource() {
      		
      	}
      	
      	public void initializeBean() {
      		if (getDictionaries() != null && getDictionaries().size() > 0) {
      			final String[] message_files = new String[getDictionaries().size()];
      			int i = 0;
      			for (Object dictionary : getDictionaries().values()) {
      				message_files[i++] = (String)dictionary;
      			}
      			
      			setBasenames(message_files);
      		}
      	}
      }
      Thanks again for your insight - if you spot anything obviously wrong,
      pl let me know. I'd immensely appreciate it!!

      /U

      Comment

      Working...
      X