-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathplural.js
90 lines (73 loc) · 2.15 KB
/
plural.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
define([
"cldr",
"make-plural",
"./common/runtime-bind",
"./common/validate/cldr",
"./common/validate/default-locale",
"./common/validate/parameter-presence",
"./common/validate/parameter-type/number",
"./common/validate/parameter-type/plain-object",
"./common/validate/parameter-type/plural-type",
"./core",
"./plural/generator-fn",
"cldr/event",
"cldr/supplemental"
], function( _Cldr, MakePlural, runtimeBind, validateCldr, validateDefaultLocale,
validateParameterPresence, validateParameterTypeNumber,
validateParameterTypePlainObject, validateParameterTypePluralType, Globalize,
pluralGeneratorFn ) {
/**
* .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, options ) {
validateParameterPresence( value, "value" );
validateParameterTypeNumber( value, "value" );
return this.pluralGenerator( options )( value );
};
/**
* .pluralGenerator( [options] )
*
* Return a plural function (of the form below).
*
* fn( value )
*
* @value [Number]
*
* Return the corresponding form (zero | one | two | few | many | other) of a value given the
* default/instance locale.
*/
Globalize.pluralGenerator =
Globalize.prototype.pluralGenerator = function( options ) {
var args, cldr, isOrdinal, plural, returnFn, type;
validateParameterTypePlainObject( options, "options" );
options = options || {};
cldr = this.cldr;
args = [ options ];
type = options.type || "cardinal";
validateParameterTypePluralType( options.type, "options.type" );
validateDefaultLocale( cldr );
isOrdinal = type === "ordinal";
cldr.on( "get", validateCldr );
try {
cldr.supplemental([ "plurals-type-" + type, "{language}" ]);
} finally {
cldr.off( "get", validateCldr );
}
MakePlural.rules = {};
MakePlural.rules[ type ] = cldr.supplemental( "plurals-type-" + type );
plural = new MakePlural( cldr.attributes.language, {
"ordinals": isOrdinal,
"cardinals": !isOrdinal
});
returnFn = pluralGeneratorFn( plural );
runtimeBind( args, cldr, returnFn, [ plural ] );
return returnFn;
};
return Globalize;
});