From f4dec9d37297a59444fec514b33db4202f2d2316 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Mon, 24 Jul 2023 18:40:04 +1000 Subject: [PATCH] Add soem mroe rules --- .../global_objects/intl/pluralrules/index.md | 23 ++++++++++++++++++- .../intl/pluralrules/resolvedoptions/index.md | 13 +++++++---- .../intl/pluralrules/select/index.md | 9 ++++---- .../intl/pluralrules/selectrange/index.md | 7 +++++- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/index.md b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/index.md index 7fdc85692e0857f..7b50ddce503f886 100644 --- a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/index.md +++ b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/index.md @@ -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()")}} @@ -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 diff --git a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/resolvedoptions/index.md b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/resolvedoptions/index.md index f7175dc53f5334a..1685a90b99676e4 100644 --- a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/resolvedoptions/index.md +++ b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/resolvedoptions/index.md @@ -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`. @@ -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", { diff --git a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/select/index.md b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/select/index.md index c2cdcadda50d136..5e61de1b69cc985 100644 --- a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/select/index.md +++ b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/select/index.md @@ -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 diff --git a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/selectrange/index.md b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/selectrange/index.md index ad608d3adf88542..d2bff7906d08c9e 100644 --- a/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/selectrange/index.md +++ b/files/en-us/web/javascript/reference/global_objects/intl/pluralrules/selectrange/index.md @@ -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()