消息包密钥中的空格

时间:2011-03-30 19:52:08

标签: spring internationalization message-bundle

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

春天的消息来源似乎并不喜欢这样。有可能吗?

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)

2 个答案:

答案 0 :(得分:13)

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

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

请参阅维基百科中的属性文件示例:

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".

答案 1 :(得分: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;
  }