forked from i18next/i18next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpolator.js
169 lines (139 loc) · 5.33 KB
/
Interpolator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as utils from "./utils.js";
import baseLogger from "./logger.js";
const prefix = "{{";
const suffix = "}}";
const formatSeparator = ",";
const unescapePrefix = "-";
const unescapeSuffix = "";
const nestingPrefix = utils.regexEscape("$t(");
const nestingSuffix = utils.regexEscape(")");
const nestingOptionsSeparator = ",";
const regexSafe = (val) => val.replace(/\$/g, "$$$$");
const Interpolator = (options = {}) => {
const logger = baseLogger.create("interpolator");
const format = options.interpolationFormat || ((value) => value);
const maxReplaces = options.maxReplaces || 1000;
let regexp, regexpUnescape, regexpNesting;
const _resetRegExp = () => {
regexp = new RegExp(`${prefix}(.+?)${suffix}`, "g");
regexpUnescape = new RegExp(`${prefix}${unescapePrefix}(.+?)${unescapeSuffix}${suffix}`, "g");
regexpNesting = new RegExp(`${nestingPrefix}(.+?)${nestingSuffix}`, "g");
};
_resetRegExp();
const interpolate = (str, data, lng, options) => {
const handleFormat = (key) => {
if (key.indexOf(formatSeparator) < 0) {
return utils.getPath(data, key);
}
const p = key.split(formatSeparator);
const k = p.shift().trim();
const f = p.join(formatSeparator).trim();
return format(utils.getPath(data, k), f, lng, options);
};
_resetRegExp();
let match;
let replaces;
replaces = 0;
// unescape
/* eslint no-cond-assign: 0 */
while ((match = regexpUnescape.exec(str))) {
let value = handleFormat(match[1].trim());
if (value === undefined) {
logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
value = "";
} else if (typeof value !== "string") {
value = utils.makeString(value);
}
str = str.replace(match[0], regexSafe(value));
regexpUnescape.lastIndex = 0;
replaces++;
if (replaces >= maxReplaces) {
break;
}
}
replaces = 0;
// regular escape on demand
while ((match = regexp.exec(str))) {
let value = handleFormat(match[1].trim());
if (value === undefined) {
logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
value = "";
} else if (typeof value !== "string") {
value = utils.makeString(value);
}
value = regexSafe(utils.escape(value));
str = str.replace(match[0], value);
regexp.lastIndex = 0;
replaces++;
if (replaces >= maxReplaces) {
break;
}
}
return str;
};
const nest = (str, t, lng, options = {}) => {
let match;
let value;
let clonedOptions = { ...options };
delete clonedOptions.defaultValue; // assert we do not get a endless loop on interpolating defaultValue again and again
// if value is something like "myKey": "lorem $(anotherKey, { "count": {{aValueInOptions}} })"
const handleHasOptions = (key, inheritedOptions) => {
const sep = nestingOptionsSeparator;
if (key.indexOf(sep) < 0) return key;
const c = key.split(new RegExp(`${sep}[ ]*{`));
let optionsString = `{${c[1]}`;
key = c[0];
optionsString = interpolate(optionsString, clonedOptions);
optionsString = optionsString.replace(/'/g, '"');
try {
clonedOptions = JSON.parse(optionsString);
if (inheritedOptions) clonedOptions = { ...inheritedOptions, ...clonedOptions };
} catch (e) {
logger.warn(`failed parsing options string in nesting for key ${key}`, e);
return `${key}${sep}${optionsString}`;
}
// assert we do not get a endless loop on interpolating defaultValue again and again
delete clonedOptions.defaultValue;
return key;
};
// regular escape on demand
while ((match = regexpNesting.exec(str))) {
let formatters = [];
/**
* If there is more than one parameter (contains the format separator). E.g.:
* - t(a, b)
* - t(a, b, c)
*
* And those parameters are not dynamic values (parameters do not include curly braces). E.g.:
* - Not t(a, { "key": "{{variable}}" })
* - Not t(a, b, {"keyA": "valueA", "keyB": "valueB"})
*/
let doReduce = false;
if (match[0].includes(formatSeparator) && !/{.*}/.test(match[1])) {
[match[1], ...formatters] = match[1].split(formatSeparator).map((elem) => elem.trim());
doReduce = true;
}
value = t(handleHasOptions(match[1].trim(), clonedOptions), clonedOptions);
// is only the nesting key (key1 = '$(key2)') return the value without stringify
if (value && match[0] === str && typeof value !== "string") return value;
// no string to include or empty
if (typeof value !== "string") value = utils.makeString(value);
if (!value) {
logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
value = "";
}
if (doReduce) {
value = formatters.reduce((v, f) => format(v, f, lng, options), value.trim());
}
// Nested keys should not be escaped by default #854
// value = regexSafe(utils.escape(value));
str = str.replace(match[0], value);
// TODO: was `regexp`
// https://github.com/i18next/i18next/blob/master/src/Interpolator.js#L224
regexpNesting.lastIndex = 0;
}
return str;
};
return { maxReplaces, interpolate, nest };
};
export default Interpolator;