4

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)
mkoryak
  • 57,086
  • 61
  • 201
  • 257

2 Answers2

16

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".
Adrian Smith
  • 17,236
  • 11
  • 71
  • 93
  • thats not fun! can i store my bundle in anything but a properties file? – mkoryak Mar 30 '11 at 20:04
  • Look at http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader,%20java.util.ResourceBundle.Control%29. It explains how resource bundles are loaded, and how you can define other formats. – JB Nizet Mar 30 '11 at 20:16
1

for anyone else who has this issue, here is a little hacky groovy i wrote to fix spaces:

@Test
  void fixMessageBundle(){
      def path = "/path/to/it/messages_en_US.properties";
      String fixed = "";
      Map keys = [:]
      new File(path).eachLine { String it->
          String line = "";
          if(it.contains("=")){
            String key = it.split("=")[0];
            String val = it.split("=")[1];
            if(keys.containsKey(key)){
                println "removed duplicate key: ${key}. kept val: [${keys[key]}], threw out val: [${val}]"
                return
            }
            keys.put(key, val);
            line = key.replaceAll(/([^\\]) /, "\$1\\\\ ") + "=" + val;
          }
          fixed  += line+"\n";
      }
      new File(path).text = fixed;
  }
mkoryak
  • 57,086
  • 61
  • 201
  • 257