|
1 | 1 | define(function() { |
2 | 2 |
|
3 | | -return function( cldr, messageformatter, runtime, pluralType, locale, embeddedFormatters ) { |
4 | | - var origToString = messageformatter.toString; |
| 3 | +return function( messageformatter, formatterSrc, runtime, pluralType, locale, formatters ) { |
| 4 | + var hasFormatters = formatters.length > 0; |
5 | 5 |
|
6 | 6 | messageformatter.toString = function() { |
7 | | - var argNames, argValues, output, |
8 | | - args = {}; |
9 | | - |
10 | | - // Properly adjust SlexAxton/messageformat.js compiled variables with Globalize variables: |
11 | | - output = origToString.call( messageformatter ); |
| 7 | + var locals = []; |
| 8 | + var argCount = 0, |
| 9 | + args = []; |
12 | 10 |
|
13 | 11 | if ( runtime.number ) { |
14 | | - args.number = "messageFormat.number"; |
| 12 | + locals.push( "var number = messageFormat.number;" ); |
15 | 13 | } |
16 | 14 |
|
17 | 15 | if ( runtime.plural ) { |
18 | | - args.plural = "messageFormat.plural"; |
| 16 | + locals.push( "var plural = messageFormat.plural;" ); |
19 | 17 | } |
20 | 18 |
|
21 | 19 | if ( runtime.select ) { |
22 | | - args.select = "messageFormat.select"; |
23 | | - } |
24 | | - |
25 | | - if ( embeddedFormatters.length ) { |
26 | | - args.fmt = "[" + embeddedFormatters.map( function( fn ) { |
27 | | - return fn.generatorString(); |
28 | | - } ).join( ", " ) + "]"; |
| 20 | + locals.push( "var select = messageFormat.select;" ); |
29 | 21 | } |
30 | 22 |
|
31 | 23 | if ( pluralType !== false ) { |
32 | | - args[locale] = "Globalize(\"" + locale + "\")." + |
33 | | - "pluralGenerator( { type: \"" + pluralType + "\" } )"; |
| 24 | + args.push( locale ); |
| 25 | + argCount++; |
34 | 26 | } |
35 | 27 |
|
36 | | - argNames = Object.keys( args ).join( ", " ); |
37 | | - argValues = Object.keys( args ).map(function( key ) { |
38 | | - return args[ key ]; |
39 | | - }).join( ", " ); |
40 | | - |
41 | | - return "(function( " + argNames + " ) {\n" + |
42 | | - " return " + output + "\n" + |
43 | | - "})(" + argValues + ")"; |
| 28 | + return "(function( " + args.join( ", " ) + " ) {\n" + |
| 29 | + ( locals.length ? ( locals.join( "\n" ) + "\n" ) : "" ) + |
| 30 | + ( hasFormatters ? " var fmt = [].slice.call( arguments, " + argCount + " );\n" : "" ) + |
| 31 | + " return " + formatterSrc + "\n" + |
| 32 | + "})"; |
44 | 33 | }; |
45 | 34 |
|
46 | 35 | return messageformatter; |
|
0 commit comments