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

发布于 2024-08-18 05:04:43 字数 1270 浏览 2 评论 0原文

使用资源包和 MessageFormat 是否可以得到以下结果?

  • 当我调用 getBundle("message.07", "test") 来获取 "Group test"
  • 当我调用 getBundle("message.07", null 时) 获取“未选择组”

我在互联网上找到的每个示例都涉及行星、磁盘上的文件等等。

我只需要检查资源包属性文件中的一个参数是否为 null或不存在)。我希望为 null 参数找到一种特殊格式,例如 {0,choice,null#No group selected|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 "???";
  }
}

我也为其他捆绑包调用此方法,例如

  • 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

Is it possible, with resource bundles and MessageFormat to have the following result?

  • when I call getBundle("message.07", "test") to get "Group test"
  • when I call getBundle("message.07", null) to get "No group selected"

Every example I found on the Internet is with planets, with files on the disk and so on.

I only need to check if one parameter is null (or doesn't exist) in the resource bundle's properties file. I hope to find a special format for the null parameter something like {0,choice,null#No group selected|notnull#Group {0}}.

The method I use to get the bundles is:

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 "???";
  }
}

I also call this method for other bundles, like

  • getBundle("message.08", 1, 2) => "Page 1 of 2" (always parameters, no need to check for null)
  • getBundle("message.09") => "Open file" (no parameters, no need to check for null)

What should I write in my .properties file for message.07 to have the result described?
What I have now is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-08-25 05:04:43

我建议不要尝试更改捆绑包功能(即使您有一个封装它的 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);
}

I'll recommend not trying to change the bundle functionality (even if you have a getBundle method encapsulating it).

Simply do in your code:

getBundle(param == null? "message.07.null": "message.07", param)

Or make another method:

getBundleOrNull("message.07", param, "message.07.null")

that does

public String getBundleOrNull(String key, value, nullKey) {
   return getBundle(value == null? nullKey: key: value);
}
苏大泽ㄣ 2024-08-25 05:04:43

您的 .properties 文件,

message.07=Group {0} 
message.08=Page {0} of {1}
message.09=Open file
message.null = No group selected

然后您需要更改代码以对 params 进行显式检查 null。如果null,那么您可以执行resourceBundle.getString(NULL_MSG)之类的操作。其中 NULL_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'

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

Your .properties file,

message.07=Group {0} 
message.08=Page {0} of {1}
message.09=Open file
message.null = No group selected

And then you need to change your code to put an explicit check params for null. And if null then you can do something like resourceBundle.getString(NULL_MSG). Where NULL_MSG will be this,

private static final String NULL_MSG = "message.null";

So, now your original method would become something like this.

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;
}

Calling my method like below,

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'

Now tell me where is the problem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文