【问题标题】:spaces in message bundle keys消息包键中的空格
【发布时间】:2011-07-26 09:05:45
【问题描述】:

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

spring 消息源似乎不喜欢这样。有可能吗?

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)

【问题讨论】:

    标签: spring internationalization message-bundle


    【解决方案1】:

    如果您通过 .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".
    

    【讨论】:

    【解决方案2】:

    对于遇到此问题的其他人,这是我为修复空格而写的一些 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;
      }
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 2015-04-03
      • 2022-01-23
      相关资源
      最近更新 更多