---
title: "Select JavaScript plural forms in C#"
url: https://recursiveai.org/oneecma/cookbook/select-plural-message-forms/
description: "Use JavaScript Intl.PluralRules in C# to select count and rank forms for your own localized messages. See runnable cardinal and ordinal examples."
version: "1.1.1"
---

# Select JavaScript plural forms in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/select-plural-message-forms/select-plural-message-forms.zip)

Choose “2 files” for a count or “2nd” for a rank.

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const count = 2;
const cardinal = new Intl.PluralRules('en-US');
const ordinal = new Intl.PluralRules('en-US',
  { type: 'ordinal' });
const units = { one: 'file', other: 'files' };
const ends = { one: 'st', two: 'nd',
  few: 'rd', other: 'th' };
`${count} ${units[cardinal.select(count)]}\n` +
  `${count}${ends[ordinal.select(count)]}`;
""").AsString);
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/select-plural-message-forms/select-plural-message-forms.zip), then `dotnet run`.

Prints:

```text
2 files
2nd
```

## The full program

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const cardinal = new Intl.PluralRules('en-US');
const ordinal = new Intl.PluralRules('en-US', { type: 'ordinal' });
const files = {
  one: n => `${n} file uploaded`,
  other: n => `${n} files uploaded`
};
const suffix = { one: 'st', two: 'nd', few: 'rd', other: 'th' };

[1, 2, 3, 11, 21].map(n => {
  const countForm = cardinal.select(n);
  const rankForm = ordinal.select(n);
  return `${countForm}: ${files[countForm](n)} | ${rankForm}: ${n}${suffix[rankForm]}`;
}).join('\n');
""").AsString);
```

`dotnet run` prints:

```text
one: 1 file uploaded | one: 1st
other: 2 files uploaded | two: 2nd
other: 3 files uploaded | few: 3rd
other: 11 files uploaded | other: 11th
other: 21 files uploaded | one: 21st
```

OneEcma ships data for `en-US`, `en-GB`, `es` (and `es-US`), `fr`, `de`, and `pt-BR`. The English words come from your own table; `select` only returns the category key. A locale outside that set can fall back to the host’s default; the result won’t necessarily match your translations.

## Variations

**Compare the same count in French and English.** Select the template for the requested locale, then look up the category:

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const bag = {
  fr: { one: 'fichier', other: 'fichiers' },
  'en-US': { one: 'file', other: 'files' }
};
['fr', 'en-US'].map(locale => {
  const form = new Intl.PluralRules(locale).select(0);
  return `${locale}: 0 ${bag[locale][form]} (${form})`;
}).join('\n');
""").AsString);
```

```text
fr: 0 fichier (one)
en-US: 0 files (other)
```

**Choose the category for a displayed decimal.** Set `minimumFractionDigits` to `1` when the count is displayed with at least one fractional digit:

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const rules = new Intl.PluralRules('en-US', {
  minimumFractionDigits: 1
});
rules.select(1);
""").AsString);
```

```text
other
```

## Related recipes

- [Format numbers with JavaScript Intl](https://recursiveai.org/oneecma/cookbook/format-numbers-with-javascript-intl/)
- [Format JavaScript durations](https://recursiveai.org/oneecma/cookbook/format-javascript-durations/)

## The short answer

Call [`Intl.PluralRules.select`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/select) in JavaScript hosted by OneEcma. The default `cardinal` rules select a category for a quantity; `type: 'ordinal'` selects one for a position. Use the result as a key into a message table you supply. The JavaScript in the example returns the finished text to C#, not the `Intl.PluralRules` object.

## How do you use Intl.PluralRules to pluralize a message?

Build a table for each language you display. For a count of uploaded files, keys like `one` and `other` can select whole sentences, not just a noun or an `s` suffix. This leaves translators free to change word order and any other words that must agree with the count. If the application has a custom message such as “No uploads yet,” treat it as a separate zero-count case rather than assuming `select(0)` returns `zero`.

[`Intl.PluralRules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules) can return `zero`, `one`, `two`, `few`, `many`, or `other`; a given locale and type need not use all six. The names are category labels, not numeric tests. In the variation, French `0` selects `one` and US English `0` selects `other`, so replacing the lookup with `count === 1` would change the French result.

## How do you use Intl.PluralRules for ordinals like 1st and 2nd?

Pass `{ type: 'ordinal' }` and use a separate table from the one you use for counts. In US English, `21` and `1` both select `one` as ranks; `11` selects `other`. For counts, `21` instead selects `other`. The full program prints the categories and finished strings side by side so you can see which rules produce each result. English suffixes don’t translate the label: use a template that matches the way the target language writes ranks.

## Which plural categories can a locale return?

Use `resolvedOptions().pluralCategories` to see the categories for that locale and type. Check that your table covers them, including `other`. OneEcma’s [Intl documentation](https://recursiveai.org/oneecma/docs/intl/) lists the supported locales and describes default-locale fallback. If the locale you request isn’t supported, choose a supported fallback and its matching message table together rather than using fallback rules with the original translation.

When a message includes a rounded number, make its display precision agree with the options you use for category selection. In the decimal variation, `select(1)` returns `other` under the US English rules with one required fraction digit. It returns `one` with the default options. The [PluralRules constructor options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules) determine the digits used for the plural calculation; they don’t format the visible number for you.
