Search code examples
springinternationalizationmessage-bundle

spaces in message bundle keys


I want to be able to have spaces in my message bundle keys because it is easier to convert existing text into keys if you dont have to also convert spaces to underscores.

spring message source doesnt seem to like this though. is it possible?

2011-03-30 15:45:56,519 ERROR [org.springframework.web.servlet.tags.MessageTag] - No message found under code 'Invalid username or password' for locale 'en_US'.
javax.servlet.jsp.JspTagException: No message found under code 'Invalid username or password' for locale 'en_US'.
    at org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:183)

Solution

  • If you are storing your messages via a .properties resource bundle, make sure you add backslashes before the spaces in the key.

    Invalid\ username\ or\ password=Invalid username or password
    

    See the example of Properties files in Wikipedia:

    http://en.wikipedia.org/wiki/.properties

    # Add spaces to the key
    key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".