forked from globalizejs/globalize
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Borrow santhoshtr/CLDRPluralRuleParser. Ref globalizejs#220
- Loading branch information
Showing
12 changed files
with
1,121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## .formatPlural( value, messageData ) | ||
|
||
It supports the creation of internationalized messages with plural inflection by | ||
returning the appropriate message based on value's plural group: `zero`, `one`, | ||
`two`, `few`, `many`, or `other`. | ||
|
||
### Parameters | ||
|
||
**value** | ||
|
||
A Number for which the plural message should be formatted for. | ||
|
||
**messageData** | ||
|
||
JSON object with message data. | ||
|
||
### Example | ||
|
||
```javascript | ||
var messageData = { | ||
one: "You have 1 unread message", | ||
other: "You have {0} unread messages" | ||
}; | ||
Globalize.locale( "en" ); | ||
Globalize.plural( 0, messageData ); // "You have {0} unread messages" | ||
Globalize.plural( 1, messageData ); // "You have 1 unread message" | ||
Globalize.plural( 2, messageData ); // "You have {0} unread messages" | ||
|
||
// FIXME | ||
|
||
messageData = { | ||
zero: | ||
one: | ||
two: | ||
few: | ||
many: | ||
other: | ||
}; | ||
Globalize( "ar" ).plural( 0, messageData ); // "zero" | ||
Globalize( "ar" ).plural( 1, messageData ); // "one" | ||
Globalize( "ar" ).plural( 2, messageData ); // "two" | ||
Globalize( "ar" ).plural( 3, messageData ); // "few" | ||
|
||
messageData = { | ||
few: | ||
many: | ||
one: | ||
other: | ||
}; | ||
Globalize( "ru" ).plural( 0, messageData ); // "many" | ||
Globalize( "ru" ).plural( 1, messageData ); // "one" | ||
Globalize( "ru" ).plural( 2, messageData ); // "few" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*! | ||
* Globalize v@VERSION | ||
* | ||
* http://github.com/jquery/globalize | ||
* | ||
* Copyright 2005, 2013 jQuery Foundation, Inc. and other contributors | ||
* Released under the MIT license | ||
* http://jquery.org/license | ||
* | ||
* Date: @DATE | ||
*/ | ||
(function( root, factory ) { | ||
|
||
// UMD returnExports | ||
if ( typeof define === "function" && define.amd ) { | ||
|
||
// AMD | ||
define([ | ||
"cldr", | ||
"../globalize" | ||
], factory ); | ||
} else if ( typeof exports === "object" ) { | ||
|
||
// Node, CommonJS | ||
module.exports = factory( require( "cldrjs" ), require( "globalize" ) ); | ||
} else { | ||
|
||
// Global | ||
factory( root.Cldr, root.Globalize ); | ||
} | ||
}(this, function( Cldr, Globalize ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
define([ | ||
"./core", | ||
"./plural/form", | ||
"./message" | ||
], function( Globalize, pluralForm ) { | ||
|
||
/** | ||
* Globalize.formatPlural( value, data ) | ||
* | ||
* @value [Number] | ||
* | ||
* @data [JSON] | ||
* | ||
* FIXME | ||
* | ||
* Return the appropriate message based on value's plural group: zero | one | two | few | many | other. | ||
*/ | ||
Globalize.formatPlural = | ||
Globalize.prototype.formatPlural = | ||
function( value, data ) { | ||
var form; | ||
|
||
if ( typeof value !== "number" ) { | ||
throw new Error( "Value is not a number" ); | ||
} | ||
|
||
if ( !( form = pluralForm( value, this.cldr ) ) ) { | ||
throw new Error( "Plural form not found!" ); | ||
} | ||
|
||
return data[ form ]; | ||
}; | ||
|
||
return Globalize; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
define([ | ||
"CLDRPluralRuleParser", | ||
"./rules" | ||
], function( CLDRPluralRuleParser, pluralRules ) { | ||
|
||
/** | ||
* pluralForm( value, cldr ) | ||
* | ||
* @value [Number] | ||
* | ||
* @cldr [Cldr instance]. | ||
* | ||
* Return the corresponding form (zero | one | two | few | many | other) of a value given locale @cldr. | ||
*/ | ||
return function( value, cldr ) { | ||
var form, | ||
rules = pluralRules( cldr ); | ||
|
||
for( form in rules ) { | ||
if( CLDRPluralRuleParser( rules[ form ], value ) ) { | ||
return form.replace( /pluralRule-count-/, "" ); | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
define(function() { | ||
|
||
/** | ||
* pluralRules( cldr ) | ||
* | ||
* @cldr [Cldr instance]. | ||
* | ||
* Return all the plural rules of a language. | ||
*/ | ||
return function( cldr ) { | ||
return cldr.supplemental( "plurals-type-cardinal/{language}" ); | ||
}; | ||
|
||
}); |
Oops, something went wrong.