Java 测试MessageFormat的.properties文件中的null参数

Java 测试MessageFormat的.properties文件中的null参数,java,resourcebundle,messageformat,Java,Resourcebundle,Messageformat,使用资源包和MessageFormat是否可能产生以下结果 当我调用getBundle(“message.07”,“test”)获取“Group test” 当我调用getBundle(“message.07”,null)获取“未选择任何组” 我在互联网上找到的每一个例子都是关于行星、磁盘上的文件等等 我只需要检查资源包的属性文件中是否有一个参数null(或不存在)。我希望为null参数找到一种特殊的格式,比如{0,choice,null#未选择任何组| notnull#group{0} 我

使用资源包和MessageFormat是否可能产生以下结果

  • 当我调用
    getBundle(“message.07”,“test”)
    获取
    “Group test”
  • 当我调用
    getBundle(“message.07”,null)
    获取
    “未选择任何组”
我在互联网上找到的每一个例子都是关于行星、磁盘上的文件等等

我只需要检查资源包的属性文件中是否有一个参数
null
(或不存在)。我希望为null参数找到一种特殊的格式,比如
{0,choice,null#未选择任何组| notnull#group{0}

我用于获取捆绑包的方法是:

public String getBundle(String key, Object... params) {
  try {
    String message = resourceBundle.getString(key);
    if (params.length == 0) {
      return message;
    } else {
      return MessageFormat.format(message, params);
    }
  } catch (Exception e) {
    return "???";
  }
}
我还为其他bundle调用此方法,如

  • getBundle(“message.08”,1,2)
    =>
    “第1页,共2页”
    (始终为参数,无需检查
    null
  • getBundle(“message.09”)
    =>“打开文件”(无参数,无需检查
    null
我应该在.properties文件中为
message.07
写些什么来描述结果?
我现在得到的是:

message.07=Group {0} 
message.08=Page {0} of {1}    # message with parameters where I always send them
message.09=Open file          # message without parameters

您的
.properties
文件

message.07=Group {0} 
message.08=Page {0} of {1}
message.09=Open file
message.null = No group selected
然后您需要更改代码,以对
null
进行显式检查
params
。如果
null
则可以执行类似
resourceBundle.getString(null\u MSG)
的操作。其中
NULL\u MSG
将为

private static final String NULL_MSG = "message.null";
所以,现在你原来的方法会变成这样

public String getBundle(String key, Object... params) {
  String message = null;
  try {
    if (params == null) {
      message = resourceBundle.getString(NULL_MSG);
    } else {
      message = MessageFormat.format(resourceBundle.getString(key), params);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return message;
}
像下面这样调用我的方法

getBundle("message.07", "test") // returning 'Group test'
getBundle("message.07", null) // returning 'No group selected'
getBundle("message.08", 1, 2) // returning 'Page 1 of 2'
getBundle("message.08", null) // returning 'No group selected'
getBundle("message.09", new Object[0]) // returning 'Open file'
getBundle("message.09", null) // returning 'No group selected'

现在告诉我问题出在哪里?

我建议不要尝试更改bundle功能(即使您有一个封装它的getBundle方法)

只需在代码中执行以下操作:

getBundle(param == null? "message.07.null": "message.07", param)
或者用另一种方法:

getBundleOrNull("message.07", param, "message.07.null")
确实如此

public String getBundleOrNull(String key, value, nullKey) {
   return getBundle(value == null? nullKey: key: value);
}

我不想更改该方法,因为它在我的代码中随处可见,并且应该可以处理其余的消息(其中参数是数字、字符串…),而不需要检查
null
。还有可能出现更多这样的情况(更多带有空测试的消息),它不会在任何地方破坏代码,只需更改内部行为即可。参数不变,返回类型也不变。我看不出有什么问题。您不需要为每条消息都提供补充密钥。只需为
null
创建一个泛型。那就足够了,阿费克斯。此外,请检查我的编辑。我在问题中添加了更多细节。例如,对于您的更改,调用
getBundle(“message.09”)
将不会给出预期的结果。您是否有只接受一个参数的重载方法?此外,在本例中,我的方法将给出结果“openfile”。你还期待什么?谢谢你的回答。我会选择把你的答案和Vinegear的答案结合起来,很难选择一个公认的答案。无论如何,我会投票给你的答案。我的问题的答案似乎是,传递给
MessageFormat
String
没有特殊格式来检查参数是否为
null