3

我希望能够在我的消息包键中包含空格,因为如果您不必将空格也转换为下划线,则将现有文本转换为键更容易。

春季消息来源似乎并不喜欢这样。是否可以?

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)
4

2 回答 2

15

如果您通过 .properties 资源包存储消息,请确保在键中的空格之前添加反斜杠。

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

请参阅 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".
于 2011-03-30T20:00:23.960 回答
1

对于遇到此问题的其他人,这是我为修复空间而写的一些 hacky groovy:

@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;
  }
于 2011-03-30T20:35:06.017 回答