Plural forms with Java java.util.ResourceBundle

1.6k views Asked by At

I usejava.util.ResourceBundle for i18n like this:

try {
  resourceBundle = ResourceBundle.getBundle("Messages", locale);
} catch (MissingResourceException ex) {
  logger.log(Level.SEVERE, ex.getMessage(), ex);
}

String localizedString = resourceBundle.getString("key");

I want to create the plural forms are well. Like:

one object
two objects

How can I define plural forms of localized Strings?

2

There are 2 answers

0
AudioBubble On BEST ANSWER
0
JB Nizet On

A ResourceBundle doesn't deal with pluralization. It just allows getting values associated with keys, based on a locale.

To have pluralized translations, use different keys, or use a ChoiceFormat. Note that MessageFormat can use a ChoiceFormat by specifying it in its pattern.