@webiny/i18n 6.3.0 → 6.4.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/I18n.js +163 -350
- package/I18n.js.map +1 -1
- package/extractor/extract.js +22 -34
- package/extractor/extract.js.map +1 -1
- package/extractor/index.js +30 -31
- package/extractor/index.js.map +1 -1
- package/index.js +7 -6
- package/index.js.map +1 -1
- package/modifiers/countModifier.js +16 -28
- package/modifiers/countModifier.js.map +1 -1
- package/modifiers/dateModifier.js +7 -8
- package/modifiers/dateModifier.js.map +1 -1
- package/modifiers/dateTimeModifier.js +7 -8
- package/modifiers/dateTimeModifier.js.map +1 -1
- package/modifiers/genderModifier.js +7 -8
- package/modifiers/genderModifier.js.map +1 -1
- package/modifiers/ifModifier.js +7 -8
- package/modifiers/ifModifier.js.map +1 -1
- package/modifiers/index.js +13 -3
- package/modifiers/index.js.map +1 -1
- package/modifiers/numberModifier.js +7 -8
- package/modifiers/numberModifier.js.map +1 -1
- package/modifiers/pluralModifier.js +16 -26
- package/modifiers/pluralModifier.js.map +1 -1
- package/modifiers/priceModifier.js +7 -8
- package/modifiers/priceModifier.js.map +1 -1
- package/modifiers/timeModifier.js +7 -8
- package/modifiers/timeModifier.js.map +1 -1
- package/package.json +4 -5
- package/processors/default.js +32 -40
- package/processors/default.js.map +1 -1
- package/types.js +0 -3
- package/types.js.map +0 -1
package/I18n.js
CHANGED
|
@@ -1,365 +1,178 @@
|
|
|
1
1
|
import accounting from "accounting";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import lodashGet from "lodash/get.js";
|
|
2
|
+
import lodash_assign from "lodash/assign.js";
|
|
3
|
+
import get from "lodash/get.js";
|
|
5
4
|
import { hash } from "ohash";
|
|
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
|
-
// Returns full translation for given base text in given namespace (optional).
|
|
39
|
-
// If translation isn't found, base text will be returned.
|
|
40
|
-
// We create a key out of given namespace and base text.
|
|
41
|
-
|
|
42
|
-
if (!namespace) {
|
|
43
|
-
throw Error("I18N text namespace not defined.");
|
|
44
|
-
}
|
|
45
|
-
base = lodashGet(base, "raw.0", base);
|
|
46
|
-
let translation = this.getTranslation(namespace + "." + hash(base));
|
|
47
|
-
if (!translation) {
|
|
48
|
-
translation = base;
|
|
49
|
-
}
|
|
50
|
-
const hasVariables = base.includes("{") && base.includes("}");
|
|
51
|
-
if (hasVariables) {
|
|
52
|
-
// oxlint-disable-next-line typescript/no-this-alias
|
|
53
|
-
const $this = this;
|
|
54
|
-
return function i18n(values) {
|
|
5
|
+
import * as __rspack_external_fecha from "fecha";
|
|
6
|
+
class I18N {
|
|
7
|
+
constructor(){
|
|
8
|
+
this.locale = null;
|
|
9
|
+
this.defaultFormats = this.getDefaultFormats();
|
|
10
|
+
this.translations = {};
|
|
11
|
+
this.modifiers = {};
|
|
12
|
+
this.processors = {};
|
|
13
|
+
}
|
|
14
|
+
translate(base, namespace) {
|
|
15
|
+
if (!namespace) throw Error("I18N text namespace not defined.");
|
|
16
|
+
base = get(base, "raw.0", base);
|
|
17
|
+
let translation = this.getTranslation(namespace + "." + hash(base));
|
|
18
|
+
if (!translation) translation = base;
|
|
19
|
+
const hasVariables = base.includes("{") && base.includes("}");
|
|
20
|
+
if (hasVariables) {
|
|
21
|
+
const $this = this;
|
|
22
|
+
return function(values) {
|
|
23
|
+
const data = {
|
|
24
|
+
translation: translation,
|
|
25
|
+
base,
|
|
26
|
+
namespace,
|
|
27
|
+
values,
|
|
28
|
+
i18n: $this
|
|
29
|
+
};
|
|
30
|
+
for(const key in $this.processors){
|
|
31
|
+
const processor = $this.processors[key];
|
|
32
|
+
if (processor.canExecute(data)) return processor.execute(data);
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
55
37
|
const data = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
38
|
+
translation,
|
|
39
|
+
base,
|
|
40
|
+
namespace,
|
|
41
|
+
values: {},
|
|
42
|
+
i18n: this
|
|
61
43
|
};
|
|
62
|
-
for
|
|
63
|
-
const processor = $this.processors[key];
|
|
64
|
-
if (processor.canExecute(data)) {
|
|
65
|
-
return processor.execute(data);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
44
|
+
for(const key in this.processors)if (this.processors[key].canExecute(data)) return this.processors[key].execute(data);
|
|
68
45
|
return null;
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
const data = {
|
|
72
|
-
translation,
|
|
73
|
-
base,
|
|
74
|
-
namespace,
|
|
75
|
-
values: {},
|
|
76
|
-
i18n: this
|
|
77
|
-
};
|
|
78
|
-
for (const key in this.processors) {
|
|
79
|
-
if (this.processors[key].canExecute(data)) {
|
|
80
|
-
return this.processors[key].execute(data);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
namespace(namespace) {
|
|
86
|
-
return base => {
|
|
87
|
-
return this.translate(base, namespace);
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
ns(namespace) {
|
|
91
|
-
return this.namespace(namespace);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Formats and outputs date.
|
|
96
|
-
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
97
|
-
*/
|
|
98
|
-
date(value, outputFormat, inputFormat) {
|
|
99
|
-
if (!outputFormat) {
|
|
100
|
-
outputFormat = this.getDateFormat();
|
|
101
46
|
}
|
|
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
|
-
|
|
47
|
+
namespace(namespace) {
|
|
48
|
+
return (base)=>this.translate(base, namespace);
|
|
49
|
+
}
|
|
50
|
+
ns(namespace) {
|
|
51
|
+
return this.namespace(namespace);
|
|
52
|
+
}
|
|
53
|
+
date(value, outputFormat, inputFormat) {
|
|
54
|
+
if (!outputFormat) outputFormat = this.getDateFormat();
|
|
55
|
+
if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
56
|
+
let parsedValue;
|
|
57
|
+
parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
|
|
58
|
+
return __rspack_external_fecha.format(parsedValue, outputFormat);
|
|
59
|
+
}
|
|
60
|
+
time(value, outputFormat, inputFormat) {
|
|
61
|
+
if (!outputFormat) outputFormat = this.getTimeFormat();
|
|
62
|
+
if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
63
|
+
let parsedValue;
|
|
64
|
+
parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
|
|
65
|
+
return __rspack_external_fecha.format(parsedValue, outputFormat);
|
|
66
|
+
}
|
|
67
|
+
dateTime(value, outputFormat, inputFormat) {
|
|
68
|
+
if (!outputFormat) outputFormat = this.getDateTimeFormat();
|
|
69
|
+
if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
70
|
+
let parsedValue;
|
|
71
|
+
parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
|
|
72
|
+
return __rspack_external_fecha.format(parsedValue, outputFormat);
|
|
73
|
+
}
|
|
74
|
+
price(value, outputFormat) {
|
|
75
|
+
outputFormat = outputFormat ? lodash_assign({}, this.defaultFormats.price, outputFormat) : this.getPriceFormat();
|
|
76
|
+
let format = outputFormat.format;
|
|
77
|
+
format = format.replace("{symbol}", "%s");
|
|
78
|
+
format = format.replace("{amount}", "%v");
|
|
79
|
+
return accounting.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
|
|
80
|
+
}
|
|
81
|
+
number(value, outputFormat) {
|
|
82
|
+
outputFormat = outputFormat ? lodash_assign({}, this.defaultFormats.number, outputFormat) : this.getNumberFormat();
|
|
83
|
+
return accounting.formatNumber(value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
|
|
84
|
+
}
|
|
85
|
+
getTranslation(key) {
|
|
86
|
+
if (!key) return null;
|
|
87
|
+
return this.translations[key];
|
|
88
|
+
}
|
|
89
|
+
getTranslations() {
|
|
90
|
+
return this.translations;
|
|
91
|
+
}
|
|
92
|
+
hasTranslation(key) {
|
|
93
|
+
return key in this.translations;
|
|
94
|
+
}
|
|
95
|
+
setTranslation(key, translation) {
|
|
96
|
+
this.translations[key] = translation;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
setTranslations(translations) {
|
|
100
|
+
this.translations = translations;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
clearTranslations() {
|
|
104
|
+
this.setTranslations({});
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
mergeTranslations(translations) {
|
|
108
|
+
return lodash_assign(this.translations, translations);
|
|
109
|
+
}
|
|
110
|
+
getLocale() {
|
|
111
|
+
return this.locale;
|
|
112
|
+
}
|
|
113
|
+
setLocale(locale) {
|
|
114
|
+
this.locale = locale;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
registerModifier(modifier) {
|
|
118
|
+
this.modifiers[modifier.name] = modifier;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
registerModifiers(modifiers) {
|
|
122
|
+
modifiers.forEach((modifier)=>this.registerModifier(modifier));
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
unregisterModifier(name) {
|
|
126
|
+
delete this.modifiers[name];
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
registerProcessor(processor) {
|
|
130
|
+
this.processors[processor.name] = processor;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
registerProcessors(processors) {
|
|
134
|
+
processors.forEach((processor)=>this.registerProcessor(processor));
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
unregisterProcessor(name) {
|
|
138
|
+
delete this.processors[name];
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
getDefaultFormats() {
|
|
142
|
+
return {
|
|
143
|
+
date: "DD/MM/YYYY",
|
|
144
|
+
time: "HH:mm",
|
|
145
|
+
datetime: "DD/MM/YYYY HH:mm",
|
|
146
|
+
price: {
|
|
147
|
+
symbol: "",
|
|
148
|
+
format: "{symbol}{amount}",
|
|
149
|
+
decimal: ".",
|
|
150
|
+
thousand: ",",
|
|
151
|
+
precision: 2
|
|
152
|
+
},
|
|
153
|
+
number: {
|
|
154
|
+
decimal: ".",
|
|
155
|
+
thousand: ",",
|
|
156
|
+
precision: 2
|
|
157
|
+
}
|
|
158
|
+
};
|
|
141
159
|
}
|
|
142
|
-
|
|
143
|
-
|
|
160
|
+
getDateFormat() {
|
|
161
|
+
return get(this.locale, "formats.date", this.defaultFormats.date);
|
|
144
162
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
parsedValue = fecha.parse(value, inputFormat);
|
|
148
|
-
} else {
|
|
149
|
-
parsedValue = value instanceof Date ? value : new Date(value);
|
|
163
|
+
getTimeFormat() {
|
|
164
|
+
return get(this.locale, "formats.time", this.defaultFormats.time);
|
|
150
165
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Outputs formatted number as amount of price.
|
|
156
|
-
*/
|
|
157
|
-
price(value, outputFormat) {
|
|
158
|
-
if (!outputFormat) {
|
|
159
|
-
outputFormat = this.getPriceFormat();
|
|
160
|
-
} else {
|
|
161
|
-
outputFormat = lodashAssign({}, this.defaultFormats.price, outputFormat);
|
|
166
|
+
getDateTimeFormat() {
|
|
167
|
+
return get(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
162
168
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
let format = outputFormat.format;
|
|
166
|
-
format = format.replace("{symbol}", "%s");
|
|
167
|
-
format = format.replace("{amount}", "%v");
|
|
168
|
-
return accounting.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Outputs formatted number.
|
|
173
|
-
*/
|
|
174
|
-
number(value, outputFormat) {
|
|
175
|
-
if (!outputFormat) {
|
|
176
|
-
outputFormat = this.getNumberFormat();
|
|
177
|
-
} else {
|
|
178
|
-
outputFormat = lodashAssign({}, this.defaultFormats.number, outputFormat);
|
|
169
|
+
getPriceFormat() {
|
|
170
|
+
return lodash_assign({}, this.defaultFormats.price, get(this.locale, "formats.price", {}));
|
|
179
171
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
* Cast as number because method transforms it internally.
|
|
183
|
-
*/
|
|
184
|
-
value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Returns translation for given text key.
|
|
189
|
-
*/
|
|
190
|
-
getTranslation(key) {
|
|
191
|
-
if (!key) {
|
|
192
|
-
return null;
|
|
172
|
+
getNumberFormat() {
|
|
173
|
+
return lodash_assign({}, this.defaultFormats.number, get(this.locale, "formats.number", {}));
|
|
193
174
|
}
|
|
194
|
-
return this.translations[key];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Returns all translations for current locale.
|
|
199
|
-
*/
|
|
200
|
-
getTranslations() {
|
|
201
|
-
return this.translations;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Returns true if given key has a translation for currently set locale.
|
|
206
|
-
*/
|
|
207
|
-
hasTranslation(key) {
|
|
208
|
-
return key in this.translations;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Sets translation for given text key.
|
|
213
|
-
*/
|
|
214
|
-
setTranslation(key, translation) {
|
|
215
|
-
this.translations[key] = translation;
|
|
216
|
-
return this;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Sets translations that will be used.
|
|
221
|
-
*/
|
|
222
|
-
setTranslations(translations) {
|
|
223
|
-
this.translations = translations;
|
|
224
|
-
return this;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Clears all translations.
|
|
229
|
-
*/
|
|
230
|
-
clearTranslations() {
|
|
231
|
-
this.setTranslations({});
|
|
232
|
-
return this;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Merges given translations object with already existing.
|
|
237
|
-
*/
|
|
238
|
-
|
|
239
|
-
mergeTranslations(translations) {
|
|
240
|
-
return lodashAssign(this.translations, translations);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Returns currently selected locale (locale's key).
|
|
245
|
-
*/
|
|
246
|
-
getLocale() {
|
|
247
|
-
return this.locale;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Sets current locale.
|
|
252
|
-
*/
|
|
253
|
-
setLocale(locale) {
|
|
254
|
-
this.locale = locale;
|
|
255
|
-
return this;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Registers single modifier.
|
|
260
|
-
*/
|
|
261
|
-
registerModifier(modifier) {
|
|
262
|
-
this.modifiers[modifier.name] = modifier;
|
|
263
|
-
return this;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Registers all modifiers in given array.
|
|
268
|
-
*/
|
|
269
|
-
registerModifiers(modifiers) {
|
|
270
|
-
modifiers.forEach(modifier => this.registerModifier(modifier));
|
|
271
|
-
return this;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Unregisters given modifier.
|
|
276
|
-
*/
|
|
277
|
-
unregisterModifier(name) {
|
|
278
|
-
delete this.modifiers[name];
|
|
279
|
-
return this;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Registers single processor.
|
|
284
|
-
*/
|
|
285
|
-
registerProcessor(processor) {
|
|
286
|
-
this.processors[processor.name] = processor;
|
|
287
|
-
return this;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Registers all processors in given array.
|
|
292
|
-
*/
|
|
293
|
-
registerProcessors(processors) {
|
|
294
|
-
processors.forEach(processor => this.registerProcessor(processor));
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Unregisters given processor.
|
|
300
|
-
*/
|
|
301
|
-
unregisterProcessor(name) {
|
|
302
|
-
delete this.processors[name];
|
|
303
|
-
return this;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Returns default formats
|
|
308
|
-
*/
|
|
309
|
-
getDefaultFormats() {
|
|
310
|
-
return {
|
|
311
|
-
date: "DD/MM/YYYY",
|
|
312
|
-
time: "HH:mm",
|
|
313
|
-
datetime: "DD/MM/YYYY HH:mm",
|
|
314
|
-
price: {
|
|
315
|
-
symbol: "",
|
|
316
|
-
format: "{symbol}{amount}",
|
|
317
|
-
decimal: ".",
|
|
318
|
-
thousand: ",",
|
|
319
|
-
precision: 2
|
|
320
|
-
},
|
|
321
|
-
number: {
|
|
322
|
-
decimal: ".",
|
|
323
|
-
thousand: ",",
|
|
324
|
-
precision: 2
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Returns current format to be used when outputting dates.
|
|
331
|
-
*/
|
|
332
|
-
getDateFormat() {
|
|
333
|
-
return lodashGet(this.locale, "formats.date", this.defaultFormats.date);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Returns current format to be used when outputting time.
|
|
338
|
-
*/
|
|
339
|
-
getTimeFormat() {
|
|
340
|
-
return lodashGet(this.locale, "formats.time", this.defaultFormats.time);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Returns current format to be used when outputting date/time.
|
|
345
|
-
*/
|
|
346
|
-
getDateTimeFormat() {
|
|
347
|
-
return lodashGet(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Returns current format to be used when outputting prices.
|
|
352
|
-
*/
|
|
353
|
-
getPriceFormat() {
|
|
354
|
-
return lodashAssign({}, this.defaultFormats.price, lodashGet(this.locale, "formats.price", {}));
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Returns current format to be used when outputting numbers.
|
|
359
|
-
*/
|
|
360
|
-
getNumberFormat() {
|
|
361
|
-
return lodashAssign({}, this.defaultFormats.number, lodashGet(this.locale, "formats.number", {}));
|
|
362
|
-
}
|
|
363
175
|
}
|
|
176
|
+
export default I18N;
|
|
364
177
|
|
|
365
178
|
//# sourceMappingURL=I18n.js.map
|
package/I18n.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["accounting","fecha","lodashAssign","lodashGet","hash","I18N","locale","constructor","defaultFormats","getDefaultFormats","translations","modifiers","processors","translate","base","namespace","Error","translation","getTranslation","hasVariables","includes","$this","i18n","values","data","key","processor","canExecute","execute","ns","date","value","outputFormat","inputFormat","getDateFormat","parsedValue","parse","Date","format","time","getTimeFormat","dateTime","getDateTimeFormat","price","getPriceFormat","replace","formatMoney","symbol","precision","thousand","decimal","number","getNumberFormat","formatNumber","getTranslations","hasTranslation","setTranslation","setTranslations","clearTranslations","mergeTranslations","getLocale","setLocale","registerModifier","modifier","name","registerModifiers","forEach","unregisterModifier","registerProcessor","registerProcessors","unregisterProcessor","datetime"],"sources":["I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\nimport lodashAssign from \"lodash/assign.js\";\nimport lodashGet from \"lodash/get.js\";\n\nimport type {\n Formats,\n I18NData,\n I18NDataValues,\n Modifier,\n NumberFormat,\n PriceFormat,\n Processor,\n ProcessorResult,\n Translations,\n Translator\n} from \"./types.js\";\nimport { hash } from \"ohash\";\n\nexport type Translated =\n | ((values: I18NDataValues) => ProcessorResult | null)\n | ProcessorResult\n | null;\n/**\n * Main class used for all I18n needs.\n */\nexport default class I18N {\n public locale: string | null = null;\n public defaultFormats: Formats;\n public translations: Translations;\n public modifiers: {\n [name: string]: Modifier;\n };\n public processors: {\n [name: string]: Processor;\n };\n\n public constructor() {\n /**\n * If we fail to fetch formats for currently selected locale, these default formats will be used.\n * @type {{date: string, time: string, datetime: string, number: string}}\n */\n this.defaultFormats = this.getDefaultFormats();\n\n /**\n * All currently-loaded translations, for easier (synchronous) access.\n * @type {{}}\n */\n this.translations = {};\n\n /**\n * All registered modifiers.\n * @type {{}}\n */\n this.modifiers = {};\n\n /**\n * All registered processors.\n * Default built-in processors are registered immediately below.\n * @type {{}}\n */\n this.processors = {};\n }\n\n public translate(base: string, namespace?: string): Translated {\n // Returns full translation for given base text in given namespace (optional).\n // If translation isn't found, base text will be returned.\n // We create a key out of given namespace and base text.\n\n if (!namespace) {\n throw Error(\"I18N text namespace not defined.\");\n }\n\n base = lodashGet(base, \"raw.0\", base);\n\n let translation: string | null = this.getTranslation(namespace + \".\" + hash(base));\n\n if (!translation) {\n translation = base;\n }\n\n const hasVariables = base.includes(\"{\") && base.includes(\"}\");\n if (hasVariables) {\n // oxlint-disable-next-line typescript/no-this-alias\n const $this = this;\n return function i18n(values: I18NDataValues) {\n const data: I18NData = {\n translation: translation as string,\n base,\n namespace,\n values,\n i18n: $this\n };\n for (const key in $this.processors) {\n const processor = $this.processors[key];\n if (processor.canExecute(data)) {\n return processor.execute(data);\n }\n }\n return null;\n };\n }\n\n const data: I18NData = { translation, base, namespace, values: {}, i18n: this };\n for (const key in this.processors) {\n if (this.processors[key].canExecute(data)) {\n return this.processors[key].execute(data);\n }\n }\n return null;\n }\n\n public namespace(namespace: string): Translator {\n return base => {\n return this.translate(base as string, namespace);\n };\n }\n\n public ns(namespace: string): Translator {\n return this.namespace(namespace);\n }\n\n /**\n * Formats and outputs date.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public date(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getDateFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public time(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs date/time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string {\n if (!outputFormat) {\n outputFormat = this.getDateTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Outputs formatted number as amount of price.\n */\n public price(value: string | number, outputFormat?: any): string {\n if (!outputFormat) {\n outputFormat = this.getPriceFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.price, outputFormat);\n }\n\n // Convert placeholders to accounting's placeholders.\n let format = outputFormat.format;\n format = format.replace(\"{symbol}\", \"%s\");\n format = format.replace(\"{amount}\", \"%v\");\n\n return accounting.formatMoney(\n value,\n outputFormat.symbol,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal,\n format\n );\n }\n\n /**\n * Outputs formatted number.\n */\n public number(value: string | number, outputFormat?: NumberFormat): string {\n if (!outputFormat) {\n outputFormat = this.getNumberFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.number, outputFormat);\n }\n return accounting.formatNumber(\n /**\n * Cast as number because method transforms it internally.\n */\n value as number,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal\n );\n }\n\n /**\n * Returns translation for given text key.\n */\n public getTranslation(key?: string): string | null {\n if (!key) {\n return null;\n }\n return this.translations[key];\n }\n\n /**\n * Returns all translations for current locale.\n */\n public getTranslations(): Translations {\n return this.translations;\n }\n\n /**\n * Returns true if given key has a translation for currently set locale.\n */\n public hasTranslation(key: string): boolean {\n return key in this.translations;\n }\n\n /**\n * Sets translation for given text key.\n */\n public setTranslation(key: string, translation: string): I18N {\n this.translations[key] = translation;\n return this;\n }\n\n /**\n * Sets translations that will be used.\n */\n public setTranslations(translations: Translations): I18N {\n this.translations = translations;\n return this;\n }\n\n /**\n * Clears all translations.\n */\n public clearTranslations(): I18N {\n this.setTranslations({});\n return this;\n }\n\n /**\n * Merges given translations object with already existing.\n */\n\n public mergeTranslations(translations: Translations): Translations {\n return lodashAssign(this.translations, translations);\n }\n\n /**\n * Returns currently selected locale (locale's key).\n */\n public getLocale(): null | string {\n return this.locale;\n }\n\n /**\n * Sets current locale.\n */\n public setLocale(locale: string): I18N {\n this.locale = locale;\n return this;\n }\n\n /**\n * Registers single modifier.\n */\n public registerModifier(modifier: Modifier): I18N {\n this.modifiers[modifier.name] = modifier;\n return this;\n }\n\n /**\n * Registers all modifiers in given array.\n */\n public registerModifiers(modifiers: Array<Modifier>): I18N {\n modifiers.forEach(modifier => this.registerModifier(modifier));\n return this;\n }\n\n /**\n * Unregisters given modifier.\n */\n public unregisterModifier(name: string): I18N {\n delete this.modifiers[name];\n return this;\n }\n\n /**\n * Registers single processor.\n */\n public registerProcessor(processor: Processor): I18N {\n this.processors[processor.name] = processor;\n return this;\n }\n\n /**\n * Registers all processors in given array.\n */\n public registerProcessors(processors: Array<Processor>): I18N {\n processors.forEach(processor => this.registerProcessor(processor));\n return this;\n }\n\n /**\n * Unregisters given processor.\n */\n public unregisterProcessor(name: string): I18N {\n delete this.processors[name];\n return this;\n }\n\n /**\n * Returns default formats\n */\n public getDefaultFormats(): Formats {\n return {\n date: \"DD/MM/YYYY\",\n time: \"HH:mm\",\n datetime: \"DD/MM/YYYY HH:mm\",\n price: {\n symbol: \"\",\n format: \"{symbol}{amount}\",\n decimal: \".\",\n thousand: \",\",\n precision: 2\n },\n number: {\n decimal: \".\",\n thousand: \",\",\n precision: 2\n }\n };\n }\n\n /**\n * Returns current format to be used when outputting dates.\n */\n public getDateFormat(): string {\n return lodashGet(this.locale, \"formats.date\", this.defaultFormats.date);\n }\n\n /**\n * Returns current format to be used when outputting time.\n */\n public getTimeFormat(): string {\n return lodashGet(this.locale, \"formats.time\", this.defaultFormats.time);\n }\n\n /**\n * Returns current format to be used when outputting date/time.\n */\n public getDateTimeFormat(): string {\n return lodashGet(this.locale, \"formats.datetime\", this.defaultFormats.datetime);\n }\n\n /**\n * Returns current format to be used when outputting prices.\n */\n public getPriceFormat(): PriceFormat {\n return lodashAssign(\n {},\n this.defaultFormats.price,\n lodashGet(this.locale, \"formats.price\", {})\n );\n }\n\n /**\n * Returns current format to be used when outputting numbers.\n */\n public getNumberFormat(): NumberFormat {\n return lodashAssign(\n {},\n this.defaultFormats.number,\n lodashGet(this.locale, \"formats.number\", {})\n );\n }\n}\n"],"mappings":"AAAA,OAAOA,UAAU,MAAM,YAAY;AACnC,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,OAAOC,YAAY,MAAM,kBAAkB;AAC3C,OAAOC,SAAS,MAAM,eAAe;AAcrC,SAASC,IAAI,QAAQ,OAAO;AAM5B;AACA;AACA;AACA,eAAe,MAAMC,IAAI,CAAC;EACfC,MAAM,GAAkB,IAAI;EAU5BC,WAAWA,CAAA,EAAG;IACjB;AACR;AACA;AACA;IACQ,IAAI,CAACC,cAAc,GAAG,IAAI,CAACC,iBAAiB,CAAC,CAAC;;IAE9C;AACR;AACA;AACA;IACQ,IAAI,CAACC,YAAY,GAAG,CAAC,CAAC;;IAEtB;AACR;AACA;AACA;IACQ,IAAI,CAACC,SAAS,GAAG,CAAC,CAAC;;IAEnB;AACR;AACA;AACA;AACA;IACQ,IAAI,CAACC,UAA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;AACA;EACW8C,eAAeA,CAAA,EAAiB;IACnC,OAAOlD,YAAY,CACf,CAAC,CAAC,EACF,IAAI,CAACM,cAAc,CAAC2C,MAAM,EAC1BhD,SAAS,CAAC,IAAI,CAACG,MAAM,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAC/C,CAAC;EACL;AACJ","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"I18n.js","sources":["../src/I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\nimport lodashAssign from \"lodash/assign.js\";\nimport lodashGet from \"lodash/get.js\";\n\nimport type {\n Formats,\n I18NData,\n I18NDataValues,\n Modifier,\n NumberFormat,\n PriceFormat,\n Processor,\n ProcessorResult,\n Translations,\n Translator\n} from \"./types.js\";\nimport { hash } from \"ohash\";\n\nexport type Translated =\n | ((values: I18NDataValues) => ProcessorResult | null)\n | ProcessorResult\n | null;\n/**\n * Main class used for all I18n needs.\n */\nexport default class I18N {\n public locale: string | null = null;\n public defaultFormats: Formats;\n public translations: Translations;\n public modifiers: {\n [name: string]: Modifier;\n };\n public processors: {\n [name: string]: Processor;\n };\n\n public constructor() {\n /**\n * If we fail to fetch formats for currently selected locale, these default formats will be used.\n * @type {{date: string, time: string, datetime: string, number: string}}\n */\n this.defaultFormats = this.getDefaultFormats();\n\n /**\n * All currently-loaded translations, for easier (synchronous) access.\n * @type {{}}\n */\n this.translations = {};\n\n /**\n * All registered modifiers.\n * @type {{}}\n */\n this.modifiers = {};\n\n /**\n * All registered processors.\n * Default built-in processors are registered immediately below.\n * @type {{}}\n */\n this.processors = {};\n }\n\n public translate(base: string, namespace?: string): Translated {\n // Returns full translation for given base text in given namespace (optional).\n // If translation isn't found, base text will be returned.\n // We create a key out of given namespace and base text.\n\n if (!namespace) {\n throw Error(\"I18N text namespace not defined.\");\n }\n\n base = lodashGet(base, \"raw.0\", base);\n\n let translation: string | null = this.getTranslation(namespace + \".\" + hash(base));\n\n if (!translation) {\n translation = base;\n }\n\n const hasVariables = base.includes(\"{\") && base.includes(\"}\");\n if (hasVariables) {\n // oxlint-disable-next-line typescript/no-this-alias\n const $this = this;\n return function i18n(values: I18NDataValues) {\n const data: I18NData = {\n translation: translation as string,\n base,\n namespace,\n values,\n i18n: $this\n };\n for (const key in $this.processors) {\n const processor = $this.processors[key];\n if (processor.canExecute(data)) {\n return processor.execute(data);\n }\n }\n return null;\n };\n }\n\n const data: I18NData = { translation, base, namespace, values: {}, i18n: this };\n for (const key in this.processors) {\n if (this.processors[key].canExecute(data)) {\n return this.processors[key].execute(data);\n }\n }\n return null;\n }\n\n public namespace(namespace: string): Translator {\n return base => {\n return this.translate(base as string, namespace);\n };\n }\n\n public ns(namespace: string): Translator {\n return this.namespace(namespace);\n }\n\n /**\n * Formats and outputs date.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public date(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getDateFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public time(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs date/time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string {\n if (!outputFormat) {\n outputFormat = this.getDateTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Outputs formatted number as amount of price.\n */\n public price(value: string | number, outputFormat?: any): string {\n if (!outputFormat) {\n outputFormat = this.getPriceFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.price, outputFormat);\n }\n\n // Convert placeholders to accounting's placeholders.\n let format = outputFormat.format;\n format = format.replace(\"{symbol}\", \"%s\");\n format = format.replace(\"{amount}\", \"%v\");\n\n return accounting.formatMoney(\n value,\n outputFormat.symbol,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal,\n format\n );\n }\n\n /**\n * Outputs formatted number.\n */\n public number(value: string | number, outputFormat?: NumberFormat): string {\n if (!outputFormat) {\n outputFormat = this.getNumberFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.number, outputFormat);\n }\n return accounting.formatNumber(\n /**\n * Cast as number because method transforms it internally.\n */\n value as number,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal\n );\n }\n\n /**\n * Returns translation for given text key.\n */\n public getTranslation(key?: string): string | null {\n if (!key) {\n return null;\n }\n return this.translations[key];\n }\n\n /**\n * Returns all translations for current locale.\n */\n public getTranslations(): Translations {\n return this.translations;\n }\n\n /**\n * Returns true if given key has a translation for currently set locale.\n */\n public hasTranslation(key: string): boolean {\n return key in this.translations;\n }\n\n /**\n * Sets translation for given text key.\n */\n public setTranslation(key: string, translation: string): I18N {\n this.translations[key] = translation;\n return this;\n }\n\n /**\n * Sets translations that will be used.\n */\n public setTranslations(translations: Translations): I18N {\n this.translations = translations;\n return this;\n }\n\n /**\n * Clears all translations.\n */\n public clearTranslations(): I18N {\n this.setTranslations({});\n return this;\n }\n\n /**\n * Merges given translations object with already existing.\n */\n\n public mergeTranslations(translations: Translations): Translations {\n return lodashAssign(this.translations, translations);\n }\n\n /**\n * Returns currently selected locale (locale's key).\n */\n public getLocale(): null | string {\n return this.locale;\n }\n\n /**\n * Sets current locale.\n */\n public setLocale(locale: string): I18N {\n this.locale = locale;\n return this;\n }\n\n /**\n * Registers single modifier.\n */\n public registerModifier(modifier: Modifier): I18N {\n this.modifiers[modifier.name] = modifier;\n return this;\n }\n\n /**\n * Registers all modifiers in given array.\n */\n public registerModifiers(modifiers: Array<Modifier>): I18N {\n modifiers.forEach(modifier => this.registerModifier(modifier));\n return this;\n }\n\n /**\n * Unregisters given modifier.\n */\n public unregisterModifier(name: string): I18N {\n delete this.modifiers[name];\n return this;\n }\n\n /**\n * Registers single processor.\n */\n public registerProcessor(processor: Processor): I18N {\n this.processors[processor.name] = processor;\n return this;\n }\n\n /**\n * Registers all processors in given array.\n */\n public registerProcessors(processors: Array<Processor>): I18N {\n processors.forEach(processor => this.registerProcessor(processor));\n return this;\n }\n\n /**\n * Unregisters given processor.\n */\n public unregisterProcessor(name: string): I18N {\n delete this.processors[name];\n return this;\n }\n\n /**\n * Returns default formats\n */\n public getDefaultFormats(): Formats {\n return {\n date: \"DD/MM/YYYY\",\n time: \"HH:mm\",\n datetime: \"DD/MM/YYYY HH:mm\",\n price: {\n symbol: \"\",\n format: \"{symbol}{amount}\",\n decimal: \".\",\n thousand: \",\",\n precision: 2\n },\n number: {\n decimal: \".\",\n thousand: \",\",\n precision: 2\n }\n };\n }\n\n /**\n * Returns current format to be used when outputting dates.\n */\n public getDateFormat(): string {\n return lodashGet(this.locale, \"formats.date\", this.defaultFormats.date);\n }\n\n /**\n * Returns current format to be used when outputting time.\n */\n public getTimeFormat(): string {\n return lodashGet(this.locale, \"formats.time\", this.defaultFormats.time);\n }\n\n /**\n * Returns current format to be used when outputting date/time.\n */\n public getDateTimeFormat(): string {\n return lodashGet(this.locale, \"formats.datetime\", this.defaultFormats.datetime);\n }\n\n /**\n * Returns current format to be used when outputting prices.\n */\n public getPriceFormat(): PriceFormat {\n return lodashAssign(\n {},\n this.defaultFormats.price,\n lodashGet(this.locale, \"formats.price\", {})\n );\n }\n\n /**\n * Returns current format to be used when outputting numbers.\n */\n public getNumberFormat(): NumberFormat {\n return lodashAssign(\n {},\n this.defaultFormats.number,\n lodashGet(this.locale, \"formats.number\", {})\n );\n }\n}\n"],"names":["I18N","base","namespace","Error","lodashGet","translation","hash","hasVariables","$this","values","data","key","processor","value","outputFormat","inputFormat","parsedValue","fecha","Date","lodashAssign","format","accounting","translations","locale","modifier","modifiers","name","processors"],"mappings":";;;;;AA0Be,MAAMA;IAWjB,aAAqB;aAVd,MAAM,GAAkB;QAe3B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,iBAAiB;QAM5C,IAAI,CAAC,YAAY,GAAG,CAAC;QAMrB,IAAI,CAAC,SAAS,GAAG,CAAC;QAOlB,IAAI,CAAC,UAAU,GAAG,CAAC;IACvB;IAEO,UAAUC,IAAY,EAAEC,SAAkB,EAAc;QAK3D,IAAI,CAACA,WACD,MAAMC,MAAM;QAGhBF,OAAOG,IAAUH,MAAM,SAASA;QAEhC,IAAII,cAA6B,IAAI,CAAC,cAAc,CAACH,YAAY,MAAMI,KAAKL;QAE5E,IAAI,CAACI,aACDA,cAAcJ;QAGlB,MAAMM,eAAeN,KAAK,QAAQ,CAAC,QAAQA,KAAK,QAAQ,CAAC;QACzD,IAAIM,cAAc;YAEd,MAAMC,QAAQ,IAAI;YAClB,OAAO,SAAcC,MAAsB;gBACvC,MAAMC,OAAiB;oBACnB,aAAaL;oBACbJ;oBACAC;oBACAO;oBACA,MAAMD;gBACV;gBACA,IAAK,MAAMG,OAAOH,MAAM,UAAU,CAAE;oBAChC,MAAMI,YAAYJ,MAAM,UAAU,CAACG,IAAI;oBACvC,IAAIC,U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