-
Notifications
You must be signed in to change notification settings - Fork 595
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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`, | ||
|
@@ -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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, could you move options above value? Also, replace |
||
### 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 | ||
|
@@ -27,6 +38,8 @@ default locale. | |
var plural; | ||
|
||
Globalize.locale( "en" ); | ||
|
||
// Cardinals | ||
plural = Globalize.pluralGenerator(); | ||
|
||
plural( 0 ); | ||
|
@@ -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 | ||
|
@@ -49,7 +74,7 @@ plural( 1 ); | |
// > "other" | ||
``` | ||
|
||
For comparison: | ||
For comparison (cardinals): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's create a similar table for ordinals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
| | en (English) | ru (Russian) | ar (Arabic) | | ||
| --- | --- | --- | --- | | ||
|
@@ -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` | |
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\"" | ||
); | ||
}; | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) | ||
|
@@ -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). | ||
* | ||
|
@@ -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 | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
return function( value ) { | ||
validateParameterPresence( value, "value" ); | ||
|
There was a problem hiding this comment.
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" }).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍