@webiny/i18n 0.0.0-unstable.fcdad0bc61 → 0.0.0-unstable.fdd9228b5d
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.d.ts +2 -2
- package/I18n.js +36 -102
- package/I18n.js.map +1 -1
- package/extractor/extract.js +5 -14
- package/extractor/extract.js.map +1 -1
- package/extractor/index.js +4 -24
- package/extractor/index.js.map +1 -1
- package/index.js +4 -9
- package/index.js.map +1 -1
- package/modifiers/countModifier.d.ts +1 -1
- package/modifiers/countModifier.js +6 -11
- package/modifiers/countModifier.js.map +1 -1
- package/modifiers/dateModifier.d.ts +1 -1
- package/modifiers/dateModifier.js +2 -4
- package/modifiers/dateModifier.js.map +1 -1
- package/modifiers/dateTimeModifier.d.ts +1 -1
- package/modifiers/dateTimeModifier.js +2 -4
- package/modifiers/dateTimeModifier.js.map +1 -1
- package/modifiers/genderModifier.d.ts +1 -1
- package/modifiers/genderModifier.js +2 -4
- package/modifiers/genderModifier.js.map +1 -1
- package/modifiers/ifModifier.d.ts +1 -1
- package/modifiers/ifModifier.js +2 -4
- package/modifiers/ifModifier.js.map +1 -1
- package/modifiers/index.d.ts +1 -1
- package/modifiers/index.js +2 -12
- package/modifiers/index.js.map +1 -1
- package/modifiers/numberModifier.d.ts +1 -1
- package/modifiers/numberModifier.js +2 -4
- package/modifiers/numberModifier.js.map +1 -1
- package/modifiers/pluralModifier.d.ts +1 -1
- package/modifiers/pluralModifier.js +4 -10
- package/modifiers/pluralModifier.js.map +1 -1
- package/modifiers/priceModifier.d.ts +1 -1
- package/modifiers/priceModifier.js +2 -4
- package/modifiers/priceModifier.js.map +1 -1
- package/modifiers/timeModifier.d.ts +1 -1
- package/modifiers/timeModifier.js +2 -4
- package/modifiers/timeModifier.js.map +1 -1
- package/package.json +13 -21
- package/processors/default.d.ts +1 -1
- package/processors/default.js +3 -19
- package/processors/default.js.map +1 -1
- package/types.d.ts +3 -3
- package/types.js +3 -1
- package/types.js.map +1 -1
package/I18n.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Formats, I18NDataValues, Modifier, NumberFormat, PriceFormat, Processor, ProcessorResult, Translations, Translator } from "./types";
|
|
2
|
-
export
|
|
1
|
+
import type { Formats, I18NDataValues, Modifier, NumberFormat, PriceFormat, Processor, ProcessorResult, Translations, Translator } from "./types";
|
|
2
|
+
export type Translated = ((values: I18NDataValues) => ProcessorResult | null) | ProcessorResult | null;
|
|
3
3
|
/**
|
|
4
4
|
* Main class used for all I18n needs.
|
|
5
5
|
*/
|
package/I18n.js
CHANGED
|
@@ -1,85 +1,66 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
-
|
|
5
4
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
-
|
|
7
5
|
Object.defineProperty(exports, "__esModule", {
|
|
8
6
|
value: true
|
|
9
7
|
});
|
|
10
8
|
exports.default = void 0;
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
9
|
var _accounting = _interopRequireDefault(require("accounting"));
|
|
15
|
-
|
|
16
10
|
var fecha = _interopRequireWildcard(require("fecha"));
|
|
17
|
-
|
|
18
11
|
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
19
|
-
|
|
20
12
|
var _assign = _interopRequireDefault(require("lodash/assign"));
|
|
21
|
-
|
|
22
13
|
var _get = _interopRequireDefault(require("lodash/get"));
|
|
23
|
-
|
|
24
14
|
/**
|
|
25
15
|
* Package short-hash has no types.
|
|
26
16
|
*/
|
|
27
|
-
// @ts-
|
|
17
|
+
// @ts-expect-error
|
|
28
18
|
|
|
29
19
|
/**
|
|
30
20
|
* Main class used for all I18n needs.
|
|
31
21
|
*/
|
|
32
22
|
class I18N {
|
|
23
|
+
locale = null;
|
|
33
24
|
constructor() {
|
|
34
|
-
(0, _defineProperty2.default)(this, "locale", null);
|
|
35
|
-
(0, _defineProperty2.default)(this, "defaultFormats", void 0);
|
|
36
|
-
(0, _defineProperty2.default)(this, "translations", void 0);
|
|
37
|
-
(0, _defineProperty2.default)(this, "modifiers", void 0);
|
|
38
|
-
(0, _defineProperty2.default)(this, "processors", void 0);
|
|
39
|
-
|
|
40
25
|
/**
|
|
41
26
|
* If we fail to fetch formats for currently selected locale, these default formats will be used.
|
|
42
27
|
* @type {{date: string, time: string, datetime: string, number: string}}
|
|
43
28
|
*/
|
|
44
29
|
this.defaultFormats = this.getDefaultFormats();
|
|
30
|
+
|
|
45
31
|
/**
|
|
46
32
|
* All currently-loaded translations, for easier (synchronous) access.
|
|
47
33
|
* @type {{}}
|
|
48
34
|
*/
|
|
49
|
-
|
|
50
35
|
this.translations = {};
|
|
36
|
+
|
|
51
37
|
/**
|
|
52
38
|
* All registered modifiers.
|
|
53
39
|
* @type {{}}
|
|
54
40
|
*/
|
|
55
|
-
|
|
56
41
|
this.modifiers = {};
|
|
42
|
+
|
|
57
43
|
/**
|
|
58
44
|
* All registered processors.
|
|
59
45
|
* Default built-in processors are registered immediately below.
|
|
60
46
|
* @type {{}}
|
|
61
47
|
*/
|
|
62
|
-
|
|
63
48
|
this.processors = {};
|
|
64
49
|
}
|
|
65
|
-
|
|
66
50
|
translate(base, namespace) {
|
|
67
51
|
// Returns full translation for given base text in given namespace (optional).
|
|
68
52
|
// If translation isn't found, base text will be returned.
|
|
69
53
|
// We create a key out of given namespace and base text.
|
|
54
|
+
|
|
70
55
|
if (!namespace) {
|
|
71
56
|
throw Error("I18N text namespace not defined.");
|
|
72
57
|
}
|
|
73
|
-
|
|
74
58
|
base = (0, _get.default)(base, "raw.0", base);
|
|
75
59
|
let translation = this.getTranslation(namespace + "." + (0, _shortHash.default)(base));
|
|
76
|
-
|
|
77
60
|
if (!translation) {
|
|
78
61
|
translation = base;
|
|
79
62
|
}
|
|
80
|
-
|
|
81
63
|
const hasVariables = base.includes("{") && base.includes("}");
|
|
82
|
-
|
|
83
64
|
if (hasVariables) {
|
|
84
65
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
85
66
|
const $this = this;
|
|
@@ -91,19 +72,15 @@ class I18N {
|
|
|
91
72
|
values,
|
|
92
73
|
i18n: $this
|
|
93
74
|
};
|
|
94
|
-
|
|
95
75
|
for (const key in $this.processors) {
|
|
96
76
|
const processor = $this.processors[key];
|
|
97
|
-
|
|
98
77
|
if (processor.canExecute(data)) {
|
|
99
78
|
return processor.execute(data);
|
|
100
79
|
}
|
|
101
80
|
}
|
|
102
|
-
|
|
103
81
|
return null;
|
|
104
82
|
};
|
|
105
83
|
}
|
|
106
|
-
|
|
107
84
|
const data = {
|
|
108
85
|
translation,
|
|
109
86
|
base,
|
|
@@ -111,275 +88,237 @@ class I18N {
|
|
|
111
88
|
values: {},
|
|
112
89
|
i18n: this
|
|
113
90
|
};
|
|
114
|
-
|
|
115
91
|
for (const key in this.processors) {
|
|
116
92
|
if (this.processors[key].canExecute(data)) {
|
|
117
93
|
return this.processors[key].execute(data);
|
|
118
94
|
}
|
|
119
95
|
}
|
|
120
|
-
|
|
121
96
|
return null;
|
|
122
97
|
}
|
|
123
|
-
|
|
124
98
|
namespace(namespace) {
|
|
125
99
|
return base => {
|
|
126
100
|
return this.translate(base, namespace);
|
|
127
101
|
};
|
|
128
102
|
}
|
|
129
|
-
|
|
130
103
|
ns(namespace) {
|
|
131
104
|
return this.namespace(namespace);
|
|
132
105
|
}
|
|
106
|
+
|
|
133
107
|
/**
|
|
134
108
|
* Formats and outputs date.
|
|
135
109
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
136
110
|
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
111
|
date(value, outputFormat, inputFormat) {
|
|
140
112
|
if (!outputFormat) {
|
|
141
113
|
outputFormat = this.getDateFormat();
|
|
142
114
|
}
|
|
143
|
-
|
|
144
115
|
if (!inputFormat) {
|
|
145
116
|
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
146
117
|
}
|
|
147
|
-
|
|
148
118
|
let parsedValue;
|
|
149
|
-
|
|
150
119
|
if (typeof value === "string") {
|
|
151
120
|
parsedValue = fecha.parse(value, inputFormat);
|
|
152
121
|
} else {
|
|
153
122
|
parsedValue = value;
|
|
154
123
|
}
|
|
155
|
-
|
|
156
124
|
return fecha.format(parsedValue, outputFormat);
|
|
157
125
|
}
|
|
126
|
+
|
|
158
127
|
/**
|
|
159
128
|
* Formats and outputs time.
|
|
160
129
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
161
130
|
*/
|
|
162
|
-
|
|
163
|
-
|
|
164
131
|
time(value, outputFormat, inputFormat) {
|
|
165
132
|
if (!outputFormat) {
|
|
166
133
|
outputFormat = this.getTimeFormat();
|
|
167
134
|
}
|
|
168
|
-
|
|
169
135
|
if (!inputFormat) {
|
|
170
136
|
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
171
137
|
}
|
|
172
|
-
|
|
173
138
|
let parsedValue;
|
|
174
|
-
|
|
175
139
|
if (typeof value === "string") {
|
|
176
140
|
parsedValue = fecha.parse(value, inputFormat);
|
|
177
141
|
} else {
|
|
178
142
|
parsedValue = value;
|
|
179
143
|
}
|
|
180
|
-
|
|
181
144
|
return fecha.format(parsedValue, outputFormat);
|
|
182
145
|
}
|
|
146
|
+
|
|
183
147
|
/**
|
|
184
148
|
* Formats and outputs date/time.
|
|
185
149
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
186
150
|
*/
|
|
187
|
-
|
|
188
|
-
|
|
189
151
|
dateTime(value, outputFormat, inputFormat) {
|
|
190
152
|
if (!outputFormat) {
|
|
191
153
|
outputFormat = this.getDateTimeFormat();
|
|
192
154
|
}
|
|
193
|
-
|
|
194
155
|
if (!inputFormat) {
|
|
195
156
|
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
196
157
|
}
|
|
197
|
-
|
|
198
158
|
let parsedValue;
|
|
199
|
-
|
|
200
159
|
if (typeof value === "string") {
|
|
201
160
|
parsedValue = fecha.parse(value, inputFormat);
|
|
202
161
|
} else {
|
|
203
162
|
parsedValue = value;
|
|
204
163
|
}
|
|
205
|
-
|
|
206
164
|
return fecha.format(parsedValue, outputFormat);
|
|
207
165
|
}
|
|
166
|
+
|
|
208
167
|
/**
|
|
209
168
|
* Outputs formatted number as amount of price.
|
|
210
169
|
*/
|
|
211
|
-
|
|
212
|
-
|
|
213
170
|
price(value, outputFormat) {
|
|
214
171
|
if (!outputFormat) {
|
|
215
172
|
outputFormat = this.getPriceFormat();
|
|
216
173
|
} else {
|
|
217
174
|
outputFormat = (0, _assign.default)({}, this.defaultFormats.price, outputFormat);
|
|
218
|
-
}
|
|
219
|
-
|
|
175
|
+
}
|
|
220
176
|
|
|
177
|
+
// Convert placeholders to accounting's placeholders.
|
|
221
178
|
let format = outputFormat.format;
|
|
222
179
|
format = format.replace("{symbol}", "%s");
|
|
223
180
|
format = format.replace("{amount}", "%v");
|
|
224
181
|
return _accounting.default.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
|
|
225
182
|
}
|
|
183
|
+
|
|
226
184
|
/**
|
|
227
185
|
* Outputs formatted number.
|
|
228
186
|
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
187
|
number(value, outputFormat) {
|
|
232
188
|
if (!outputFormat) {
|
|
233
189
|
outputFormat = this.getNumberFormat();
|
|
234
190
|
} else {
|
|
235
191
|
outputFormat = (0, _assign.default)({}, this.defaultFormats.number, outputFormat);
|
|
236
192
|
}
|
|
237
|
-
|
|
238
193
|
return _accounting.default.formatNumber(
|
|
239
194
|
/**
|
|
240
195
|
* Cast as number because method transforms it internally.
|
|
241
196
|
*/
|
|
242
197
|
value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
|
|
243
198
|
}
|
|
199
|
+
|
|
244
200
|
/**
|
|
245
201
|
* Returns translation for given text key.
|
|
246
202
|
*/
|
|
247
|
-
|
|
248
|
-
|
|
249
203
|
getTranslation(key) {
|
|
250
204
|
if (!key) {
|
|
251
205
|
return null;
|
|
252
206
|
}
|
|
253
|
-
|
|
254
207
|
return this.translations[key];
|
|
255
208
|
}
|
|
209
|
+
|
|
256
210
|
/**
|
|
257
211
|
* Returns all translations for current locale.
|
|
258
212
|
*/
|
|
259
|
-
|
|
260
|
-
|
|
261
213
|
getTranslations() {
|
|
262
214
|
return this.translations;
|
|
263
215
|
}
|
|
216
|
+
|
|
264
217
|
/**
|
|
265
218
|
* Returns true if given key has a translation for currently set locale.
|
|
266
219
|
*/
|
|
267
|
-
|
|
268
|
-
|
|
269
220
|
hasTranslation(key) {
|
|
270
221
|
return key in this.translations;
|
|
271
222
|
}
|
|
223
|
+
|
|
272
224
|
/**
|
|
273
225
|
* Sets translation for given text key.
|
|
274
226
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
277
227
|
setTranslation(key, translation) {
|
|
278
228
|
this.translations[key] = translation;
|
|
279
229
|
return this;
|
|
280
230
|
}
|
|
231
|
+
|
|
281
232
|
/**
|
|
282
233
|
* Sets translations that will be used.
|
|
283
234
|
*/
|
|
284
|
-
|
|
285
|
-
|
|
286
235
|
setTranslations(translations) {
|
|
287
236
|
this.translations = translations;
|
|
288
237
|
return this;
|
|
289
238
|
}
|
|
239
|
+
|
|
290
240
|
/**
|
|
291
241
|
* Clears all translations.
|
|
292
242
|
*/
|
|
293
|
-
|
|
294
|
-
|
|
295
243
|
clearTranslations() {
|
|
296
244
|
this.setTranslations({});
|
|
297
245
|
return this;
|
|
298
246
|
}
|
|
247
|
+
|
|
299
248
|
/**
|
|
300
249
|
* Merges given translations object with already existing.
|
|
301
250
|
*/
|
|
302
251
|
|
|
303
|
-
|
|
304
252
|
mergeTranslations(translations) {
|
|
305
253
|
return (0, _assign.default)(this.translations, translations);
|
|
306
254
|
}
|
|
255
|
+
|
|
307
256
|
/**
|
|
308
257
|
* Returns currently selected locale (locale's key).
|
|
309
258
|
*/
|
|
310
|
-
|
|
311
|
-
|
|
312
259
|
getLocale() {
|
|
313
260
|
return this.locale;
|
|
314
261
|
}
|
|
262
|
+
|
|
315
263
|
/**
|
|
316
264
|
* Sets current locale.
|
|
317
265
|
*/
|
|
318
|
-
|
|
319
|
-
|
|
320
266
|
setLocale(locale) {
|
|
321
267
|
this.locale = locale;
|
|
322
268
|
return this;
|
|
323
269
|
}
|
|
270
|
+
|
|
324
271
|
/**
|
|
325
272
|
* Registers single modifier.
|
|
326
273
|
*/
|
|
327
|
-
|
|
328
|
-
|
|
329
274
|
registerModifier(modifier) {
|
|
330
275
|
this.modifiers[modifier.name] = modifier;
|
|
331
276
|
return this;
|
|
332
277
|
}
|
|
278
|
+
|
|
333
279
|
/**
|
|
334
280
|
* Registers all modifiers in given array.
|
|
335
281
|
*/
|
|
336
|
-
|
|
337
|
-
|
|
338
282
|
registerModifiers(modifiers) {
|
|
339
283
|
modifiers.forEach(modifier => this.registerModifier(modifier));
|
|
340
284
|
return this;
|
|
341
285
|
}
|
|
286
|
+
|
|
342
287
|
/**
|
|
343
288
|
* Unregisters given modifier.
|
|
344
289
|
*/
|
|
345
|
-
|
|
346
|
-
|
|
347
290
|
unregisterModifier(name) {
|
|
348
291
|
delete this.modifiers[name];
|
|
349
292
|
return this;
|
|
350
293
|
}
|
|
294
|
+
|
|
351
295
|
/**
|
|
352
296
|
* Registers single processor.
|
|
353
297
|
*/
|
|
354
|
-
|
|
355
|
-
|
|
356
298
|
registerProcessor(processor) {
|
|
357
299
|
this.processors[processor.name] = processor;
|
|
358
300
|
return this;
|
|
359
301
|
}
|
|
302
|
+
|
|
360
303
|
/**
|
|
361
304
|
* Registers all processors in given array.
|
|
362
305
|
*/
|
|
363
|
-
|
|
364
|
-
|
|
365
306
|
registerProcessors(processors) {
|
|
366
307
|
processors.forEach(processor => this.registerProcessor(processor));
|
|
367
308
|
return this;
|
|
368
309
|
}
|
|
310
|
+
|
|
369
311
|
/**
|
|
370
312
|
* Unregisters given processor.
|
|
371
313
|
*/
|
|
372
|
-
|
|
373
|
-
|
|
374
314
|
unregisterProcessor(name) {
|
|
375
315
|
delete this.processors[name];
|
|
376
316
|
return this;
|
|
377
317
|
}
|
|
318
|
+
|
|
378
319
|
/**
|
|
379
320
|
* Returns default formats
|
|
380
321
|
*/
|
|
381
|
-
|
|
382
|
-
|
|
383
322
|
getDefaultFormats() {
|
|
384
323
|
return {
|
|
385
324
|
date: "DD/MM/YYYY",
|
|
@@ -399,47 +338,42 @@ class I18N {
|
|
|
399
338
|
}
|
|
400
339
|
};
|
|
401
340
|
}
|
|
341
|
+
|
|
402
342
|
/**
|
|
403
343
|
* Returns current format to be used when outputting dates.
|
|
404
344
|
*/
|
|
405
|
-
|
|
406
|
-
|
|
407
345
|
getDateFormat() {
|
|
408
346
|
return (0, _get.default)(this.locale, "formats.date", this.defaultFormats.date);
|
|
409
347
|
}
|
|
348
|
+
|
|
410
349
|
/**
|
|
411
350
|
* Returns current format to be used when outputting time.
|
|
412
351
|
*/
|
|
413
|
-
|
|
414
|
-
|
|
415
352
|
getTimeFormat() {
|
|
416
353
|
return (0, _get.default)(this.locale, "formats.time", this.defaultFormats.time);
|
|
417
354
|
}
|
|
355
|
+
|
|
418
356
|
/**
|
|
419
357
|
* Returns current format to be used when outputting date/time.
|
|
420
358
|
*/
|
|
421
|
-
|
|
422
|
-
|
|
423
359
|
getDateTimeFormat() {
|
|
424
360
|
return (0, _get.default)(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
425
361
|
}
|
|
362
|
+
|
|
426
363
|
/**
|
|
427
364
|
* Returns current format to be used when outputting prices.
|
|
428
365
|
*/
|
|
429
|
-
|
|
430
|
-
|
|
431
366
|
getPriceFormat() {
|
|
432
367
|
return (0, _assign.default)({}, this.defaultFormats.price, (0, _get.default)(this.locale, "formats.price", {}));
|
|
433
368
|
}
|
|
369
|
+
|
|
434
370
|
/**
|
|
435
371
|
* Returns current format to be used when outputting numbers.
|
|
436
372
|
*/
|
|
437
|
-
|
|
438
|
-
|
|
439
373
|
getNumberFormat() {
|
|
440
374
|
return (0, _assign.default)({}, this.defaultFormats.number, (0, _get.default)(this.locale, "formats.number", {}));
|
|
441
375
|
}
|
|
442
|
-
|
|
443
376
|
}
|
|
377
|
+
exports.default = I18N;
|
|
444
378
|
|
|
445
|
-
|
|
379
|
+
//# sourceMappingURL=I18n.js.map
|
package/I18n.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["I18N","constructor","defaultFormats","getDefaultFormats","translations","modifiers","processors","translate","base","namespace","Error","lodashGet","translation","getTranslation","hash","hasVariables","includes","$this","i18n","values","data","key","processor","canExecute","execute","ns","date","value","outputFormat","inputFormat","getDateFormat","parsedValue","fecha","parse","format","time","getTimeFormat","dateTime","getDateTimeFormat","price","getPriceFormat","lodashAssign","replace","accounting","formatMoney","symbol","precision","thousand","decimal","number","getNumberFormat","formatNumber","getTranslations","hasTranslation","setTranslation","setTranslations","clearTranslations","mergeTranslations","getLocale","locale","setLocale","registerModifier","modifier","name","registerModifiers","forEach","unregisterModifier","registerProcessor","registerProcessors","unregisterProcessor","datetime"],"sources":["I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\n/**\n * Package short-hash has no types.\n */\n// @ts-ignore\nimport hash from \"short-hash\";\nimport lodashAssign from \"lodash/assign\";\nimport lodashGet from \"lodash/get\";\n\nimport {\n Formats,\n I18NData,\n I18NDataValues,\n Modifier,\n NumberFormat,\n PriceFormat,\n Processor,\n ProcessorResult,\n Translations,\n Translator\n} from \"./types\";\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 // eslint-disable-next-line @typescript-eslint/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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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;;AACA;;AAKA;;AACA;;AACA;;AANA;AACA;AACA;AACA;;AAsBA;AACA;AACA;AACe,MAAMA,IAAN,CAAW;EAWfC,WAAW,GAAG;IAAA,8CAVU,IAUV;IAAA;IAAA;IAAA;IAAA;;IACjB;AACR;AACA;AACA;IACQ,KAAKC,cAAL,GAAsB,KAAKC,iBAAL,EAAtB;IAEA;AACR;AACA;AACA;;IACQ,KAAKC,YAAL,GAAoB,EAApB;IAEA;AACR;AACA;AACA;;IACQ,KAAKC,SAAL,GAAiB,EAAjB;IAEA;AACR;AACA;AACA;AACA;;IACQ,KAAKC,UA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AEHX,MAAM,EAAE,kBAFL;QAGHc,OAAO,EAAE,GAHN;QAIHD,QAAQ,EAAE,GAJP;QAKHD,SAAS,EAAE;MALR,CAJJ;MAWHG,MAAM,EAAE;QACJD,OAAO,EAAE,GADL;QAEJD,QAAQ,EAAE,GAFN;QAGJD,SAAS,EAAE;MAHP;IAXL,CAAP;EAiBH;EAED;AACJ;AACA;;;EACWhB,aAAa,GAAW;IAC3B,OAAO,IAAAnB,YAAA,EAAU,KAAKgD,MAAf,EAAuB,cAAvB,EAAuC,KAAKzD,cAAL,CAAoBwB,IAA3D,CAAP;EACH;EAED;AACJ;AACA;;;EACWU,aAAa,GAAW;IAC3B,OAAO,IAAAzB,YAAA,EAAU,KAAKgD,MAAf,EAAuB,cAAvB,EAAuC,KAAKzD,cAAL,CAAoBiC,IAA3D,CAAP;EACH;EAED;AACJ;AACA;;;EACWG,iBAAiB,GAAW;IAC/B,OAAO,IAAA3B,YAAA,EAAU,KAAKgD,MAAf,EAAuB,kBAAvB,EAA2C,KAAKzD,cAAL,CAAoBoE,QAA/D,CAAP;EACH;EAED;AACJ;AACA;;;EACW9B,cAAc,GAAgB;IACjC,OAAO,IAAAC,eAAA,EACH,EADG,EAEH,KAAKvC,cAAL,CAAoBqC,KAFjB,EAGH,IAAA5B,YAAA,EAAU,KAAKgD,MAAf,EAAuB,eAAvB,EAAwC,EAAxC,CAHG,CAAP;EAKH;EAED;AACJ;AACA;;;EACWT,eAAe,GAAiB;IACnC,OAAO,IAAAT,eAAA,EACH,EADG,EAEH,KAAKvC,cAAL,CAAoB+C,MAFjB,EAGH,IAAAtC,YAAA,EAAU,KAAKgD,MAAf,EAAuB,gBAAvB,EAAyC,EAAzC,CAHG,CAAP;EAKH;;AAjZqB"}
|
|
1
|
+
{"version":3,"names":["_accounting","_interopRequireDefault","require","fecha","_interopRequireWildcard","_shortHash","_assign","_get","I18N","locale","constructor","defaultFormats","getDefaultFormats","translations","modifiers","processors","translate","base","namespace","Error","lodashGet","translation","getTranslation","hash","hasVariables","includes","$this","i18n","values","data","key","processor","canExecute","execute","ns","date","value","outputFormat","inputFormat","getDateFormat","parsedValue","parse","format","time","getTimeFormat","dateTime","getDateTimeFormat","price","getPriceFormat","lodashAssign","replace","accounting","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","exports","default"],"sources":["I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\n/**\n * Package short-hash has no types.\n */\n// @ts-expect-error\nimport hash from \"short-hash\";\nimport lodashAssign from \"lodash/assign\";\nimport lodashGet from \"lodash/get\";\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\";\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 // eslint-disable-next-line @typescript-eslint/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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = 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,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,KAAA,GAAAC,uBAAA,CAAAF,OAAA;AAKA,IAAAG,UAAA,GAAAJ,sBAAA,CAAAC,OAAA;AACA,IAAAI,OAAA,GAAAL,sBAAA,CAAAC,OAAA;AACA,IAAAK,IAAA,GAAAN,sBAAA,CAAAC,OAAA;AANA;AACA;AACA;AACA;;AAsBA;AACA;AACA;AACe,MAAMM,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,UAAU,GAAG,CAAC,CAAC;EACxB;EAEOC,SAASA,CAACC,IAAY,EAAEC,SAAkB,EAAc;IAC3D;IACA;IACA;;IAEA,IAAI,CAACA,SAAS,EAAE;MACZ,MAAMC,KAAK,CAAC,kCAAkC,CAAC;IACnD;IAEAF,IAAI,GAAG,IAAAG,YAAS,EAACH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
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* Package short-hash has no types.
|
|
14
11
|
*/
|
|
15
|
-
// @ts-
|
|
12
|
+
// @ts-expect-error
|
|
16
13
|
|
|
17
14
|
/**
|
|
18
15
|
* Searches for all declared namespaces.
|
|
@@ -24,39 +21,33 @@ const getNamespaces = source => {
|
|
|
24
21
|
const regex = /([a-zA-Z0-9]+)[ ]+=[ ]+i18n.namespace\(['"`]([a-zA-Z0-9.]+)['"`]\)/g;
|
|
25
22
|
let m;
|
|
26
23
|
const results = {};
|
|
27
|
-
|
|
28
24
|
while ((m = regex.exec(source)) !== null) {
|
|
29
25
|
if (m.index === regex.lastIndex) {
|
|
30
26
|
regex.lastIndex++;
|
|
31
27
|
}
|
|
32
|
-
|
|
33
28
|
results[m[1]] = m[2];
|
|
34
29
|
}
|
|
35
|
-
|
|
36
30
|
return results;
|
|
37
31
|
};
|
|
38
|
-
|
|
39
32
|
var _default = source => {
|
|
40
33
|
const results = {};
|
|
41
34
|
const allDeclaredNamespaces = getNamespaces(source);
|
|
42
|
-
|
|
43
35
|
for (const variable in allDeclaredNamespaces) {
|
|
44
36
|
const regex = new RegExp(variable + "`(.*?)`", "g");
|
|
45
37
|
let m;
|
|
46
|
-
|
|
47
38
|
while ((m = regex.exec(source)) !== null) {
|
|
48
39
|
if (m.index === regex.lastIndex) {
|
|
49
40
|
regex.lastIndex++;
|
|
50
|
-
}
|
|
51
|
-
|
|
41
|
+
}
|
|
52
42
|
|
|
43
|
+
// This is the key - namespace + hash of matched label.
|
|
53
44
|
const matchedText = m[1];
|
|
54
45
|
const key = allDeclaredNamespaces[variable] + "." + (0, _shortHash.default)(matchedText);
|
|
55
46
|
results[key] = matchedText;
|
|
56
47
|
}
|
|
57
48
|
}
|
|
58
|
-
|
|
59
49
|
return results;
|
|
60
50
|
};
|
|
51
|
+
exports.default = _default;
|
|
61
52
|
|
|
62
|
-
|
|
53
|
+
//# sourceMappingURL=extract.js.map
|
package/extractor/extract.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getNamespaces","source","regex","m","results","exec","index","lastIndex","allDeclaredNamespaces","variable","RegExp","matchedText","key","hash"],"sources":["extract.ts"],"sourcesContent":["/**\n * Package short-hash has no types.\n */\n// @ts-
|
|
1
|
+
{"version":3,"names":["_shortHash","_interopRequireDefault","require","getNamespaces","source","regex","m","results","exec","index","lastIndex","_default","allDeclaredNamespaces","variable","RegExp","matchedText","key","hash","exports","default"],"sources":["extract.ts"],"sourcesContent":["/**\n * Package short-hash has no types.\n */\n// @ts-expect-error\nimport hash from \"short-hash\";\n\n/**\n * Searches for all declared namespaces.\n * Result contains an object with identifiers as keys, and namespaces they represent as values, for example:\n * {ns1: 'Webiny.Ns1', ns2: 'Webiny.Ns2', i18n: 'NewNamespace', t: 'Some.Other.Namespace'}\n * @param source\n */\nconst getNamespaces = (source: string) => {\n const regex = /([a-zA-Z0-9]+)[ ]+=[ ]+i18n.namespace\\(['\"`]([a-zA-Z0-9.]+)['\"`]\\)/g;\n let m;\n\n const results: Record<string, string> = {};\n\n while ((m = regex.exec(source)) !== null) {\n if (m.index === regex.lastIndex) {\n regex.lastIndex++;\n }\n\n results[m[1]] = m[2];\n }\n\n return results;\n};\n\nexport default (source: string) => {\n const results: Record<string, string> = {};\n const allDeclaredNamespaces = getNamespaces(source);\n\n for (const variable in allDeclaredNamespaces) {\n const regex = new RegExp(variable + \"`(.*?)`\", \"g\");\n\n let m;\n while ((m = regex.exec(source)) !== null) {\n if (m.index === regex.lastIndex) {\n regex.lastIndex++;\n }\n\n // This is the key - namespace + hash of matched label.\n const matchedText = m[1];\n const key = allDeclaredNamespaces[variable] + \".\" + hash(matchedText);\n results[key] = matchedText;\n }\n }\n\n return results;\n};\n"],"mappings":";;;;;;;AAIA,IAAAA,UAAA,GAAAC,sBAAA,CAAAC,OAAA;AAJA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,aAAa,GAAIC,MAAc,IAAK;EACtC,MAAMC,KAAK,GAAG,qEAAqE;EACnF,IAAIC,CAAC;EAEL,MAAMC,OAA+B,GAAG,CAAC,CAAC;EAE1C,OAAO,CAACD,CAAC,GAAGD,KAAK,CAACG,IAAI,CAACJ,MAAM,CAAC,MAAM,IAAI,EAAE;IACtC,IAAIE,CAAC,CAACG,KAAK,KAAKJ,KAAK,CAACK,SAAS,EAAE;MAC7BL,KAAK,CAACK,SAAS,EAAE;IACrB;IAEAH,OAAO,CAACD,CAAC,CAAC,CAAC,CAAC,CAAC,GAAGA,CAAC,CAAC,CAAC,CAAC;EACxB;EAEA,OAAOC,OAAO;AAClB,CAAC;AAAC,IAAAI,QAAA,GAEcP,MAAc,IAAK;EAC/B,MAAMG,OAA+B,GAAG,CAAC,CAAC;EAC1C,MAAMK,qBAAqB,GAAGT,aAAa,CAACC,MAAM,CAAC;EAEnD,KAAK,MAAMS,QAAQ,IAAID,qBAAqB,EAAE;IAC1C,MAAMP,KAAK,GAAG,IAAIS,MAAM,CAACD,QAAQ,GAAG,SAAS,EAAE,GAAG,CAAC;IAEnD,IAAIP,CAAC;IACL,OAAO,CAACA,CAAC,GAAGD,KAAK,CAACG,IAAI,CAACJ,MAAM,CAAC,MAAM,IAAI,EAAE;MACtC,IAAIE,CAAC,CAACG,KAAK,KAAKJ,KAAK,CAACK,SAAS,EAAE;QAC7BL,KAAK,CAACK,SAAS,EAAE;MACrB;;MAEA;MACA,MAAMK,WAAW,GAAGT,CAAC,CAAC,CAAC,CAAC;MACxB,MAAMU,GAAG,GAAGJ,qBAAqB,CAACC,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