Any way to pluralize with translations using formatjs?

891 Views Asked by At

I originally misunderstood the usage of , as it seems it's only used for single-language applications.
Is there a way to pluralize message that use translations as well? For example, we have a simple line of code saying something like so:

{pluralize(formattedType, numberSelected, true)} Selected

Which would result in an output of "0 items Selected", "1 item Selected", "2 items Selected", etc.
Is there a way to incorporate similar logic with the formatjs library?

1

There are 1 best solutions below

2
On

You can use MessageFormat

You can see the first example:

new IntlMessageFormat(
  `You have {numPhotos, plural,
      =0 {no photos.}
      =1 {one photo.}
      other {# photos.}
    }`,
  'en-US'
).format({numPhotos: 1})