Skip to content

Commit 00bafc3

Browse files
committed
Only compile required plural generators
1 parent 4c95d94 commit 00bafc3

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

src/message.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,26 @@ Globalize.prototype.messageFormatter = function( path ) {
8484
}
8585
validateMessageType( path, message );
8686

87-
// Is plural module present? Yes, use its generator. Nope, use an error generator.
88-
pluralGenerator = this.plural !== undefined ?
89-
this.pluralGenerator( { type: "both" } ) :
90-
createErrorPluralModulePresence;
91-
9287
var compiler = new messageCompiler( this, Globalize._messageFmts );
9388
var formatterSrc = compiler
94-
.compile( message, cldr.locale, pluralGenerator );
89+
.compile( message, cldr.locale );
90+
91+
var pluralType = false;
92+
if ( compiler.pluralTypes.cardinal && compiler.pluralTypes.ordinal ) {
93+
pluralType = "both";
94+
} else if ( compiler.pluralTypes.cardinal ) {
95+
pluralType = "cardinal";
96+
} else if ( compiler.pluralTypes.ordinal ) {
97+
pluralType = "ordinal";
98+
}
99+
100+
if ( pluralType !== false ) {
101+
102+
// Is plural module present? Yes, use its generator. Nope, use an error generator.
103+
pluralGenerator = this.plural !== undefined ?
104+
this.pluralGenerator( { type: pluralType } ) :
105+
createErrorPluralModulePresence;
106+
}
95107

96108
var runtime = new messageFormatterRuntime( compiler.strictNumberSign );
97109

@@ -104,15 +116,20 @@ Globalize.prototype.messageFormatter = function( path ) {
104116

105117
returnFn = messageFormatterFn( formatter );
106118

107-
runtimeBind( args, cldr, returnFn,
108-
[
109-
messageFormatterRuntimeBind(
110-
cldr, formatter, compiler.runtime, compiler.locales, compiler.formatters
111-
),
112-
pluralGenerator
113-
].concat(
114-
compiler.formatters
115-
));
119+
var runtimeArgs = [
120+
messageFormatterRuntimeBind(
121+
cldr, formatter, compiler.runtime, pluralType, cldr.locale, compiler.formatters
122+
)
123+
];
124+
125+
if ( pluralGenerator ) {
126+
runtimeArgs.push( pluralGenerator );
127+
}
128+
runtimeArgs = runtimeArgs.concat(
129+
compiler.formatters
130+
);
131+
132+
runtimeBind( args, cldr, returnFn, runtimeArgs );
116133

117134
return returnFn;
118135
};

src/message/compiler.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ define([
99
* @class
1010
* @param {object} context - Context (this) for formatter functions
1111
* @param {array} fmtFuncs - Formatter functions
12-
* @property {object} locales - The locale identifiers used by the compiled functions
1312
* @property {object} runtime - Names of the core runtime functions used by the compiled functions
1413
* @property {array} formatters - The formatter functions used by the compiled functions
14+
* @property {object} pluralTypes - The plural types used by the compiled functions
1515
*/
1616
function Compiler( context, fmtFuncs ) {
1717
this._globalize = Globalize;
1818
this.fmt = fmtFuncs;
19-
this.locales = {};
2019
this.runtime = {};
20+
this.pluralTypes = {
21+
ordinal: false,
22+
cardinal: false
23+
};
2124
this.formatters = [];
2225
this.context = context;
2326
}
@@ -136,8 +139,8 @@ Compiler.prototype.token = function( token, plural ) {
136139
case "selectordinal":
137140
fn = "plural";
138141
args.push( 0, Compiler.funcname( this.lc ), this.cases( token, token ), 1 );
139-
this.locales[ this.lc ] = true;
140142
this.runtime.plural = true;
143+
this.pluralTypes.ordinal = true;
141144
break;
142145

143146
case "plural":
@@ -146,8 +149,8 @@ Compiler.prototype.token = function( token, plural ) {
146149
token.offset || 0,
147150
Compiler.funcname( this.lc ), this.cases( token, token )
148151
);
149-
this.locales[ this.lc ] = true;
150152
this.runtime.plural = true;
153+
this.pluralTypes.cardinal = true;
151154
break;
152155

153156
case "function":

src/message/formatter-runtime-bind.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
define(function() {
22

3-
return function( cldr, messageformatter, runtime, pluralLocales, embeddedFormatters ) {
3+
return function( cldr, messageformatter, runtime, pluralType, locale, embeddedFormatters ) {
44
var origToString = messageformatter.toString;
55

66
messageformatter.toString = function() {
7-
var argNames, argValues, output, locale,
7+
var argNames, argValues, output,
88
args = {};
99

1010
// Properly adjust SlexAxton/messageformat.js compiled variables with Globalize variables:
@@ -28,9 +28,9 @@ return function( cldr, messageformatter, runtime, pluralLocales, embeddedFormatt
2828
} ).join( ", " ) + "]";
2929
}
3030

31-
for ( locale in pluralLocales ) {
31+
if ( pluralType !== false ) {
3232
args[locale] = "Globalize(\"" + locale + "\")." +
33-
"pluralGenerator( { type: \"both\" } )";
33+
"pluralGenerator( { type: \"" + pluralType + "\" } )";
3434
}
3535

3636
argNames = Object.keys( args ).join( ", " );

test/functional/message/message-formatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
236236
function( runtimeArgs ) {
237237
assert.equal(
238238
runtimeArgs[ 0 ].toString(),
239-
"(function( number, plural, en ) {\n return function (d) { return plural(d.count, 1, en, { \"0\": \"Be the first to like this\", \"1\": \"You liked this\", one: \"You and \" + d.someone + \" liked this\", other: \"You and \" + number(d.count, \"count\", 1) + \" others liked this\" }); }\n})(messageFormat.number, messageFormat.plural, Globalize(\"en\").pluralGenerator( { type: \"both\" } ))"
239+
"(function( number, plural, en ) {\n return function (d) { return plural(d.count, 1, en, { \"0\": \"Be the first to like this\", \"1\": \"You liked this\", one: \"You and \" + d.someone + \" liked this\", other: \"You and \" + number(d.count, \"count\", 1) + \" others liked this\" }); }\n})(messageFormat.number, messageFormat.plural, Globalize(\"en\").pluralGenerator( { type: \"cardinal\" } ))"
240240
);
241241
}
242242
);

0 commit comments

Comments
 (0)