Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneEcma

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, then dotnet run.

Prints:

Text
2 files
2nd

The full program

C#
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:

C#
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:

C#
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

The short answer

Call 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 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 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 determine the digits used for the plural calculation; they don’t format the visible number for you.

All oneecma recipes