简体   繁体   中英

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. Eg:

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

我认为您将需要一个for循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM