Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1d67358

Browse files
committed
Make it work with multiple regexps
1 parent bb030da commit 1d67358

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

src/languageHandler.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,38 @@ export function _t(...args) {
4949
* @param {string} jsxText The untranslated stringified JSX e.g "click <a href=''>here</a> now".
5050
* This will be translated by passing the string through to _t(...)
5151
*
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.
5353
* The captured groups from the regexp will be fed to 'sub'.
5454
* 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.
5558
*
56-
* @param {Function} sub A function which will be called
59+
* @param {Function|Function[]} subs A function which will be called
5760
* with multiple args, each arg representing a captured group of the matching regexp.
5861
* This function must return a JSX node.
5962
*
6063
* @return A list of strings/JSX nodes.
6164
*/
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];
6569
}
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+
}
6884
}
6985

7086
// tJsxText may be unsafe if malicious translators try to inject HTML.
@@ -74,18 +90,26 @@ export function _tJsx(jsxText, pattern, sub) {
7490
if (tJsxText !== sanitized) {
7591
throw new Error(`_tJsx: translator error. untrusted HTML supplied. '${tJsxText}' != '${sanitized}'`);
7692
}
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));
80110
}
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;
89113
}
90114

91115
// Allow overriding the text displayed when no translation exists

0 commit comments

Comments
 (0)