Skip to content

Commit

Permalink
Improve plural rules top level intro
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee committed Jul 25, 2023
1 parent f4dec9d commit 19e9b37
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ The **`Intl.PluralRules`** object enables plural-sensitive formatting and plural
## Concepts and usage

Languages use different patterns for expressing both plural numbers of items (cardinal numbers) and for expressing the order of items (ordinal numbers).
The plural rules methods [`PluralRules.select()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/select) and [`PluralRules.selectRange()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/selectRange) return tags that represent the form of a particular cardinal or ordinal number (or range) in a specified language.
English has two forms for expressing cardinal numbers: one for the singular "item" (1 hour, 1 dog, 1 fish) and the other for zero or any other number of "items" (0 hours, 2 lemmings, 100000.5 fish), while Chinese has only one form, and Arabic has six!
Similarly, English has four forms for ordinal numbers: "th", "st", "nd", "rd", giving the sequence: 0th, 1st, 2nd, 3rd, 4th, 5th, ..., 21st, 22nd, 23rd, 24th, 25th, and so on, while both Chinese and Arabic only have one form for ordinal numbers.

In English there are two forms for expressing cardinal numbers: one for the singular "item" (1 hour, 1 dog, 1 fish) and the other for zero or any other number of "items" (0 hours, 2 lemmings, 100000.5 fish).
The `PluralRules.select()` method returns the tag `"one"` for the singular case, and `"other"` for all other cardinal numbers.
Code can use the returned tag to format strings appropriately.
For example: "1 dog _is_ happy" and "10 dogs _are_ happy", or "1 fish _is_ golden" and "10 fish _are_ golden".
The plural rules methods [`PluralRules.select()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/select) and [`PluralRules.selectRange()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/selectRange) return a _tag_ that represents the plural form of a particular cardinal or ordinal number, or range, for a particular language and set of formatting options.
Code can use the returned tags to format strings appropriately for the given language and number/range.

For ordinal numbers English has four forms: "th", "st", "nd", "rd", giving the sequence: 0th, 1st, 2nd, 3rd, 4th, 5th, ..., 21st, 22nd, 23rd, 24th, 25th, and so on.
Again, `PluralRules.select()` returns tags to represent each of the allowed forms: `one` for "st" numbers (1, 21, 31, ...), `two` for "nd" numbers (2, 22, 32, ...), `few` for "rd" numbers (3, 33, 43, ...), and `other` for "th" numbers (0, 4-20, etc.).

Other languages have their own rules and forms.
Chinese has only one form for expressing cardinal numbers and another for ordinal numbers: in both cases `ther` is returned.
Arabic has six forms for cardinal numbers and only one for ordinal numbers.
The full set of tags that might be returned are: `zero`, `one`, `two`, `few`, `many`, `other` (the "general" plural form, used if the language only has one form).
As English only has two forms for cardinal numbers, the `PluralRules.select()` method return only two tags: `"one"` for the singular case, and `"other"` for all other cardinal numbers.
This allows constructions of sentences that make sense in English: "1 dog _is_ happy" and "10 dogs _are_ happy", or "1 fish _is_ golden" and "10 fish _are_ golden".

`PluralRules.select()` can return any of four tags for ordinal numbers in English, representing each of the allowed forms: `one` for "st" numbers (1, 21, 31, ...), `two` for "nd" numbers (2, 22, 32, ...), `few` for "rd" numbers (3, 33, 43, ...), and `other` for "th" numbers (0, 4-20, etc.).
Again, the returned tags allow appropriate formatting of strings describing an ordinal number.

For more information about the rules and how they are used see [Plural Rules](https://cldr.unicode.org/index/cldr-spec/plural-rules).
For a list of the rules and how they apply for different languages see the [LDML Language Plural Rules](https://www.unicode.org/cldr/charts/43/supplemental/language_plural_rules.html).
Expand Down Expand Up @@ -62,21 +60,72 @@ These properties are defined on `Intl.PluralRules.prototype` and shared by all `

### Using locales

This example shows some of the variations in localized plural rules. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the [constructor `locales`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules#locales) argument:
This example shows some of the variations in localized plural rules for cardinal numbers.

In order to get the format for the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the [constructor `locales`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules#locales) argument:

```js
// US English
const enCardinalRules = new Intl.PluralRules("en-US");
console.log(enCardinalRules.select(0)); // > "other"
console.log(enCardinalRules.select(1)); // > "one"
console.log(enCardinalRules.select(2)); // > "other"
console.log(enCardinalRules.select(3)); // > "other"

// Arabic
const arCardinalRules = new Intl.PluralRules("ar-EG");
console.log(arCardinalRules.select(0)); // > "zero"
console.log(arCardinalRules.select(1)); // > "one"
console.log(arCardinalRules.select(2)); // > "two"
console.log(arCardinalRules.select(6)); // > "few"
console.log(arCardinalRules.select(18)); // > "many"
```

### Using options

The plural form of the specified number may also depend on [constructor `options`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules#locales), such as how the number is rounded, and whether it is cardinal or ordinal.

This example shows how you can set the type of rules to "ordinal", and how this affects the form for some numbers in US English.

```js
// US English - ordinal
const enOrdinalRules = new Intl.PluralRules("en-US", { type: "ordinal" });
console.log(enOrdinalRules.select(0)); // > "other" (0th)
console.log(enOrdinalRules.select(1)); // > "one" (1st)
console.log(enOrdinalRules.select(2)); // > "two" (2nd)
console.log(enOrdinalRules.select(3)); // > "few" (3rd)
console.log(enOrdinalRules.select(4)); // > "other" (4th)
console.log(enOrdinalRules.select(21)); // > "one" (21st)
```

### Formatting text using the returned tag

The code below extends the previous example, showing how you might use the returned tag for an ordinal number to format text in English.

```js
// Arabic has different plural rules

new Intl.PluralRules("ar-EG").select(0);
// → 'zero'
new Intl.PluralRules("ar-EG").select(1);
// → 'one'
new Intl.PluralRules("ar-EG").select(2);
// → 'two'
new Intl.PluralRules("ar-EG").select(6);
// → 'few'
new Intl.PluralRules("ar-EG").select(18);
// → 'many'
const enOrdinalRules = new Intl.PluralRules("en-US", { type: "ordinal" });

const suffixes = new Map([
["one", "st"],
["two", "nd"],
["few", "rd"],
["other", "th"],
]);
const formatOrdinals = (n) => {
const rule = enOrdinalRules.select(n);
const suffix = suffixes.get(rule);
return `${n}${suffix}`;
};

formatOrdinals(0); // '0th'
formatOrdinals(1); // '1st'
formatOrdinals(2); // '2nd'
formatOrdinals(3); // '3rd'
formatOrdinals(4); // '4th'
formatOrdinals(11); // '11th'
formatOrdinals(21); // '21st'
formatOrdinals(42); // '42nd'
formatOrdinals(103); // '103rd'
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This function selects a pluralization category according to the locale and forma

Conceptually the behavior is the same as getting plural rules for a single cardinal or ordinal number.
Languages have one or more forms for describing ranges, and this method returns the appropriate form given the supplied locale and formatting options.
In English there is only one plural form, such a "1-10 apples", and the method will return `other`
In English there is only one plural form, such a "1-10 apples", and the method will return `other`.
Other languages can have many forms.

## Examples
Expand Down

0 comments on commit 19e9b37

Please sign in to comment.