开发者

ResourceBundle/Propertie file to accept array of String in argument {0}

Is there a way to pass an array of String to a resource bundle to localize an unknown number of argument for a given key?

I have:

my.message=List of retired products: {0}

getValue(bundle, "my.message", list.toArray());

With this, only the first item in the array is showed开发者_运维问答 in the resulting message.


No, there are no builtin facilities for that in the MessageFormat API. You need to build a string with the values yourself. E.g.:

StringBuilder products = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
    products.append(list.get(i));
    if (i + 2 < list.size()) products.append(", ");
    else if (i + 2 == list.size()) products.append(" and "); // Localize this?
}
getValue(bundle, "my.message", products);


I think you are going to need a for loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜