Results 1 to 2 of 2

Thread: Odd behavior using ResourceBundleMessageSource with messages containing apostrophes

  1. #1
    Join Date
    Jul 2007
    Posts
    3

    Default Odd behavior using ResourceBundleMessageSource with messages containing apostrophes

    I'm experiencing some strange behavior when fetching messages with apostrophes surrounding argument usage from a resource bundle using Spring's ResourceBundleMessageSource. Specifically, the apostrophes seem to short-circuit the insertion of argument values into the message. The following are and example resource bundle file and a simplified version of the calling class:

    application.properties:
    Code:
    message.0=Blah {1} {0}
    message.1=Blah {1} '{0}'
    message.2=Blah {1} \'{0}\'
    message.3=Blah {1} \\'{0}\\'
    ResourceServiceImpl:
    Code:
    public class ResourceServiceImpl extends BaseService implements ResourceService, InitializingBean
    {
        private ResourceBundleMessageSource messageSource;
        
        @Override
        public void afterPropertiesSet() throws Exception
        {
            Validate.notNull(messageSource);
        }
        
        public String getResource(String key, Object... params) throws RuntimeException
        {
            LanguageService service = serviceProvider.getLanguageService();
            Locale locale = service.getSystemLocale();
    
            return messageSource.getMessage(key, params, locale);
        }
    }
    Now, assuming resourceService is a reference to an instance of ResourceServiceImpl and I call resourceService.getResoruce(...) once for each message as follows, I get very strange results:

    Calling code:
    Code:
    for (int i=0; i<4; i++)
    {
        System.out.println(resourceService.getMessage("message"+i, "arg0", "arg1");
    }
    Result:
    Code:
    Blah arg1 arg0
    Blah arg1 {0}
    Blah arg1 {0}
    Blah arg1 \{0}\
    So no combination of escape characters seems to do the trick either... Any ideas as to what is going on here?

  2. #2
    Join Date
    Jul 2007
    Posts
    3

    Default Solved... sort of

    Shortly after posting I discovered the solution...

    Turns out that prior to migrating to Spring, Struts was handling this for us in org.apache.struts.util.MessageResources#getMessage (), and wasn't using java.text.MessageFormat#applyPattern() as the Spring class was.

    See http://java.sun.com/j2se/1.5.0/docs/...ageFormat.html for more information on the special meaning of single quotes in messages.

    It turns out that the solution is to construct the message as follows (those are two single quotes on each side):

    Code:
    test.resource.2=Blah {1} ''{0}''
    The only problem is, now we'll likely have one too many single quotes in anything that still uses the Struts method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •