@@ -49,22 +49,38 @@ export function _t(...args) {
49
49
* @param {string } jsxText The untranslated stringified JSX e.g "click <a href=''>here</a> now".
50
50
* This will be translated by passing the string through to _t(...)
51
51
*
52
- * @param {RegExp } pattern A regexp to match against the translated text.
52
+ * @param {RegExp|RegExp[] } patterns A regexp to match against the translated text.
53
53
* The captured groups from the regexp will be fed to 'sub'.
54
54
* Only the captured groups will be included in the output, the match itself is discarded.
55
+ * If multiple RegExps are provided, the function at the same position will be called. The
56
+ * match will always be done from left to right, so the 2nd RegExp will be matched against the
57
+ * remaining text from the first RegExp.
55
58
*
56
- * @param {Function } sub A function which will be called
59
+ * @param {Function|Function[] } subs A function which will be called
57
60
* with multiple args, each arg representing a captured group of the matching regexp.
58
61
* This function must return a JSX node.
59
62
*
60
63
* @return A list of strings/JSX nodes.
61
64
*/
62
- export function _tJsx ( jsxText , pattern , sub ) {
63
- if ( ! pattern instanceof RegExp ) {
64
- throw new Error ( `_tJsx: programmer error. expected RegExp for text: ${ jsxText } ` ) ;
65
+ export function _tJsx ( jsxText , patterns , subs ) {
66
+ // convert everything to arrays
67
+ if ( patterns instanceof RegExp ) {
68
+ patterns = [ patterns ] ;
65
69
}
66
- if ( ! sub instanceof Function ) {
67
- throw new Error ( `_tJsx: programmer error. expected Function for text: ${ jsxText } ` ) ;
70
+ if ( subs instanceof Function ) {
71
+ subs = [ subs ] ;
72
+ }
73
+ // sanity checks
74
+ if ( subs . length !== patterns . length || subs . length < 1 ) {
75
+ throw new Error ( `_tJsx: programmer error. expected number of RegExps == number of Functions: ${ subs . length } != ${ patterns . length } ` ) ;
76
+ }
77
+ for ( let i = 0 ; i < subs . length ; i ++ ) {
78
+ if ( ! patterns [ i ] instanceof RegExp ) {
79
+ throw new Error ( `_tJsx: programmer error. expected RegExp for text: ${ jsxText } ` ) ;
80
+ }
81
+ if ( ! subs [ i ] instanceof Function ) {
82
+ throw new Error ( `_tJsx: programmer error. expected Function for text: ${ jsxText } ` ) ;
83
+ }
68
84
}
69
85
70
86
// tJsxText may be unsafe if malicious translators try to inject HTML.
@@ -74,18 +90,26 @@ export function _tJsx(jsxText, pattern, sub) {
74
90
if ( tJsxText !== sanitized ) {
75
91
throw new Error ( `_tJsx: translator error. untrusted HTML supplied. '${ tJsxText } ' != '${ sanitized } '` ) ;
76
92
}
77
- let match = tJsxText . match ( pattern ) ;
78
- if ( ! match ) {
79
- throw new Error ( `_tJsx: translator error. expected translation to match regexp: ${ pattern } ` ) ;
93
+
94
+ let output = [ tJsxText ] ;
95
+ for ( let i = 0 ; i < patterns . length ; i ++ ) {
96
+ // convert the last element in 'output' into 3 elements (pre-text, sub function, post-text).
97
+ // Rinse and repeat for other patterns (using post-text).
98
+ let inputText = output . pop ( ) ;
99
+ let match = inputText . match ( patterns [ i ] ) ;
100
+ if ( ! match ) {
101
+ throw new Error ( `_tJsx: translator error. expected translation to match regexp: ${ patterns [ i ] } ` ) ;
102
+ }
103
+ let capturedGroups = match . slice ( 1 ) ;
104
+
105
+ // Return the raw translation before the *match* followed by the return value of sub() followed
106
+ // by the raw translation after the *match* (not captured group).
107
+ output . push ( inputText . substr ( 0 , match . index ) ) ;
108
+ output . push ( subs [ i ] . apply ( null , capturedGroups ) ) ;
109
+ output . push ( inputText . substr ( match . index + match [ 0 ] . length ) ) ;
80
110
}
81
- let capturedGroups = match . slice ( 1 ) ;
82
- // Return the raw translation before the *match* followed by the return value of sub() followed
83
- // by the raw translation after the *match* (not captured group).
84
- return [
85
- tJsxText . substr ( 0 , match . index ) ,
86
- sub . apply ( null , capturedGroups ) ,
87
- tJsxText . substr ( match . index + match [ 0 ] . length ) ,
88
- ] ;
111
+
112
+ return output ;
89
113
}
90
114
91
115
// Allow overriding the text displayed when no translation exists
0 commit comments