Skip to content

Plural: Support ordinals #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ requirements. See table below.
| Currency module | cldr/main/`locale`/currencies.json<br>cldr/supplemental/currencyData.json<br>+CLDR JSON files from number module<br>+CLDR JSON files from plural module for name style support |
| Date module | cldr/main/`locale`/ca-gregorian.json<br>cldr/main/`locale`/timeZoneNames.json<br>cldr/supplemental/timeData.json<br>cldr/supplemental/weekData.json<br>+CLDR JSON files from number module |
| Number module | cldr/main/`locale`/numbers.json<br>cldr/supplemental/numberingSystems.json |
| Plural module | cldr/supplemental/plurals.json |
| Plural module | cldr/supplemental/plurals.json (for cardinals)<br>cldr/supplemental/ordinals.json (for ordinals) |

*(b) How am I supposed to get and load CLDR content?*

Expand Down Expand Up @@ -433,24 +433,32 @@ to you in different flavors):

### Plural module

- **`.pluralGenerator()`**
- **`.pluralGenerator( [options] )`**

Return a function that returns the value's corresponding plural group: `zero`,
`one`, `two`, `few`, `many`, or `other`.

The function may be used for cardinals or ordinals.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below, we need one example using ({ type: "ordinal" }).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

```javascript
.pluralGenerator()( 0 )
// > "other"

.pluralGenerator()( 1 )
// > "one"

.pluralGenerator({ type: "ordinal" })( 1 )
// > "one"

.pluralGenerator({ type: "ordinal" })( 2 )
// > "two"
```

[Read more...](doc/api/plural/plural-generator.md)

- **`.plural( value )`**
- **`.plural( value[, options ] )`**

Alias for `.pluralGenerator()( value )`.
Alias for `.pluralGenerator( [options] )( value )`.


## Error reference
Expand Down
41 changes: 38 additions & 3 deletions doc/api/plural/plural-generator.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## .pluralGenerator() ➜ function( value )
## .pluralGenerator( [options] ) ➜ function( value )

It supports the creation of internationalized messages with plural inflection by
returning a function that returns the value's plural group: `zero`, `one`,
Expand All @@ -9,13 +9,24 @@ to return the plural group.

### Parameters

**options** Optional

A JSON object including none or any of the following options.

> **type** Optional
>
> String `cardinal` (default), or `ordinal`.

**value**

A Number for which to return the plural group.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, could you move options above value? Also, replace >> with > only.

### Example

Prior to using any plural method, you must load `supplemental/plurals.json`.
Prior to using any plural method, you must load either
`supplemental/plurals.json` for cardinals or `supplemental/ordinals.json` for
ordinals.

Read [CLDR content][] if you need more information.

[CLDR content]: ../../../README.md#2-cldr-content
Expand All @@ -27,6 +38,8 @@ default locale.
var plural;

Globalize.locale( "en" );

// Cardinals
plural = Globalize.pluralGenerator();

plural( 0 );
Expand All @@ -37,6 +50,18 @@ plural( 1 );

plural( 2 );
// > "other"

// Ordinals
plural = Globalize.pluralGenerator({ type: "ordinal" });

plural( 0 );
// > "other"

plural( 1 );
// > "one"

plural( 2 );
// > "two"
```

You can use the instance method `.pluralGenerator()`, which uses the instance
Expand All @@ -49,7 +74,7 @@ plural( 1 );
// > "other"
```

For comparison:
For comparison (cardinals):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a similar table for ordinals?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


| | en (English) | ru (Russian) | ar (Arabic) |
| --- | --- | --- | --- |
Expand All @@ -58,3 +83,13 @@ For comparison:
| `plural( 2 )` | `other` | `few` | `two` |
| `plural( 3 )` | `other` | `few` | `few` |
| `plural( 5 )` | `other` | `many` | `few` |

For comparison (ordinals):

| | en (English) | ru (Russian) | ar (Arabic) |
| --- | --- | --- | --- |
| `plural( 0, { type: "ordinal" } )` | `other` | `other` | `other` |
| `plural( 1, { type: "ordinal" } )` | `one` | `other` | `other` |
| `plural( 2, { type: "ordinal" } )` | `two` | `other` | `other` |
| `plural( 3, { type: "ordinal" } )` | `few` | `other` | `other` |
| `plural( 5, { type: "ordinal" } )` | `other` | `other` | `other` |
3 changes: 2 additions & 1 deletion src/build/intro-plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
var validateCldr = Globalize._validateCldr,
validateDefaultLocale = Globalize._validateDefaultLocale,
validateParameterPresence = Globalize._validateParameterPresence,
validateParameterType = Globalize._validateParameterType;
validateParameterType = Globalize._validateParameterType,
validateParameterTypePlainObject = Globalize._validateParameterTypePlainObject;
14 changes: 14 additions & 0 deletions src/common/validate/parameter-type/plural-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define([
"../parameter-type"
], function( validateParameterType ) {

return function( value, name ) {
validateParameterType(
value,
name,
value === undefined || value === "cardinal" || value === "ordinal",
"String \"cardinal\" or \"ordinal\""
);
};

});
37 changes: 25 additions & 12 deletions src/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ define([
"./common/validate/parameter-presence",
"./common/validate/parameter-type",
"./common/validate/parameter-type/number",
"./common/validate/parameter-type/plain-object",
"./common/validate/parameter-type/plural-type",

"cldr/event",
"cldr/supplemental"
], function( Cldr, MakePlural, Globalize, validateCldr, validateDefaultLocale,
validateParameterPresence, validateParameterType, validateParameterTypeNumber ) {
validateParameterPresence, validateParameterType, validateParameterTypeNumber,
validateParameterTypePlainObject, validateParameterTypePluralType ) {

/**
* .plural( value )
Expand All @@ -22,14 +25,14 @@ define([
* value given locale.
*/
Globalize.plural =
Globalize.prototype.plural = function( value ) {
Globalize.prototype.plural = function( value, options ) {
validateParameterPresence( value, "value" );
validateParameterTypeNumber( value, "value" );
return this.pluralGenerator()( value );
return this.pluralGenerator( options )( value );
};

/**
* .pluralGenerator()
* .pluralGenerator( [options] )
*
* Return a plural function (of the form below).
*
Expand All @@ -41,23 +44,33 @@ Globalize.prototype.plural = function( value ) {
* default/instance locale.
*/
Globalize.pluralGenerator =
Globalize.prototype.pluralGenerator = function() {
var cldr, plural;
Globalize.prototype.pluralGenerator = function( options ) {
var cldr, isOrdinal, plural, type;

validateParameterTypePlainObject( options, "options" );

options = options || {};
type = options.type || "cardinal";
cldr = this.cldr;

validateParameterTypePluralType( options.type, "options.type" );

validateDefaultLocale( cldr );

isOrdinal = type === "ordinal";

cldr.on( "get", validateCldr );
cldr.supplemental( "plurals-type-cardinal/{language}" );
cldr.supplemental([ "plurals-type-" + type, "{language}" ]);
cldr.off( "get", validateCldr );

// Set CLDR data
MakePlural.rules = {
cardinal: cldr.supplemental( "plurals-type-cardinal" )
};
MakePlural.rules = {};
MakePlural.rules[ type ] = cldr.supplemental( "plurals-type-" + type );

plural = MakePlural( cldr.attributes.language, { "no_tests": true } );
plural = MakePlural( cldr.attributes.language, {
"no_tests": true,
"ordinals": isOrdinal,
"no_cardinals": isOrdinal
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this code needs simplification... What about the below?

  */
 Globalize.pluralGenerator =
 Globalize.prototype.pluralGenerator = function( options ) {
-   var cldr, plural, isOrdinal;
+   var cldr, isOrdinal, plural, type;

    validateParameterTypePlainObject( options, "options" );

    options = options || {};
+   type = options.type || "cardinal";
    cldr = this.cldr;

    validateParameterTypePluralType( options.type, "options.type" );
@@ -58,20 +59,11 @@ Globalize.prototype.pluralGenerator = function( options ) {

    isOrdinal = options.type === "ordinal";

-   cldr.on( "get", validateCldr );
-   if ( isOrdinal ) {
-       cldr.supplemental( "plurals-type-ordinal/{language}" );
-   } else {
-       cldr.supplemental( "plurals-type-cardinal/{language}" );
-   }
-   cldr.off( "get", validateCldr );
+   cldr.once( "get", validateCldr );
+   cldr.supplemental([ "plurals-type-" + type, "{language}" ]);

    MakePlural.rules = {};
-   if ( isOrdinal ) {
-       MakePlural.rules.ordinal = cldr.supplemental( "plurals-type-ordinal" );
-   } else {
-       MakePlural.rules.cardinal = cldr.supplemental( "plurals-type-cardinal" );
-   }
+   MakePlural.rules[ type ] = cldr.supplemental( "plurals-type-" + type );

    plural = MakePlural( cldr.attributes.language, {
        "no_tests": true,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return function( value ) {
validateParameterPresence( value, "value" );
Expand Down
22 changes: 21 additions & 1 deletion test/functional/plural/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ define([
"globalize",
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/plurals.json",
"json!cldr-data/supplemental/ordinals.json",
"../../util",

"globalize/plural"
], function( Globalize, likelySubtags, plurals, util ) {
], function( Globalize, likelySubtags, plurals, ordinals, util ) {

function extraSetup() {
Globalize.load( plurals );
Globalize.load( ordinals );

// Temporary fix due to CLDR v26 regression about pt_BR plural
// http://unicode.org/cldr/trac/ticket/7178
Expand Down Expand Up @@ -47,6 +49,12 @@ QUnit.test( "should validate parameters", function( assert ) {
Globalize.plural( invalidValue );
};
});

util.assertPlainObjectParameter( assert, "options", function( invalidOptions ) {
return function() {
Globalize.plural( 0, invalidOptions );
};
});
});

QUnit.test( "should validate CLDR content", function( assert ) {
Expand All @@ -67,6 +75,14 @@ QUnit.test( "should return plural form", function( assert ) {
assert.equal( Globalize( "en" ).plural( 0.14 ), "other" );
assert.equal( Globalize( "en" ).plural( 3.14 ), "other" );

assert.equal( Globalize( "en" ).plural( 0, { type: "ordinal" } ), "other" );
assert.equal( Globalize( "en" ).plural( 1, { type: "ordinal" } ), "one" );
assert.equal( Globalize( "en" ).plural( 2, { type: "ordinal" } ), "two" );
assert.equal( Globalize( "en" ).plural( 3, { type: "ordinal" } ), "few" );
assert.equal( Globalize( "en" ).plural( 1412, { type: "ordinal" } ), "other" );
assert.equal( Globalize( "en" ).plural( 0.14, { type: "ordinal" } ), "other" );
assert.equal( Globalize( "en" ).plural( 3.14, { type: "ordinal" } ), "other" );

assert.equal( Globalize( "ar" ).plural( 0 ), "zero" );
assert.equal( Globalize( "ar" ).plural( 1 ), "one" );
assert.equal( Globalize( "ar" ).plural( 2 ), "two" );
Expand All @@ -87,6 +103,10 @@ QUnit.test( "should return plural form", function( assert ) {
assert.equal( Globalize( "ar" ).plural( 199 ), "many" );
assert.equal( Globalize( "ar" ).plural( 3.14 ), "other" );

[ 0, 1, 2, 3, 9, 10, 11, 99, 100, 101, 3.14 ].forEach(function( value ) {
assert.equal( Globalize( "ar" ).plural( value, { type: "ordinal" } ), "other" );
});

assert.equal( Globalize( "ja" ).plural( 0 ), "other" );
assert.equal( Globalize( "ja" ).plural( 1 ), "other" );
assert.equal( Globalize( "ja" ).plural( 2 ), "other" );
Expand Down