@@ -4,11 +4,10 @@ import generator from "./generator";
4
4
import "globalize/message" ;
5
5
import "globalize/plural" ;
6
6
7
- function messageSetup ( componentProps , globalize , args ) {
8
- var defaultMessage , path ;
9
- var children = componentProps . children ;
10
- var scope = componentProps . scope ;
11
- var pathProperty = componentProps . path ;
7
+ function messageSetup ( globalize , props , globalizePropValues ) {
8
+ var defaultMessage ;
9
+ var children = props . children ;
10
+ var scope = props . scope ;
12
11
13
12
function getDefaultMessage ( children ) {
14
13
if ( typeof children === "string" ) {
@@ -18,71 +17,72 @@ function messageSetup(componentProps, globalize, args) {
18
17
}
19
18
}
20
19
21
- function getMessage ( ) {
22
- return globalize . cldr . get ( [ "globalize-messages/{bundle}" ] . concat ( path ) ) ;
23
- }
24
-
25
- function setMessage ( message ) {
26
- var data = { } ;
27
- function set ( data , path , value ) {
28
- var i ;
29
- var node = data ;
30
- var length = path . length ;
31
-
32
- for ( i = 0 ; i < length - 1 ; i ++ ) {
33
- if ( ! node [ path [ i ] ] ) {
34
- node [ path [ i ] ] = { } ;
35
- }
36
- node = node [ path [ i ] ] ;
37
- }
38
- node [ path [ i ] ] = value ;
39
- }
40
- set ( data , [ globalize . cldr . attributes . bundle ] . concat ( path ) , message ) ;
41
- Globalize . loadMessages ( data ) ;
42
- }
43
-
44
20
// Set path - path as props supercedes default value.
45
- if ( pathProperty ) {
46
- // Override generator assumption. The generator assumes the args [0]
21
+ if ( props . path ) {
22
+ // Override generator assumption. The generator assumes the globalizePropValues [0]
47
23
// (path) and props.children to be mutually exclusive, but this isn't
48
24
// true here for messages. Because, it's possible to use props.path (for
49
25
// path) and props.children for defaultMessage, which are two different
50
26
// variables.
51
- args [ 0 ] = pathProperty ;
52
- path = pathProperty . split ( "/" ) ;
27
+ globalizePropValues [ 0 ] = props . path ;
53
28
} else {
54
- // Although the generator had already set args [0] (path) as
29
+ // Although the generator had already set globalizePropValues [0] (path) as
55
30
// props.children, here its type is checked and its value is sanitized.
56
31
defaultMessage = getDefaultMessage ( children ) ;
57
- args [ 0 ] = sanitizePath ( defaultMessage ) ;
58
- path = [ args [ 0 ] ] ;
32
+ globalizePropValues [ 0 ] = sanitizePath ( defaultMessage ) ;
59
33
}
60
34
61
35
// Scope path.
62
36
if ( scope ) {
63
- args [ 0 ] = scope + "/" + args [ 0 ] ;
64
- path = scope . split ( "/" ) . concat ( path ) ;
37
+ globalizePropValues [ 0 ] = scope + "/" + globalizePropValues [ 0 ] ;
65
38
}
66
39
67
40
// Development mode only.
68
- if ( globalize . cldr ) {
69
- if ( ! getMessage ( ) ) {
70
- defaultMessage = defaultMessage || getDefaultMessage ( children ) ;
71
- setMessage ( defaultMessage ) ;
41
+ if ( process . env . NODE_ENV !== "production" ) {
42
+ var path = props . path ? props . path . split ( "/" ) : [ globalizePropValues [ 0 ] ] ;
43
+ /* eslint-disable no-inner-declarations */
44
+ function getMessage ( globalize , path ) {
45
+ return globalize . cldr . get ( [ "globalize-messages/{bundle}" ] . concat ( path ) ) ;
72
46
}
73
- }
74
47
48
+ function setMessage ( globalize , path , message ) {
49
+ var data = { } ;
50
+ function set ( data , path , value ) {
51
+ var i ;
52
+ var node = data ;
53
+ var length = path . length ;
54
+
55
+ for ( i = 0 ; i < length - 1 ; i ++ ) {
56
+ if ( ! node [ path [ i ] ] ) {
57
+ node [ path [ i ] ] = { } ;
58
+ }
59
+ node = node [ path [ i ] ] ;
60
+ }
61
+ node [ path [ i ] ] = value ;
62
+ }
63
+ set ( data , [ globalize . cldr . attributes . bundle ] . concat ( path ) , message ) ;
64
+ Globalize . loadMessages ( data ) ;
65
+ }
66
+ /* eslint-enable no-inner-declarations */
67
+
68
+ if ( globalize . cldr ) {
69
+ if ( ! getMessage ( globalize , path ) ) {
70
+ defaultMessage = defaultMessage || getDefaultMessage ( children ) ;
71
+ setMessage ( globalize , path , defaultMessage ) ;
72
+ }
73
+ }
74
+ }
75
75
}
76
76
77
- function replaceElements ( componentProps , formatted ) {
78
- var elements = componentProps . elements ;
77
+ function replaceElements ( props , formatted ) {
78
+ var elements = props . elements ;
79
79
80
80
function _replaceElements ( string , elements ) {
81
81
if ( typeof string !== "string" ) {
82
- throw new Error ( "missing or invalid string `" + string + "` (" + typeof string + ")" ) ;
82
+ throw new Error ( "Missing or invalid string `" + string + "` (" + typeof string + ")" ) ;
83
83
}
84
84
if ( typeof elements !== "object" ) {
85
- throw new Error ( "missing or invalid elements `" + elements + "` (" + typeof elements + ")" ) ;
85
+ throw new Error ( "Missing or invalid elements `" + elements + "` (" + typeof elements + ")" ) ;
86
86
}
87
87
88
88
// Given [x, y, z], it returns [x, element, y, element, z].
@@ -156,9 +156,8 @@ function sanitizePath(pathString) {
156
156
}
157
157
158
158
// Overload Globalize's `.formatMessage` to allow default message.
159
- var messageFormatterSuper = Globalize . messageFormatter ;
160
- Globalize . messageFormatter =
161
- Globalize . prototype . messageFormatter = function ( pathOrMessage ) {
159
+ var globalizeMessageFormatter = Globalize . messageFormatter ;
160
+ Globalize . messageFormatter = Globalize . prototype . messageFormatter = function ( pathOrMessage ) {
162
161
var aux = { } ;
163
162
var sanitizedPath = sanitizePath ( pathOrMessage ) ;
164
163
@@ -167,9 +166,9 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
167
166
// On runtime, the only way for deciding between using sanitizedPath or
168
167
// pathOrMessage as path is by checking which formatter exists.
169
168
arguments [ 0 ] = sanitizedPath ;
170
- aux = messageFormatterSuper . apply ( this , arguments ) ;
169
+ aux = globalizeMessageFormatter . apply ( this , arguments ) ;
171
170
arguments [ 0 ] = pathOrMessage ;
172
- return aux || messageFormatterSuper . apply ( this , arguments ) ;
171
+ return aux || globalizeMessageFormatter . apply ( this , arguments ) ;
173
172
}
174
173
175
174
var sanitizedPathExists = this . cldr . get ( [ "globalize-messages/{bundle}" , sanitizedPath ] ) !== undefined ;
@@ -186,12 +185,12 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
186
185
}
187
186
188
187
arguments [ 0 ] = sanitizedPathExists ? sanitizedPath : pathOrMessage ;
189
- return messageFormatterSuper . apply ( this , arguments ) ;
188
+ return globalizeMessageFormatter . apply ( this , arguments ) ;
190
189
} ;
191
190
192
191
export default generator ( "formatMessage" , [ "path" , "variables" ] , {
193
192
beforeFormat : function ( ) {
194
- messageSetup ( this . props , this . globalize , this . globalizePropValues ) ;
193
+ messageSetup ( this . globalize , this . props , this . globalizePropValues ) ;
195
194
} ,
196
195
afterFormat : function ( formattedValue ) {
197
196
return replaceElements ( this . props , formattedValue ) ;
0 commit comments