| @@ -0,0 +1,40 @@ | ||
| /*! | ||
| * Globalize v@VERSION | ||
| * | ||
| * http://github.com/jquery/globalize | ||
| * | ||
| * Copyright 2010, 2014 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", | ||
| "cldr/event" | ||
| ], 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 ) { | ||
|
|
||
| var formatMessage = Globalize._formatMessage, | ||
| validate = Globalize._validate, | ||
| validateCldr = Globalize._validateCldr, | ||
| validateDefaultLocale = Globalize._validateDefaultLocale, | ||
| validatePresence = Globalize._validatePresence, | ||
| validateType = Globalize._validateType, | ||
| validateTypePlainObject = Globalize._validateTypePlainObject; |
| @@ -0,0 +1,9 @@ | ||
| define([ | ||
| "../type" | ||
| ], function( validateType ) { | ||
|
|
||
| return function( value, name ) { | ||
| validateType( value, name, typeof value === "undefined" || typeof value === "string" || typeof value === "number", "String or Number" ); | ||
| }; | ||
|
|
||
| }); |
| @@ -0,0 +1,73 @@ | ||
| define([ | ||
| "./core", | ||
| "./common/validate", | ||
| "./common/validate/cldr", | ||
| "./common/validate/default-locale", | ||
| "./common/validate/presence", | ||
| "./common/validate/type/number", | ||
| "./common/validate/type/plain-object", | ||
| "./common/validate/type/plural-format-value", | ||
| "./plural/form", | ||
| "./common/format-message" | ||
| ], function( Globalize, validate, validateCldr, validateDefaultLocale, validatePresence, validateTypeNumber, validateTypePlainObject, validateTypePluralFormatValue, pluralForm, formatMessage ) { | ||
|
|
||
| /** | ||
| * .formatPlural( value, data, formatValue ) | ||
| * | ||
| * @value [Number] | ||
| * | ||
| * @data [JSON] eg. { one: "{0} second", other: "{0} seconds" } | ||
| * | ||
| * @formatValue [String|Number] It defaults to `value`. It's used to replace the | ||
| * {0} variable of plural messages. | ||
| * | ||
| * Return the appropriate message based on value's plural group: zero | one | | ||
| * two | few | many | other. | ||
| */ | ||
| Globalize.formatPlural = | ||
| Globalize.prototype.formatPlural = function( value, data, formatValue ) { | ||
|
|
||
| // Note: validateTypeNumber( value, "value" ) is deferred to this.plural(). | ||
| validatePresence( value, "value" ); | ||
| validatePresence( data, "data" ); | ||
| validateTypePlainObject( data, "data" ); | ||
| validateTypePluralFormatValue( formatValue, "formatValue" ); | ||
|
|
||
| formatValue = formatValue || value; | ||
|
|
||
| return formatMessage( data[ this.plural( value ) ], [ formatValue ] ); | ||
| }; | ||
|
|
||
| /** | ||
| * .plural( value ) | ||
| * | ||
| * @value [Number] | ||
| * | ||
| * Return the corresponding form (zero | one | two | few | many | other) of a | ||
| * value given locale. | ||
| */ | ||
| Globalize.plural = | ||
| Globalize.prototype.plural = function( value ) { | ||
| var cldr, form; | ||
|
|
||
| validatePresence( value, "value" ); | ||
| validateTypeNumber( value, "value" ); | ||
|
|
||
| cldr = this.cldr; | ||
|
|
||
| validateDefaultLocale( cldr ); | ||
|
|
||
| cldr.on( "get", validateCldr ); | ||
| form = pluralForm( value, cldr ); | ||
| cldr.off( "get", validateCldr ); | ||
|
|
||
| validate( "E_PLURAL_FORM_NOT_FOUND", "Plural form not found for value `{value}`", typeof form === "string", { | ||
| value: value | ||
| }); | ||
|
|
||
| return form; | ||
| }; | ||
|
|
||
| return Globalize; | ||
|
|
||
| }); |
| @@ -0,0 +1,28 @@ | ||
| define([ | ||
| "CLDRPluralRuleParser" | ||
| ], function( CLDRPluralRuleParser ) { | ||
|
|
||
| /** | ||
| * 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 = cldr.supplemental( "plurals-type-cardinal/{language}" ); | ||
|
|
||
| for( form in rules ) { | ||
| if( CLDRPluralRuleParser( rules[ form ], value ) ) { | ||
| return form.replace( /pluralRule-count-/, "" ); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| }); |
| @@ -0,0 +1,73 @@ | ||
| define([ | ||
| "globalize", | ||
| "json!fixtures/cldr/supplemental/likelySubtags.json", | ||
| "json!fixtures/cldr/supplemental/plurals.json", | ||
| "../../util", | ||
| "globalize/plural" | ||
| ], function( Globalize, likelySubtags, plurals, util ) { | ||
|
|
||
| function extraSetup() { | ||
| Globalize.load( plurals ); | ||
| } | ||
|
|
||
| QUnit.module( ".formatPlural()", { | ||
| setup: function() { | ||
| Globalize.load( likelySubtags ); | ||
| Globalize.locale( "en" ); | ||
| }, | ||
| teardown: util.resetCldrContent | ||
| }); | ||
|
|
||
| QUnit.test( "should validate parameters", function( assert ) { | ||
| util.assertParameterPresence( assert, "value", function() { | ||
| Globalize.formatPlural(); | ||
| }); | ||
|
|
||
| util.assertNumberParameter( assert, "value", function( invalidValue ) { | ||
| return function() { | ||
| Globalize.formatPlural( invalidValue, { foo: "bar" } ); | ||
| }; | ||
| }); | ||
|
|
||
| util.assertParameterPresence( assert, "data", function() { | ||
| Globalize.formatPlural( 1 ); | ||
| }); | ||
|
|
||
| util.assertPlainObjectParameter( assert, "data", function( invalidValue ) { | ||
| return function() { | ||
| Globalize.formatPlural( 1, invalidValue ); | ||
| }; | ||
| }); | ||
|
|
||
| util.assertPluralFormatValueParameter( assert, "formatValue", function( invalidValue ) { | ||
| return function() { | ||
| Globalize.formatPlural( 1, { foo: "bar" }, invalidValue ); | ||
| }; | ||
| }); | ||
| }); | ||
|
|
||
| QUnit.test( "should validate CLDR content", function( assert ) { | ||
| util.assertCldrContent( assert, function() { | ||
| Globalize.formatPlural( 1, { foo: "bar" } ); | ||
| }); | ||
| }); | ||
|
|
||
| QUnit.test( "should return the appropriate message based on plural form", function( assert ) { | ||
| var message = { | ||
| zero: "nothing", | ||
| one: "one kitty ({0})", | ||
| few: "some kitties ({0})", | ||
| many: "lots of kitties ({0})", | ||
| other: "{0} kitties" | ||
| }; | ||
|
|
||
| extraSetup(); | ||
|
|
||
| assert.equal( Globalize.formatPlural( 0, message ), "0 kitties" ); | ||
| assert.equal( Globalize.formatPlural( 0, message, "no" ), "no kitties" ); | ||
| assert.equal( Globalize.formatPlural( 5, message ), "5 kitties" ); | ||
| assert.equal( Globalize( "ar" ).formatPlural( 0, message ), "nothing" ); | ||
| }); | ||
|
|
||
|
|
||
| }); |
| @@ -0,0 +1,46 @@ | ||
| define([ | ||
| "globalize", | ||
| "json!fixtures/cldr/supplemental/likelySubtags.json", | ||
| "json!fixtures/cldr/supplemental/plurals.json", | ||
| "../../util", | ||
| "globalize/plural" | ||
| ], function( Globalize, likelySubtags, plurals, util ) { | ||
|
|
||
| function extraSetup() { | ||
| Globalize.load( plurals ); | ||
| } | ||
|
|
||
| QUnit.module( ".plural()", { | ||
| setup: function() { | ||
| Globalize.load( likelySubtags ); | ||
| Globalize.locale( "en" ); | ||
| }, | ||
| teardown: util.resetCldrContent | ||
| }); | ||
|
|
||
| QUnit.test( "should validate parameters", function( assert ) { | ||
| util.assertParameterPresence( assert, "value", function() { | ||
| Globalize.plural(); | ||
| }); | ||
|
|
||
| util.assertNumberParameter( assert, "value", function( invalidValue ) { | ||
| return function() { | ||
| Globalize.plural( invalidValue ); | ||
| }; | ||
| }); | ||
| }); | ||
|
|
||
| QUnit.test( "should validate CLDR content", function( assert ) { | ||
| util.assertCldrContent( assert, function() { | ||
| Globalize.plural( 1 ); | ||
| }); | ||
| }); | ||
|
|
||
| QUnit.test( "should return plural form", function( assert ) { | ||
| extraSetup(); | ||
| assert.equal( Globalize.plural( 0 ), "other" ); | ||
| assert.equal( Globalize( "ar" ).plural( 0 ), "zero" ); | ||
| }); | ||
|
|
||
|
|
||
| }); |
| @@ -0,0 +1,83 @@ | ||
| define([ | ||
| "cldr", | ||
| "src/plural/form", | ||
| "json!fixtures/cldr/supplemental/likelySubtags.json", | ||
| "json!fixtures/cldr/supplemental/plurals.json" | ||
| ], function( Cldr, pluralForm, likelySubtags, plurals ) { | ||
|
|
||
| var ar, en, ja, pt, ru, zh; | ||
|
|
||
| Cldr.load( likelySubtags ); | ||
| Cldr.load( plurals ); | ||
|
|
||
| ar = new Cldr( "ar" ); | ||
| en = new Cldr( "en" ); | ||
| ja = new Cldr( "ja" ); | ||
| pt = new Cldr( "pt" ); | ||
| ru = new Cldr( "ru" ); | ||
| zh = new Cldr( "zh" ); | ||
|
|
||
| QUnit.module( "Plural Form" ); | ||
|
|
||
| QUnit.test( "should return plural form of different locales", function( assert ) { | ||
| assert.equal( pluralForm( 0, en ), "other" ); | ||
| assert.equal( pluralForm( 1, en ), "one" ); | ||
| assert.equal( pluralForm( 2, en ), "other" ); | ||
| assert.equal( pluralForm( 1412, en ), "other" ); | ||
| assert.equal( pluralForm( 0.14, en ), "other" ); | ||
| assert.equal( pluralForm( 3.14, en ), "other" ); | ||
|
|
||
| assert.equal( pluralForm( 0, ar ), "zero" ); | ||
| assert.equal( pluralForm( 1, ar ), "one" ); | ||
| assert.equal( pluralForm( 2, ar ), "two" ); | ||
| assert.equal( pluralForm( 3, ar ), "few" ); | ||
| assert.equal( pluralForm( 6, ar ), "few" ); | ||
| assert.equal( pluralForm( 9, ar ), "few" ); | ||
| assert.equal( pluralForm( 10, ar ), "few" ); | ||
| assert.equal( pluralForm( 11, ar ), "many" ); | ||
| assert.equal( pluralForm( 15, ar ), "many" ); | ||
| assert.equal( pluralForm( 21, ar ), "many" ); | ||
| assert.equal( pluralForm( 70, ar ), "many" ); | ||
| assert.equal( pluralForm( 99, ar ), "many" ); | ||
| assert.equal( pluralForm( 100, ar ), "other" ); | ||
| assert.equal( pluralForm( 101, ar ), "other" ); | ||
| assert.equal( pluralForm( 102, ar ), "other" ); | ||
| assert.equal( pluralForm( 103, ar ), "few" ); | ||
| assert.equal( pluralForm( 111, ar ), "many" ); | ||
| assert.equal( pluralForm( 199, ar ), "many" ); | ||
| assert.equal( pluralForm( 3.14, ar ), "few" ); | ||
|
|
||
| assert.equal( pluralForm( 0, ja ), "other" ); | ||
| assert.equal( pluralForm( 1, ja ), "other" ); | ||
| assert.equal( pluralForm( 2, ja ), "other" ); | ||
| assert.equal( pluralForm( 3.14, ja ), "other" ); | ||
|
|
||
| assert.equal( pluralForm( 0, pt ), "other" ); | ||
| assert.equal( pluralForm( 1, pt ), "one" ); | ||
| assert.equal( pluralForm( 2, pt ), "other" ); | ||
| assert.equal( pluralForm( 0.1, pt ), "one" ); | ||
| assert.equal( pluralForm( 3.14, pt ), "other" ); | ||
|
|
||
| assert.equal( pluralForm( 0, ru ), "many" ); | ||
| assert.equal( pluralForm( 1, ru ), "one" ); | ||
| assert.equal( pluralForm( 2, ru ), "few" ); | ||
| assert.equal( pluralForm( 3, ru ), "few" ); | ||
| assert.equal( pluralForm( 4, ru ), "few" ); | ||
| assert.equal( pluralForm( 5, ru ), "many" ); | ||
| assert.equal( pluralForm( 6, ru ), "many" ); | ||
| assert.equal( pluralForm( 9, ru ), "many" ); | ||
| assert.equal( pluralForm( 11, ru ), "many" ); | ||
| assert.equal( pluralForm( 12, ru ), "many" ); | ||
| assert.equal( pluralForm( 19, ru ), "many" ); | ||
| assert.equal( pluralForm( 21, ru ), "one" ); | ||
| assert.equal( pluralForm( 22, ru ), "few" ); | ||
| assert.equal( pluralForm( 29, ru ), "many" ); | ||
| assert.equal( pluralForm( 3.14, ru ), "other" ); | ||
|
|
||
| assert.equal( pluralForm( 0, zh ), "other" ); | ||
| assert.equal( pluralForm( 1, zh ), "other" ); | ||
| assert.equal( pluralForm( 2, zh ), "other" ); | ||
| assert.equal( pluralForm( 3.14, zh ), "other" ); | ||
| }); | ||
|
|
||
| }); |