开发者

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

开发者 https://www.devze.com 2022-12-27 16:38 出处:网络
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?

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

精彩评论

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