Skip to content

Commit

Permalink
Add soem mroe rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee committed Jul 24, 2023
1 parent fddce65 commit f4dec9d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ browser-compat: javascript.builtins.Intl.PluralRules

The **`Intl.PluralRules`** object enables plural-sensitive formatting and plural-related language rules.

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

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".

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).

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).

## Constructor

- {{jsxref("Intl/PluralRules/PluralRules", "Intl.PluralRules()")}}
Expand Down Expand Up @@ -41,7 +62,7 @@ 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 `locales` argument:
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:

```js
// Arabic has different plural rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ The **`resolvedOptions()`** method of {{jsxref("Intl.PluralRules")}} instances r
resolvedOptions()
```

### Return value
### Parameters

None.

A new object with properties reflecting the locale and plural formatting options
computed during the initialization of the given {{jsxref("Intl.PluralRules")}} object.
### Return value

## Description
A new object with properties reflecting the locale and plural formatting options computed during the initialization of the given {{jsxref("Intl.PluralRules")}} object.

The resulting object has the following properties:
The object has the following properties:

- `locale`
- : The BCP 47 language tag for the locale actually used. If any Unicode extension values were requested in the input BCP 47 language tag that led to this locale, the key-value pairs that were requested and are supported for this locale are included in `locale`.
Expand Down Expand Up @@ -60,6 +61,8 @@ Only one of the following two groups of properties is included:

### Using the resolvedOptions() method

The code below shows the construction of a `PluralRules` object, followed by logging of each of the resolved options.

```js
// Create a PluralRules instance
const de = new Intl.PluralRules("de-DE", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ select(number)

### Return value

A string representing the pluralization category of the `number`, can be one
of `zero`, `one`, `two`, `few`,
`many` or `other`.
A string representing the pluralization category of the `number`.
This can be one of `zero`, `one`, `two`, `few`, `many` or `other`.

## Description

This function selects a pluralization category according to the locale and formatting
options of a {{jsxref("Intl.PluralRules")}} object.
This function selects a pluralization category according to the locale and formatting options of a {{jsxref("Intl.PluralRules")}} object.
These options are set in the [`PluralRules` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules).

## Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ selectRange(startRange, endRange)

### Return value

A string representing the pluralization category of the `number`.
A string representing the pluralization category of the specified range.
This can be one of `zero`, `one`, `two`, `few`, `many` or `other`, that are relevant for the locale whose localization is specified in [LDML Language Plural Rules](https://www.unicode.org/cldr/charts/43/supplemental/language_plural_rules.html).

## Description

This function selects a pluralization category according to the locale and formatting options of an {{jsxref("Intl.PluralRules")}} object.

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`
Other languages can have many forms.

## Examples

### Using selectRange()
Expand Down

0 comments on commit f4dec9d

Please sign in to comment.