@@ -4,11 +4,10 @@ import generator from "./generator";
44import "globalize/message" ;
55import "globalize/plural" ;
66
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 ;
1211
1312 function getDefaultMessage ( children ) {
1413 if ( typeof children === "string" ) {
@@ -18,71 +17,72 @@ function messageSetup(componentProps, globalize, args) {
1817 }
1918 }
2019
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-
4420 // 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]
4723 // (path) and props.children to be mutually exclusive, but this isn't
4824 // true here for messages. Because, it's possible to use props.path (for
4925 // path) and props.children for defaultMessage, which are two different
5026 // variables.
51- args [ 0 ] = pathProperty ;
52- path = pathProperty . split ( "/" ) ;
27+ globalizePropValues [ 0 ] = props . path ;
5328 } else {
54- // Although the generator had already set args [0] (path) as
29+ // Although the generator had already set globalizePropValues [0] (path) as
5530 // props.children, here its type is checked and its value is sanitized.
5631 defaultMessage = getDefaultMessage ( children ) ;
57- args [ 0 ] = sanitizePath ( defaultMessage ) ;
58- path = [ args [ 0 ] ] ;
32+ globalizePropValues [ 0 ] = sanitizePath ( defaultMessage ) ;
5933 }
6034
6135 // Scope path.
6236 if ( scope ) {
63- args [ 0 ] = scope + "/" + args [ 0 ] ;
64- path = scope . split ( "/" ) . concat ( path ) ;
37+ globalizePropValues [ 0 ] = scope + "/" + globalizePropValues [ 0 ] ;
6538 }
6639
6740 // 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 ) ) ;
7246 }
73- }
7447
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+ }
7575}
7676
77- function replaceElements ( componentProps , formatted ) {
78- var elements = componentProps . elements ;
77+ function replaceElements ( props , formatted ) {
78+ var elements = props . elements ;
7979
8080 function _replaceElements ( string , elements ) {
8181 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 + ")" ) ;
8383 }
8484 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 + ")" ) ;
8686 }
8787
8888 // Given [x, y, z], it returns [x, element, y, element, z].
@@ -156,9 +156,8 @@ function sanitizePath(pathString) {
156156}
157157
158158// 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 ) {
162161 var aux = { } ;
163162 var sanitizedPath = sanitizePath ( pathOrMessage ) ;
164163
@@ -167,9 +166,9 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
167166 // On runtime, the only way for deciding between using sanitizedPath or
168167 // pathOrMessage as path is by checking which formatter exists.
169168 arguments [ 0 ] = sanitizedPath ;
170- aux = messageFormatterSuper . apply ( this , arguments ) ;
169+ aux = globalizeMessageFormatter . apply ( this , arguments ) ;
171170 arguments [ 0 ] = pathOrMessage ;
172- return aux || messageFormatterSuper . apply ( this , arguments ) ;
171+ return aux || globalizeMessageFormatter . apply ( this , arguments ) ;
173172 }
174173
175174 var sanitizedPathExists = this . cldr . get ( [ "globalize-messages/{bundle}" , sanitizedPath ] ) !== undefined ;
@@ -186,12 +185,12 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
186185 }
187186
188187 arguments [ 0 ] = sanitizedPathExists ? sanitizedPath : pathOrMessage ;
189- return messageFormatterSuper . apply ( this , arguments ) ;
188+ return globalizeMessageFormatter . apply ( this , arguments ) ;
190189} ;
191190
192191export default generator ( "formatMessage" , [ "path" , "variables" ] , {
193192 beforeFormat : function ( ) {
194- messageSetup ( this . props , this . globalize , this . globalizePropValues ) ;
193+ messageSetup ( this . globalize , this . props , this . globalizePropValues ) ;
195194 } ,
196195 afterFormat : function ( formattedValue ) {
197196 return replaceElements ( this . props , formattedValue ) ;
0 commit comments