@webiny/i18n 5.24.0 → 5.25.0-beta.0
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 +6 -39
- package/I18n.js +44 -56
- package/I18n.js.map +1 -0
- package/extractor/extract.js.map +1 -0
- package/extractor/index.d.ts +6 -3
- package/extractor/index.js +15 -14
- package/extractor/index.js.map +1 -0
- package/index.js.map +1 -0
- package/modifiers/countModifier.js.map +1 -0
- package/modifiers/dateModifier.js.map +1 -0
- package/modifiers/dateTimeModifier.js.map +1 -0
- package/modifiers/genderModifier.js.map +1 -0
- package/modifiers/ifModifier.js.map +1 -0
- package/modifiers/index.js.map +1 -0
- package/modifiers/numberModifier.js.map +1 -0
- package/modifiers/pluralModifier.js.map +1 -0
- package/modifiers/priceModifier.js.map +1 -0
- package/modifiers/timeModifier.js.map +1 -0
- package/package.json +7 -6
- package/processors/default.js +8 -8
- package/processors/default.js.map +1 -0
- package/types.d.ts +24 -22
- package/types.js.map +1 -0
package/I18n.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Formats, Modifier, NumberFormat, PriceFormat, Processor, Translations, Translator } from "./types";
|
|
1
|
+
import { Formats, I18NDataValues, Modifier, NumberFormat, PriceFormat, Processor, ProcessorResult, Translations, Translator } from "./types";
|
|
2
|
+
export declare type Translated = ((values: I18NDataValues) => ProcessorResult | null) | ProcessorResult | null;
|
|
2
3
|
/**
|
|
3
4
|
* Main class used for all I18n needs.
|
|
4
5
|
*/
|
|
5
6
|
export default class I18N {
|
|
6
|
-
locale
|
|
7
|
+
locale: string | null;
|
|
7
8
|
defaultFormats: Formats;
|
|
8
9
|
translations: Translations;
|
|
9
10
|
modifiers: {
|
|
@@ -13,81 +14,58 @@ export default class I18N {
|
|
|
13
14
|
[name: string]: Processor;
|
|
14
15
|
};
|
|
15
16
|
constructor();
|
|
16
|
-
translate(base: string, namespace?: string):
|
|
17
|
+
translate(base: string, namespace?: string): Translated;
|
|
17
18
|
namespace(namespace: string): Translator;
|
|
18
19
|
ns(namespace: string): Translator;
|
|
19
20
|
/**
|
|
20
21
|
* Formats and outputs date.
|
|
21
22
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
22
|
-
* @param value
|
|
23
|
-
* @param outputFormat
|
|
24
|
-
* @param inputFormat
|
|
25
23
|
*/
|
|
26
|
-
date(value: Date | string | number, outputFormat?: string, inputFormat?:
|
|
24
|
+
date(value: Date | string | number, outputFormat?: string, inputFormat?: string): string;
|
|
27
25
|
/**
|
|
28
26
|
* Formats and outputs time.
|
|
29
27
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
30
|
-
* @param value
|
|
31
|
-
* @param outputFormat
|
|
32
|
-
* @param inputFormat
|
|
33
28
|
*/
|
|
34
29
|
time(value: Date | string | number, outputFormat?: string, inputFormat?: string): string;
|
|
35
30
|
/**
|
|
36
31
|
* Formats and outputs date/time.
|
|
37
32
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
38
|
-
* @param value
|
|
39
|
-
* @param outputFormat
|
|
40
|
-
* @param inputFormat
|
|
41
33
|
*/
|
|
42
34
|
dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string;
|
|
43
35
|
/**
|
|
44
36
|
* Outputs formatted number as amount of price.
|
|
45
|
-
* @param value
|
|
46
|
-
* @param outputFormat
|
|
47
37
|
*/
|
|
48
38
|
price(value: string | number, outputFormat?: any): string;
|
|
49
39
|
/**
|
|
50
40
|
* Outputs formatted number.
|
|
51
|
-
* @param value
|
|
52
|
-
* @param outputFormat
|
|
53
41
|
*/
|
|
54
42
|
number(value: string | number, outputFormat?: NumberFormat): string;
|
|
55
43
|
/**
|
|
56
44
|
* Returns translation for given text key.
|
|
57
|
-
* @param key
|
|
58
|
-
* @returns {*|string}
|
|
59
45
|
*/
|
|
60
|
-
getTranslation(key?: string): string;
|
|
46
|
+
getTranslation(key?: string): string | null;
|
|
61
47
|
/**
|
|
62
48
|
* Returns all translations for current locale.
|
|
63
|
-
* @returns {*|{}}
|
|
64
49
|
*/
|
|
65
50
|
getTranslations(): Translations;
|
|
66
51
|
/**
|
|
67
52
|
* Returns true if given key has a translation for currently set locale.
|
|
68
|
-
* @param key
|
|
69
53
|
*/
|
|
70
54
|
hasTranslation(key: string): boolean;
|
|
71
55
|
/**
|
|
72
56
|
* Sets translation for given text key.
|
|
73
|
-
* @param key
|
|
74
|
-
* @param translation
|
|
75
|
-
* @returns {I18N}
|
|
76
57
|
*/
|
|
77
58
|
setTranslation(key: string, translation: string): I18N;
|
|
78
59
|
/**
|
|
79
60
|
* Sets translations that will be used.
|
|
80
|
-
* @returns {*|{}}
|
|
81
61
|
*/
|
|
82
62
|
setTranslations(translations: Translations): I18N;
|
|
83
63
|
/**
|
|
84
64
|
* Clears all translations.
|
|
85
|
-
* @returns {*|{}}
|
|
86
65
|
*/
|
|
87
66
|
clearTranslations(): I18N;
|
|
88
67
|
/**
|
|
89
68
|
* Merges given translations object with already existing.
|
|
90
|
-
* @returns {*|{}}
|
|
91
69
|
*/
|
|
92
70
|
mergeTranslations(translations: Translations): Translations;
|
|
93
71
|
/**
|
|
@@ -100,41 +78,30 @@ export default class I18N {
|
|
|
100
78
|
setLocale(locale: string): I18N;
|
|
101
79
|
/**
|
|
102
80
|
* Registers single modifier.
|
|
103
|
-
* @returns {I18N}
|
|
104
81
|
*/
|
|
105
82
|
registerModifier(modifier: Modifier): I18N;
|
|
106
83
|
/**
|
|
107
84
|
* Registers all modifiers in given array.
|
|
108
|
-
* @param modifiers
|
|
109
|
-
* @returns {I18N}
|
|
110
85
|
*/
|
|
111
86
|
registerModifiers(modifiers: Array<Modifier>): I18N;
|
|
112
87
|
/**
|
|
113
88
|
* Unregisters given modifier.
|
|
114
|
-
* @param name
|
|
115
|
-
* @returns {I18N}
|
|
116
89
|
*/
|
|
117
90
|
unregisterModifier(name: string): I18N;
|
|
118
91
|
/**
|
|
119
92
|
* Registers single processor.
|
|
120
|
-
* @returns {I18N}
|
|
121
93
|
*/
|
|
122
94
|
registerProcessor(processor: Processor): I18N;
|
|
123
95
|
/**
|
|
124
96
|
* Registers all processors in given array.
|
|
125
|
-
* @param processors
|
|
126
|
-
* @returns {I18N}
|
|
127
97
|
*/
|
|
128
98
|
registerProcessors(processors: Array<Processor>): I18N;
|
|
129
99
|
/**
|
|
130
100
|
* Unregisters given processor.
|
|
131
|
-
* @param name
|
|
132
|
-
* @returns {I18N}
|
|
133
101
|
*/
|
|
134
102
|
unregisterProcessor(name: string): I18N;
|
|
135
103
|
/**
|
|
136
104
|
* Returns default formats
|
|
137
|
-
* @returns {{date: string, time: string, datetime: string, number: string}}
|
|
138
105
|
*/
|
|
139
106
|
getDefaultFormats(): Formats;
|
|
140
107
|
/**
|
package/I18n.js
CHANGED
|
@@ -9,15 +9,15 @@ exports.default = void 0;
|
|
|
9
9
|
|
|
10
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _accounting = _interopRequireDefault(require("accounting"));
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var fecha = _interopRequireWildcard(require("fecha"));
|
|
15
15
|
|
|
16
16
|
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
17
17
|
|
|
18
|
-
var
|
|
18
|
+
var _assign = _interopRequireDefault(require("lodash/assign"));
|
|
19
19
|
|
|
20
|
-
var
|
|
20
|
+
var _get = _interopRequireDefault(require("lodash/get"));
|
|
21
21
|
|
|
22
22
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
23
23
|
|
|
@@ -33,17 +33,16 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
33
33
|
*/
|
|
34
34
|
class I18N {
|
|
35
35
|
constructor() {
|
|
36
|
-
(0, _defineProperty2.default)(this, "locale",
|
|
36
|
+
(0, _defineProperty2.default)(this, "locale", null);
|
|
37
37
|
(0, _defineProperty2.default)(this, "defaultFormats", void 0);
|
|
38
38
|
(0, _defineProperty2.default)(this, "translations", void 0);
|
|
39
39
|
(0, _defineProperty2.default)(this, "modifiers", void 0);
|
|
40
40
|
(0, _defineProperty2.default)(this, "processors", void 0);
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
/**
|
|
43
43
|
* If we fail to fetch formats for currently selected locale, these default formats will be used.
|
|
44
44
|
* @type {{date: string, time: string, datetime: string, number: string}}
|
|
45
45
|
*/
|
|
46
|
-
|
|
47
46
|
this.defaultFormats = this.getDefaultFormats();
|
|
48
47
|
/**
|
|
49
48
|
* All currently-loaded translations, for easier (synchronous) access.
|
|
@@ -74,7 +73,7 @@ class I18N {
|
|
|
74
73
|
throw Error("I18N text namespace not defined.");
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
base = (0,
|
|
76
|
+
base = (0, _get.default)(base, "raw.0", base);
|
|
78
77
|
let translation = this.getTranslation(namespace + "." + (0, _shortHash.default)(base));
|
|
79
78
|
|
|
80
79
|
if (!translation) {
|
|
@@ -84,10 +83,9 @@ class I18N {
|
|
|
84
83
|
const hasVariables = base.includes("{") && base.includes("}");
|
|
85
84
|
|
|
86
85
|
if (hasVariables) {
|
|
87
|
-
// @ts-ignore
|
|
88
86
|
return values => {
|
|
89
87
|
const data = {
|
|
90
|
-
translation,
|
|
88
|
+
translation: translation,
|
|
91
89
|
base,
|
|
92
90
|
namespace,
|
|
93
91
|
values,
|
|
@@ -95,8 +93,10 @@ class I18N {
|
|
|
95
93
|
};
|
|
96
94
|
|
|
97
95
|
for (const key in this.processors) {
|
|
98
|
-
|
|
99
|
-
|
|
96
|
+
const processor = this.processors[key];
|
|
97
|
+
|
|
98
|
+
if (processor.canExecute(data)) {
|
|
99
|
+
return processor.execute(data);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
@@ -133,9 +133,6 @@ class I18N {
|
|
|
133
133
|
/**
|
|
134
134
|
* Formats and outputs date.
|
|
135
135
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
136
|
-
* @param value
|
|
137
|
-
* @param outputFormat
|
|
138
|
-
* @param inputFormat
|
|
139
136
|
*/
|
|
140
137
|
|
|
141
138
|
|
|
@@ -144,10 +141,16 @@ class I18N {
|
|
|
144
141
|
outputFormat = this.getDateFormat();
|
|
145
142
|
}
|
|
146
143
|
|
|
144
|
+
if (!inputFormat) {
|
|
145
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
146
|
+
}
|
|
147
|
+
|
|
147
148
|
let parsedValue;
|
|
148
149
|
|
|
149
150
|
if (typeof value === "string") {
|
|
150
151
|
parsedValue = fecha.parse(value, inputFormat);
|
|
152
|
+
} else {
|
|
153
|
+
parsedValue = value;
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
return fecha.format(parsedValue, outputFormat);
|
|
@@ -155,21 +158,24 @@ class I18N {
|
|
|
155
158
|
/**
|
|
156
159
|
* Formats and outputs time.
|
|
157
160
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
158
|
-
* @param value
|
|
159
|
-
* @param outputFormat
|
|
160
|
-
* @param inputFormat
|
|
161
161
|
*/
|
|
162
162
|
|
|
163
163
|
|
|
164
|
-
time(value, outputFormat
|
|
164
|
+
time(value, outputFormat, inputFormat) {
|
|
165
165
|
if (!outputFormat) {
|
|
166
166
|
outputFormat = this.getTimeFormat();
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
if (!inputFormat) {
|
|
170
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
171
|
+
}
|
|
172
|
+
|
|
169
173
|
let parsedValue;
|
|
170
174
|
|
|
171
175
|
if (typeof value === "string") {
|
|
172
176
|
parsedValue = fecha.parse(value, inputFormat);
|
|
177
|
+
} else {
|
|
178
|
+
parsedValue = value;
|
|
173
179
|
}
|
|
174
180
|
|
|
175
181
|
return fecha.format(parsedValue, outputFormat);
|
|
@@ -177,29 +183,30 @@ class I18N {
|
|
|
177
183
|
/**
|
|
178
184
|
* Formats and outputs date/time.
|
|
179
185
|
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
180
|
-
* @param value
|
|
181
|
-
* @param outputFormat
|
|
182
|
-
* @param inputFormat
|
|
183
186
|
*/
|
|
184
187
|
|
|
185
188
|
|
|
186
|
-
dateTime(value, outputFormat
|
|
189
|
+
dateTime(value, outputFormat, inputFormat) {
|
|
187
190
|
if (!outputFormat) {
|
|
188
191
|
outputFormat = this.getDateTimeFormat();
|
|
189
192
|
}
|
|
190
193
|
|
|
194
|
+
if (!inputFormat) {
|
|
195
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
196
|
+
}
|
|
197
|
+
|
|
191
198
|
let parsedValue;
|
|
192
199
|
|
|
193
200
|
if (typeof value === "string") {
|
|
194
201
|
parsedValue = fecha.parse(value, inputFormat);
|
|
202
|
+
} else {
|
|
203
|
+
parsedValue = value;
|
|
195
204
|
}
|
|
196
205
|
|
|
197
206
|
return fecha.format(parsedValue, outputFormat);
|
|
198
207
|
}
|
|
199
208
|
/**
|
|
200
209
|
* Outputs formatted number as amount of price.
|
|
201
|
-
* @param value
|
|
202
|
-
* @param outputFormat
|
|
203
210
|
*/
|
|
204
211
|
|
|
205
212
|
|
|
@@ -207,7 +214,7 @@ class I18N {
|
|
|
207
214
|
if (!outputFormat) {
|
|
208
215
|
outputFormat = this.getPriceFormat();
|
|
209
216
|
} else {
|
|
210
|
-
outputFormat = (0,
|
|
217
|
+
outputFormat = (0, _assign.default)({}, this.defaultFormats.price, outputFormat);
|
|
211
218
|
} // Convert placeholders to accounting's placeholders.
|
|
212
219
|
|
|
213
220
|
|
|
@@ -218,8 +225,6 @@ class I18N {
|
|
|
218
225
|
}
|
|
219
226
|
/**
|
|
220
227
|
* Outputs formatted number.
|
|
221
|
-
* @param value
|
|
222
|
-
* @param outputFormat
|
|
223
228
|
*/
|
|
224
229
|
|
|
225
230
|
|
|
@@ -227,7 +232,7 @@ class I18N {
|
|
|
227
232
|
if (!outputFormat) {
|
|
228
233
|
outputFormat = this.getNumberFormat();
|
|
229
234
|
} else {
|
|
230
|
-
outputFormat = (0,
|
|
235
|
+
outputFormat = (0, _assign.default)({}, this.defaultFormats.number, outputFormat);
|
|
231
236
|
}
|
|
232
237
|
|
|
233
238
|
return _accounting.default.formatNumber(
|
|
@@ -238,17 +243,18 @@ class I18N {
|
|
|
238
243
|
}
|
|
239
244
|
/**
|
|
240
245
|
* Returns translation for given text key.
|
|
241
|
-
* @param key
|
|
242
|
-
* @returns {*|string}
|
|
243
246
|
*/
|
|
244
247
|
|
|
245
248
|
|
|
246
249
|
getTranslation(key) {
|
|
250
|
+
if (!key) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
|
|
247
254
|
return this.translations[key];
|
|
248
255
|
}
|
|
249
256
|
/**
|
|
250
257
|
* Returns all translations for current locale.
|
|
251
|
-
* @returns {*|{}}
|
|
252
258
|
*/
|
|
253
259
|
|
|
254
260
|
|
|
@@ -257,7 +263,6 @@ class I18N {
|
|
|
257
263
|
}
|
|
258
264
|
/**
|
|
259
265
|
* Returns true if given key has a translation for currently set locale.
|
|
260
|
-
* @param key
|
|
261
266
|
*/
|
|
262
267
|
|
|
263
268
|
|
|
@@ -266,9 +271,6 @@ class I18N {
|
|
|
266
271
|
}
|
|
267
272
|
/**
|
|
268
273
|
* Sets translation for given text key.
|
|
269
|
-
* @param key
|
|
270
|
-
* @param translation
|
|
271
|
-
* @returns {I18N}
|
|
272
274
|
*/
|
|
273
275
|
|
|
274
276
|
|
|
@@ -278,7 +280,6 @@ class I18N {
|
|
|
278
280
|
}
|
|
279
281
|
/**
|
|
280
282
|
* Sets translations that will be used.
|
|
281
|
-
* @returns {*|{}}
|
|
282
283
|
*/
|
|
283
284
|
|
|
284
285
|
|
|
@@ -288,7 +289,6 @@ class I18N {
|
|
|
288
289
|
}
|
|
289
290
|
/**
|
|
290
291
|
* Clears all translations.
|
|
291
|
-
* @returns {*|{}}
|
|
292
292
|
*/
|
|
293
293
|
|
|
294
294
|
|
|
@@ -298,12 +298,11 @@ class I18N {
|
|
|
298
298
|
}
|
|
299
299
|
/**
|
|
300
300
|
* Merges given translations object with already existing.
|
|
301
|
-
* @returns {*|{}}
|
|
302
301
|
*/
|
|
303
302
|
|
|
304
303
|
|
|
305
304
|
mergeTranslations(translations) {
|
|
306
|
-
return (0,
|
|
305
|
+
return (0, _assign.default)(this.translations, translations);
|
|
307
306
|
}
|
|
308
307
|
/**
|
|
309
308
|
* Returns currently selected locale (locale's key).
|
|
@@ -324,7 +323,6 @@ class I18N {
|
|
|
324
323
|
}
|
|
325
324
|
/**
|
|
326
325
|
* Registers single modifier.
|
|
327
|
-
* @returns {I18N}
|
|
328
326
|
*/
|
|
329
327
|
|
|
330
328
|
|
|
@@ -334,8 +332,6 @@ class I18N {
|
|
|
334
332
|
}
|
|
335
333
|
/**
|
|
336
334
|
* Registers all modifiers in given array.
|
|
337
|
-
* @param modifiers
|
|
338
|
-
* @returns {I18N}
|
|
339
335
|
*/
|
|
340
336
|
|
|
341
337
|
|
|
@@ -345,8 +341,6 @@ class I18N {
|
|
|
345
341
|
}
|
|
346
342
|
/**
|
|
347
343
|
* Unregisters given modifier.
|
|
348
|
-
* @param name
|
|
349
|
-
* @returns {I18N}
|
|
350
344
|
*/
|
|
351
345
|
|
|
352
346
|
|
|
@@ -356,7 +350,6 @@ class I18N {
|
|
|
356
350
|
}
|
|
357
351
|
/**
|
|
358
352
|
* Registers single processor.
|
|
359
|
-
* @returns {I18N}
|
|
360
353
|
*/
|
|
361
354
|
|
|
362
355
|
|
|
@@ -366,8 +359,6 @@ class I18N {
|
|
|
366
359
|
}
|
|
367
360
|
/**
|
|
368
361
|
* Registers all processors in given array.
|
|
369
|
-
* @param processors
|
|
370
|
-
* @returns {I18N}
|
|
371
362
|
*/
|
|
372
363
|
|
|
373
364
|
|
|
@@ -377,8 +368,6 @@ class I18N {
|
|
|
377
368
|
}
|
|
378
369
|
/**
|
|
379
370
|
* Unregisters given processor.
|
|
380
|
-
* @param name
|
|
381
|
-
* @returns {I18N}
|
|
382
371
|
*/
|
|
383
372
|
|
|
384
373
|
|
|
@@ -388,7 +377,6 @@ class I18N {
|
|
|
388
377
|
}
|
|
389
378
|
/**
|
|
390
379
|
* Returns default formats
|
|
391
|
-
* @returns {{date: string, time: string, datetime: string, number: string}}
|
|
392
380
|
*/
|
|
393
381
|
|
|
394
382
|
|
|
@@ -417,7 +405,7 @@ class I18N {
|
|
|
417
405
|
|
|
418
406
|
|
|
419
407
|
getDateFormat() {
|
|
420
|
-
return (0,
|
|
408
|
+
return (0, _get.default)(this.locale, "formats.date", this.defaultFormats.date);
|
|
421
409
|
}
|
|
422
410
|
/**
|
|
423
411
|
* Returns current format to be used when outputting time.
|
|
@@ -425,7 +413,7 @@ class I18N {
|
|
|
425
413
|
|
|
426
414
|
|
|
427
415
|
getTimeFormat() {
|
|
428
|
-
return (0,
|
|
416
|
+
return (0, _get.default)(this.locale, "formats.time", this.defaultFormats.time);
|
|
429
417
|
}
|
|
430
418
|
/**
|
|
431
419
|
* Returns current format to be used when outputting date/time.
|
|
@@ -433,7 +421,7 @@ class I18N {
|
|
|
433
421
|
|
|
434
422
|
|
|
435
423
|
getDateTimeFormat() {
|
|
436
|
-
return (0,
|
|
424
|
+
return (0, _get.default)(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
437
425
|
}
|
|
438
426
|
/**
|
|
439
427
|
* Returns current format to be used when outputting prices.
|
|
@@ -441,7 +429,7 @@ class I18N {
|
|
|
441
429
|
|
|
442
430
|
|
|
443
431
|
getPriceFormat() {
|
|
444
|
-
return (0,
|
|
432
|
+
return (0, _assign.default)({}, this.defaultFormats.price, (0, _get.default)(this.locale, "formats.price", {}));
|
|
445
433
|
}
|
|
446
434
|
/**
|
|
447
435
|
* Returns current format to be used when outputting numbers.
|
|
@@ -449,7 +437,7 @@ class I18N {
|
|
|
449
437
|
|
|
450
438
|
|
|
451
439
|
getNumberFormat() {
|
|
452
|
-
return (0,
|
|
440
|
+
return (0, _assign.default)({}, this.defaultFormats.number, (0, _get.default)(this.locale, "formats.number", {}));
|
|
453
441
|
}
|
|
454
442
|
|
|
455
443
|
}
|
package/I18n.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["I18n.ts"],"names":["I18N","constructor","defaultFormats","getDefaultFormats","translations","modifiers","processors","translate","base","namespace","Error","translation","getTranslation","hasVariables","includes","values","data","i18n","key","processor","canExecute","execute","ns","date","value","outputFormat","inputFormat","getDateFormat","parsedValue","fecha","parse","format","time","getTimeFormat","dateTime","getDateTimeFormat","price","getPriceFormat","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"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAKA;;AACA;;AACA;;;;;;AANA;AACA;AACA;AACA;;AAsBA;AACA;AACA;AACe,MAAMA,IAAN,CAAW;AAWfC,EAAAA,WAAW,GAAG;AAAA,kDAVU,IAUV;AAAA;AAAA;AAAA;AAAA;;AACjB;AACR;AACA;AACA;AACQ,SAAKC,cAAL,GAAsB,KAAKC,iBAAL,EAAtB;AAEA;AACR;AACA;AACA;;AACQ,SAAKC,YAAL,GAAoB,EAApB;AAEA;AACR;AACA;AACA;;AACQ,SAAKC,SAAL,GAAiB,EAAjB;AAEA;AACR;AACA;AACA;AACA;;AACQ,SAAKC,UAAL,GAAkB,EAAlB;AACH;;AAEMC,EAAAA,SAAS,CAACC,IAA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EAGH,kBAAU,KAAKU,MAAf,EAAuB,gBAAvB,EAAyC,EAAzC,CAHG,CAAP;AAKH;;AA/YqB","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 return (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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["extract.ts"],"names":["getNamespaces","source","regex","m","results","exec","index","lastIndex","allDeclaredNamespaces","variable","RegExp","matchedText","key"],"mappings":";;;;;;;;;AAIA;;AAJA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,aAAa,GAAIC,MAAD,IAAoB;AACtC,QAAMC,KAAK,GAAG,qEAAd;AACA,MAAIC,CAAJ;AAEA,QAAMC,OAA+B,GAAG,EAAxC;;AAEA,SAAO,CAACD,CAAC,GAAGD,KAAK,CAACG,IAAN,CAAWJ,MAAX,CAAL,MAA6B,IAApC,EAA0C;AACtC,QAAIE,CAAC,CAACG,KAAF,KAAYJ,KAAK,CAACK,SAAtB,EAAiC;AAC7BL,MAAAA,KAAK,CAACK,SAAN;AACH;;AAEDH,IAAAA,OAAO,CAACD,CAAC,CAAC,CAAD,CAAF,CAAP,GAAgBA,CAAC,CAAC,CAAD,CAAjB;AACH;;AAED,SAAOC,OAAP;AACH,CAfD;;eAiBgBH,MAAD,IAAoB;AAC/B,QAAMG,OAA+B,GAAG,EAAxC;AACA,QAAMI,qBAAqB,GAAGR,aAAa,CAACC,MAAD,CAA3C;;AAEA,OAAK,MAAMQ,QAAX,IAAuBD,qBAAvB,EAA8C;AAC1C,UAAMN,KAAK,GAAG,IAAIQ,MAAJ,CAAWD,QAAQ,GAAG,SAAtB,EAAiC,GAAjC,CAAd;AAEA,QAAIN,CAAJ;;AACA,WAAO,CAACA,CAAC,GAAGD,KAAK,CAACG,IAAN,CAAWJ,MAAX,CAAL,MAA6B,IAApC,EAA0C;AACtC,UAAIE,CAAC,CAACG,KAAF,KAAYJ,KAAK,CAACK,SAAtB,EAAiC;AAC7BL,QAAAA,KAAK,CAACK,SAAN;AACH,OAHqC,CAKtC;;;AACA,YAAMI,WAAW,GAAGR,CAAC,CAAC,CAAD,CAArB;AACA,YAAMS,GAAG,GAAGJ,qBAAqB,CAACC,QAAD,CAArB,GAAkC,GAAlC,GAAwC,wBAAKE,WAAL,CAApD;AACAP,MAAAA,OAAO,CAACQ,GAAD,CAAP,GAAeD,WAAf;AACH;AACJ;;AAED,SAAOP,OAAP;AACH,C","sourcesContent":["/**\n * Package short-hash has no types.\n */\n// @ts-ignore\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"]}
|
package/extractor/index.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
export interface ExtractorResults {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}
|
|
1
4
|
declare class Extractor {
|
|
2
|
-
glob
|
|
5
|
+
private glob;
|
|
3
6
|
content: string;
|
|
4
7
|
listOnly: boolean;
|
|
5
|
-
setGlob(
|
|
8
|
+
setGlob(value: string): Extractor;
|
|
6
9
|
setContent(content: string): Extractor;
|
|
7
|
-
execute():
|
|
10
|
+
execute(): ExtractorResults;
|
|
8
11
|
setListOnly(flag?: boolean): Extractor;
|
|
9
12
|
}
|
|
10
13
|
export default Extractor;
|
package/extractor/index.js
CHANGED
|
@@ -18,12 +18,12 @@ var _fs = _interopRequireDefault(require("fs"));
|
|
|
18
18
|
class Extractor {
|
|
19
19
|
constructor() {
|
|
20
20
|
(0, _defineProperty2.default)(this, "glob", void 0);
|
|
21
|
-
(0, _defineProperty2.default)(this, "content",
|
|
22
|
-
(0, _defineProperty2.default)(this, "listOnly",
|
|
21
|
+
(0, _defineProperty2.default)(this, "content", "");
|
|
22
|
+
(0, _defineProperty2.default)(this, "listOnly", false);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
setGlob(
|
|
26
|
-
this.glob =
|
|
25
|
+
setGlob(value) {
|
|
26
|
+
this.glob = value;
|
|
27
27
|
return this;
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -35,20 +35,21 @@ class Extractor {
|
|
|
35
35
|
execute() {
|
|
36
36
|
const results = {};
|
|
37
37
|
|
|
38
|
-
if (this.glob) {
|
|
39
|
-
|
|
38
|
+
if (!this.glob) {
|
|
39
|
+
return results;
|
|
40
|
+
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
const contents = _fs.default.readFileSync(path, "utf8");
|
|
42
|
+
const paths = _glob.default.sync(this.glob);
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
paths.forEach(path => {
|
|
45
|
+
const contents = _fs.default.readFileSync(path, "utf8");
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
results[key] = parsed[key];
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
47
|
+
const parsed = (0, _extract.default)(contents);
|
|
51
48
|
|
|
49
|
+
for (const key in parsed) {
|
|
50
|
+
results[key] = parsed[key];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
52
53
|
return results;
|
|
53
54
|
}
|
|
54
55
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["Extractor","setGlob","value","glob","setContent","content","execute","results","paths","sync","forEach","path","contents","fs","readFileSync","parsed","key","setListOnly","flag","listOnly"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AACA;;AAKA,MAAMA,SAAN,CAAgB;AAAA;AAAA;AAAA,mDAEK,EAFL;AAAA,oDAGM,KAHN;AAAA;;AAKLC,EAAAA,OAAO,CAACC,KAAD,EAA2B;AACrC,SAAKC,IAAL,GAAYD,KAAZ;AACA,WAAO,IAAP;AACH;;AAEME,EAAAA,UAAU,CAACC,OAAD,EAA6B;AAC1C,SAAKA,OAAL,GAAeA,OAAf;AACA,WAAO,IAAP;AACH;;AAEMC,EAAAA,OAAO,GAAqB;AAC/B,UAAMC,OAAyB,GAAG,EAAlC;;AAEA,QAAI,CAAC,KAAKJ,IAAV,EAAgB;AACZ,aAAOI,OAAP;AACH;;AACD,UAAMC,KAAK,GAAGL,cAAKM,IAAL,CAAU,KAAKN,IAAf,CAAd;;AACAK,IAAAA,KAAK,CAACE,OAAN,CAAcC,IAAI,IAAI;AAClB,YAAMC,QAAQ,GAAGC,YAAGC,YAAH,CAAgBH,IAAhB,EAAsB,MAAtB,CAAjB;;AACA,YAAMI,MAAM,GAAG,sBAAQH,QAAR,CAAf;;AACA,WAAK,MAAMI,GAAX,IAAkBD,MAAlB,EAA0B;AACtBR,QAAAA,OAAO,CAACS,GAAD,CAAP,GAAeD,MAAM,CAACC,GAAD,CAArB;AACH;AACJ,KAND;AAQA,WAAOT,OAAP;AACH;;AAEMU,EAAAA,WAAW,CAACC,IAAI,GAAG,IAAR,EAAyB;AACvC,SAAKC,QAAL,GAAgBD,IAAhB;AACA,WAAO,IAAP;AACH;;AApCW;;eAuCDlB,S","sourcesContent":["import extract from \"./extract\";\nimport glob from \"glob\";\nimport fs from \"fs\";\n\nexport interface ExtractorResults {\n [key: string]: string;\n}\nclass Extractor {\n private glob: string | undefined;\n public content = \"\";\n public listOnly = false;\n\n public setGlob(value: string): Extractor {\n this.glob = value;\n return this;\n }\n\n public setContent(content: string): Extractor {\n this.content = content;\n return this;\n }\n\n public execute(): ExtractorResults {\n const results: ExtractorResults = {};\n\n if (!this.glob) {\n return results;\n }\n const paths = glob.sync(this.glob);\n paths.forEach(path => {\n const contents = fs.readFileSync(path, \"utf8\");\n const parsed = extract(contents);\n for (const key in parsed) {\n results[key] = parsed[key];\n }\n });\n\n return results;\n }\n\n public setListOnly(flag = true): Extractor {\n this.listOnly = flag;\n return this;\n }\n}\n\nexport default Extractor;\n"]}
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["i18n","I18n","defaultModifiers"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA,MAAMA,IAAI,GAAG,IAAIC,aAAJ,EAAb;AAEA,MAAMC,gBAAgB,GAAG,wBAAiB;AAAEF,EAAAA;AAAF,CAAjB,CAAzB;;eAEeA,I","sourcesContent":["import I18n from \"./I18n\";\nimport defaultProcessor from \"./processors/default\";\nimport modifiersFactory from \"./modifiers\";\n\nconst i18n = new I18n();\n\nconst defaultModifiers = modifiersFactory({ i18n });\n\nexport default i18n;\n\nexport { I18n, defaultModifiers, defaultProcessor };\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["countModifier.ts"],"names":["name","execute","value","parameters","i","length","current","numbers","split","String"],"mappings":";;;;;;;eAEe,MAAgB;AAC3B,SAAO;AACHA,IAAAA,IAAI,EAAE,OADH;;AAEHC,IAAAA,OAAO,CAACC,KAAD,EAAgBC,UAAhB,EAA2C;AAC9C;AACA,WAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGD,UAAU,CAACE,MAA/B,EAAuCD,CAAC,GAAGA,CAAC,GAAG,CAA/C,EAAkD;AAC9C,cAAME,OAAO,GAAGH,UAAU,CAACC,CAAD,CAA1B;;AACA,YAAIE,OAAO,KAAK,SAAhB,EAA2B;AACvB,iBAAOJ,KAAK,GAAG,GAAR,GAAcC,UAAU,CAACC,CAAC,GAAG,CAAL,CAA/B;AACH;;AAED,cAAMG,OAAO,GAAGD,OAAO,CAACE,KAAR,CAAc,GAAd,CAAhB,CAN8C,CAQ9C;;AACA,YAAID,OAAO,CAACF,MAAR,KAAmB,CAAvB,EAA0B;AACtB,cAAIH,KAAK,IAAIK,OAAO,CAAC,CAAD,CAAhB,IAAuBL,KAAK,IAAIK,OAAO,CAAC,CAAD,CAA3C,EAAgD;AAC5C,mBAAOL,KAAK,GAAG,GAAR,GAAcC,UAAU,CAACC,CAAC,GAAG,CAAL,CAA/B;AACH;;AACD;AACH;;AAED,YAAIK,MAAM,CAACP,KAAD,CAAN,KAAkBK,OAAO,CAAC,CAAD,CAA7B,EAAkC;AAC9B,iBAAOL,KAAK,GAAG,GAAR,GAAcC,UAAU,CAACC,CAAC,GAAG,CAAL,CAA/B;AACH;AACJ,OArB6C,CAuB9C;;;AACA,aAAOF,KAAP;AACH;;AA3BE,GAAP;AA6BH,C","sourcesContent":["import { Modifier } from \"~/types\";\n\nexport default (): Modifier => {\n return {\n name: \"count\",\n execute(value: string, parameters: Array<string>) {\n // Numbers can be single number or ranges.\n for (let i = 0; i < parameters.length; i = i + 2) {\n const current = parameters[i];\n if (current === \"default\") {\n return value + \" \" + parameters[i + 1];\n }\n\n const numbers = current.split(\"-\");\n\n // If we are dealing with a numbers range, then let's check if we are in it.\n if (numbers.length === 2) {\n if (value >= numbers[0] && value <= numbers[1]) {\n return value + \" \" + parameters[i + 1];\n }\n continue;\n }\n\n if (String(value) === numbers[0]) {\n return value + \" \" + parameters[i + 1];\n }\n }\n\n // If we didn't match any condition, let's just remove the received value.\n return value;\n }\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["dateModifier.ts"],"names":["i18n","name","execute","value","date"],"mappings":";;;;;;;eAEe,CAAC;AAAEA,EAAAA;AAAF,CAAD,MAA0C;AACrDC,EAAAA,IAAI,EAAE,MAD+C;;AAErDC,EAAAA,OAAO,CAACC,KAAD,EAAQ;AACX,WAAOH,IAAI,CAACI,IAAL,CAAUD,KAAV,CAAP;AACH;;AAJoD,CAA1C,C","sourcesContent":["import { Modifier, ModifierOptions } from \"~/types\";\n\nexport default ({ i18n }: ModifierOptions): Modifier => ({\n name: \"date\",\n execute(value) {\n return i18n.date(value);\n }\n});\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["dateTimeModifier.ts"],"names":["i18n","name","execute","value","dateTime"],"mappings":";;;;;;;eAEe,CAAC;AAAEA,EAAAA;AAAF,CAAD,MAA0C;AACrDC,EAAAA,IAAI,EAAE,UAD+C;;AAErDC,EAAAA,OAAO,CAACC,KAAD,EAAgB;AACnB,WAAOH,IAAI,CAACI,QAAL,CAAcD,KAAd,CAAP;AACH;;AAJoD,CAA1C,C","sourcesContent":["import { Modifier, ModifierOptions } from \"~/types\";\n\nexport default ({ i18n }: ModifierOptions): Modifier => ({\n name: \"dateTime\",\n execute(value: string) {\n return i18n.dateTime(value);\n }\n});\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["genderModifier.ts"],"names":["name","execute","value","parameters"],"mappings":";;;;;;;eAEe,MAAgB;AAC3B,SAAO;AACHA,IAAAA,IAAI,EAAE,QADH;;AAEHC,IAAAA,OAAO,CAACC,KAAD,EAAgBC,UAAhB,EAA2C;AAC9C,aAAOD,KAAK,KAAK,MAAV,GAAmBC,UAAU,CAAC,CAAD,CAA7B,GAAmCA,UAAU,CAAC,CAAD,CAApD;AACH;;AAJE,GAAP;AAMH,C","sourcesContent":["import { Modifier } from \"~/types\";\n\nexport default (): Modifier => {\n return {\n name: \"gender\",\n execute(value: string, parameters: Array<string>) {\n return value === \"male\" ? parameters[0] : parameters[1];\n }\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["ifModifier.ts"],"names":["name","execute","value","parameters"],"mappings":";;;;;;;eAEe,MAAgB;AAC3B,SAAO;AACHA,IAAAA,IAAI,EAAE,IADH;;AAEHC,IAAAA,OAAO,CAACC,KAAD,EAAgBC,UAAhB,EAA2C;AAC9C,aAAOD,KAAK,KAAKC,UAAU,CAAC,CAAD,CAApB,GAA0BA,UAAU,CAAC,CAAD,CAApC,GAA0CA,UAAU,CAAC,CAAD,CAAV,IAAiB,EAAlE;AACH;;AAJE,GAAP;AAMH,C","sourcesContent":["import { Modifier } from \"~/types\";\n\nexport default (): Modifier => {\n return {\n name: \"if\",\n execute(value: string, parameters: Array<string>) {\n return value === parameters[0] ? parameters[1] : parameters[2] || \"\";\n }\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["options"],"mappings":";;;;;;;;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AATA;eAYgBA,OAAD,IAA0C,CACrD,6BADqD,EAErD,8BAFqD,EAGrD,0BAHqD,EAIrD,8BAJqD,EAKrD,2BAAaA,OAAb,CALqD,EAMrD,+BAAiBA,OAAjB,CANqD,EAOrD,2BAAaA,OAAb,CAPqD,EAQrD,6BAAeA,OAAf,CARqD,EASrD,4BAAcA,OAAd,CATqD,C","sourcesContent":["// Built-in modifiers\nimport countModifiers from \"./countModifier\";\nimport genderModifier from \"./genderModifier\";\nimport ifModifier from \"./ifModifier\";\nimport pluralModifier from \"./pluralModifier\";\nimport dateModifier from \"./dateModifier\";\nimport dateTimeModifier from \"./dateTimeModifier\";\nimport timeModifier from \"./timeModifier\";\nimport numberModifier from \"./numberModifier\";\nimport priceModifier from \"./priceModifier\";\nimport { Modifier, ModifierOptions } from \"~/types\";\n\nexport default (options: ModifierOptions): Modifier[] => [\n countModifiers(),\n genderModifier(),\n ifModifier(),\n pluralModifier(),\n dateModifier(options),\n dateTimeModifier(options),\n timeModifier(options),\n numberModifier(options),\n priceModifier(options)\n];\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["numberModifier.ts"],"names":["i18n","name","execute","value","number"],"mappings":";;;;;;;eAEe,CAAC;AAAEA,EAAAA;AAAF,CAAD,MAA0C;AACrDC,EAAAA,IAAI,EAAE,QAD+C;;AAErDC,EAAAA,OAAO,CAACC,KAAD,EAAgB;AACnB,WAAOH,IAAI,CAACI,MAAL,CAAYD,KAAZ,CAAP;AACH;;AAJoD,CAA1C,C","sourcesContent":["import { Modifier, ModifierOptions } from \"~/types\";\n\nexport default ({ i18n }: ModifierOptions): Modifier => ({\n name: \"number\",\n execute(value: string) {\n return i18n.number(value);\n }\n});\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["pluralModifier.ts"],"names":["name","execute","value","parameters","i","length","current","numbers","split","String"],"mappings":";;;;;;;eAEe,MAAgB;AAC3B,SAAO;AACHA,IAAAA,IAAI,EAAE,QADH;;AAEHC,IAAAA,OAAO,CAACC,KAAD,EAAgBC,UAAhB,EAA2C;AAC9C;AACA,WAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGD,UAAU,CAACE,MAA/B,EAAuCD,CAAC,GAAGA,CAAC,GAAG,CAA/C,EAAkD;AAC9C,cAAME,OAAO,GAAGH,UAAU,CAACC,CAAD,CAA1B;;AACA,YAAIE,OAAO,KAAK,SAAhB,EAA2B;AACvB,iBAAOH,UAAU,CAACC,CAAC,GAAG,CAAL,CAAjB;AACH;;AAED,cAAMG,OAAO,GAAGD,OAAO,CAACE,KAAR,CAAc,GAAd,CAAhB,CAN8C,CAQ9C;;AACA,YAAID,OAAO,CAACF,MAAR,KAAmB,CAAvB,EAA0B;AACtB,cAAIH,KAAK,IAAIK,OAAO,CAAC,CAAD,CAAhB,IAAuBL,KAAK,IAAIK,OAAO,CAAC,CAAD,CAA3C,EAAgD;AAC5C,mBAAOJ,UAAU,CAACC,CAAC,GAAG,CAAL,CAAjB;AACH;;AACD;AACH;;AAED,YAAIK,MAAM,CAACP,KAAD,CAAN,KAAkBK,OAAO,CAAC,CAAD,CAA7B,EAAkC;AAC9B,iBAAOJ,UAAU,CAACC,CAAC,GAAG,CAAL,CAAjB;AACH;AACJ;;AAED,aAAO,EAAP;AACH;;AA1BE,GAAP;AA4BH,C","sourcesContent":["import { Modifier } from \"~/types\";\n\nexport default (): Modifier => {\n return {\n name: \"plural\",\n execute(value: string, parameters: Array<string>) {\n // Numbers can be single number or ranges.\n for (let i = 0; i < parameters.length; i = i + 2) {\n const current = parameters[i];\n if (current === \"default\") {\n return parameters[i + 1];\n }\n\n const numbers = current.split(\"-\");\n\n // If we are dealing with a numbers range, then let's check if we are in it.\n if (numbers.length === 2) {\n if (value >= numbers[0] && value <= numbers[1]) {\n return parameters[i + 1];\n }\n continue;\n }\n\n if (String(value) === numbers[0]) {\n return parameters[i + 1];\n }\n }\n\n return \"\";\n }\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["priceModifier.ts"],"names":["i18n","name","execute","value","price"],"mappings":";;;;;;;eAEe,CAAC;AAAEA,EAAAA;AAAF,CAAD,MAA0C;AACrDC,EAAAA,IAAI,EAAE,OAD+C;;AAErDC,EAAAA,OAAO,CAACC,KAAD,EAAgB;AACnB,WAAOH,IAAI,CAACI,KAAL,CAAWD,KAAX,CAAP;AACH;;AAJoD,CAA1C,C","sourcesContent":["import { Modifier, ModifierOptions } from \"~/types\";\n\nexport default ({ i18n }: ModifierOptions): Modifier => ({\n name: \"price\",\n execute(value: string) {\n return i18n.price(value);\n }\n});\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["timeModifier.ts"],"names":["i18n","name","execute","value","time"],"mappings":";;;;;;;eAEe,CAAC;AAAEA,EAAAA;AAAF,CAAD,MAA0C;AACrDC,EAAAA,IAAI,EAAE,MAD+C;;AAErDC,EAAAA,OAAO,CAACC,KAAD,EAAgB;AACnB,WAAOH,IAAI,CAACI,IAAL,CAAUD,KAAV,CAAP;AACH;;AAJoD,CAA1C,C","sourcesContent":["import { Modifier, ModifierOptions } from \"~/types\";\n\nexport default ({ i18n }: ModifierOptions): Modifier => ({\n name: \"time\",\n execute(value: string) {\n return i18n.time(value);\n }\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/i18n",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.25.0-beta.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
],
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@babel/runtime": "7.
|
|
16
|
+
"@babel/runtime": "7.17.2",
|
|
17
17
|
"accounting": "0.4.1",
|
|
18
18
|
"fecha": "2.3.3",
|
|
19
19
|
"lodash": "4.17.21",
|
|
@@ -31,12 +31,13 @@
|
|
|
31
31
|
"@babel/register": "^7.16.0",
|
|
32
32
|
"@types/accounting": "^0.4.2",
|
|
33
33
|
"@types/lodash": "^4.14.178",
|
|
34
|
-
"@webiny/cli": "^5.
|
|
35
|
-
"@webiny/project-utils": "^5.
|
|
34
|
+
"@webiny/cli": "^5.25.0-beta.0",
|
|
35
|
+
"@webiny/project-utils": "^5.25.0-beta.0",
|
|
36
36
|
"babel-plugin-lodash": "^3.3.4",
|
|
37
37
|
"glob": "^7.1.2",
|
|
38
38
|
"rimraf": "^3.0.2",
|
|
39
|
-
"
|
|
39
|
+
"ttypescript": "^1.5.13",
|
|
40
|
+
"typescript": "4.5.5"
|
|
40
41
|
},
|
|
41
42
|
"publishConfig": {
|
|
42
43
|
"access": "public",
|
|
@@ -46,5 +47,5 @@
|
|
|
46
47
|
"build": "yarn webiny run build",
|
|
47
48
|
"watch": "yarn webiny run watch"
|
|
48
49
|
},
|
|
49
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "2d3e7833575e88fde77d84e5490e746933a5ec28"
|
|
50
51
|
}
|
package/processors/default.js
CHANGED
|
@@ -7,21 +7,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.default = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
|
13
|
-
|
|
14
|
-
var _startsWith2 = _interopRequireDefault(require("lodash/startsWith"));
|
|
10
|
+
var _trim = _interopRequireDefault(require("lodash/trim"));
|
|
15
11
|
|
|
16
12
|
const processTextPart = (part, values, modifiers) => {
|
|
17
|
-
if (
|
|
13
|
+
if (part.startsWith("{") === false) {
|
|
18
14
|
return part;
|
|
19
15
|
}
|
|
20
16
|
|
|
21
|
-
const parts = (0,
|
|
17
|
+
const parts = (0, _trim.default)(part, "{}").split("|");
|
|
22
18
|
const [variable, modifier] = parts;
|
|
23
19
|
|
|
24
|
-
if (!
|
|
20
|
+
if (!values[variable]) {
|
|
25
21
|
return `{${variable}}`;
|
|
26
22
|
}
|
|
27
23
|
|
|
@@ -33,6 +29,10 @@ const processTextPart = (part, values, modifiers) => {
|
|
|
33
29
|
const parameters = modifier.split(":");
|
|
34
30
|
const name = parameters.shift();
|
|
35
31
|
|
|
32
|
+
if (!name) {
|
|
33
|
+
return output.value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
36
|
if (modifiers[name]) {
|
|
37
37
|
const modifier = modifiers[name];
|
|
38
38
|
output.value = modifier.execute(output.value, parameters);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["default.ts"],"names":["processTextPart","part","values","modifiers","startsWith","parts","split","variable","modifier","output","value","parameters","name","shift","execute","processor","canExecute","data","key","Date","translation","reduce","carry","i18n"],"mappings":";;;;;;;;;AAAA;;AAGA,MAAMA,eAAe,GAAG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KAAK,IAFV,IAGAA,KAAK,YAAYS,IAJrB,EAKE;AACE;AACH;;AACD,aAAO,KAAP;AACH;;AAED,WAAO,IAAP;AACH,GAjBwB;;AAkBzBL,EAAAA,OAAO,CAACG,IAAD,EAAO;AACV,UAAMZ,KAAK,GAAGY,IAAI,CAACG,WAAL,CAAiBd,KAAjB,CAAuB,SAAvB,CAAd;AACA,WAAOD,KAAK,CAACgB,MAAN,CACH,CAACC,KAAD,EAAQrB,IAAR,KAAiBqB,KAAK,GAAGtB,eAAe,CAACC,IAAD,EAAOgB,IAAI,CAACf,MAAZ,EAAoBe,IAAI,CAACM,IAAL,CAAUpB,SAA9B,CADrC,EAEH,EAFG,CAAP;AAIH;;AAxBwB,CAA7B;eA2BeY,S","sourcesContent":["import lodashTrim from \"lodash/trim\";\nimport { Modifier, Processor } from \"~/types\";\n\nconst processTextPart = (\n part: string,\n values: Record<string, any>,\n modifiers: Record<string, Modifier>\n): string => {\n if (part.startsWith(\"{\") === false) {\n return part;\n }\n\n const parts = lodashTrim(part, \"{}\").split(\"|\");\n\n const [variable, modifier] = parts;\n\n if (!values[variable]) {\n return `{${variable}}`;\n }\n\n const output = {\n value: values[variable]\n };\n\n if (modifier) {\n const parameters: string[] = modifier.split(\":\");\n const name = parameters.shift();\n if (!name) {\n return output.value;\n }\n if (modifiers[name]) {\n const modifier = modifiers[name];\n output.value = modifier.execute(output.value, parameters);\n }\n }\n\n return output.value;\n};\n\nconst processor: Processor = {\n name: \"default\",\n canExecute(data) {\n for (const key in data.values) {\n const value = data.values[key];\n if (\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n value === null ||\n value instanceof Date\n ) {\n continue;\n }\n return false;\n }\n\n return true;\n },\n execute(data) {\n const parts = data.translation.split(/({.*?})/);\n return parts.reduce(\n (carry, part) => carry + processTextPart(part, data.values, data.i18n.modifiers),\n \"\"\n );\n }\n};\n\nexport default processor;\n"]}
|
package/types.d.ts
CHANGED
|
@@ -1,54 +1,56 @@
|
|
|
1
1
|
import I18N from "./I18n";
|
|
2
2
|
import { ReactElement } from "react";
|
|
3
|
-
export
|
|
3
|
+
export interface I18NDataValues {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
export interface I18NData {
|
|
4
7
|
translation: string;
|
|
5
8
|
base: string;
|
|
6
9
|
namespace: string;
|
|
7
|
-
values:
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
};
|
|
10
|
+
values: I18NDataValues;
|
|
10
11
|
i18n: I18N;
|
|
11
|
-
}
|
|
12
|
+
}
|
|
12
13
|
export interface ModifierOptions {
|
|
13
14
|
i18n: I18N;
|
|
14
15
|
}
|
|
15
16
|
/**
|
|
16
|
-
* @name Modifier
|
|
17
17
|
* @description I18N Modifier - used for modifying text dynamically.
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
19
|
+
export interface Modifier {
|
|
20
20
|
name: string;
|
|
21
21
|
execute: (...args: any[]) => string;
|
|
22
|
-
}
|
|
22
|
+
}
|
|
23
|
+
export declare type ProcessorResult = string | ReactElement;
|
|
23
24
|
/**
|
|
24
|
-
* @name Processor
|
|
25
25
|
* @description I18N Processor - used for outputting text.
|
|
26
26
|
*/
|
|
27
|
-
export
|
|
27
|
+
export interface Processor {
|
|
28
28
|
name: string;
|
|
29
|
-
canExecute
|
|
30
|
-
execute: (data: I18NData) =>
|
|
31
|
-
}
|
|
32
|
-
export
|
|
29
|
+
canExecute: (data: I18NData) => boolean;
|
|
30
|
+
execute: (data: I18NData) => ProcessorResult;
|
|
31
|
+
}
|
|
32
|
+
export interface NumberFormat {
|
|
33
33
|
decimal: string;
|
|
34
34
|
thousand: string;
|
|
35
35
|
precision: number;
|
|
36
|
-
}
|
|
37
|
-
export
|
|
36
|
+
}
|
|
37
|
+
export interface PriceFormat {
|
|
38
38
|
symbol: string;
|
|
39
39
|
format: string;
|
|
40
40
|
decimal: string;
|
|
41
41
|
thousand: string;
|
|
42
42
|
precision: number;
|
|
43
|
-
}
|
|
44
|
-
export
|
|
43
|
+
}
|
|
44
|
+
export interface Formats {
|
|
45
45
|
date: string;
|
|
46
46
|
time: string;
|
|
47
47
|
datetime: string;
|
|
48
48
|
price: PriceFormat;
|
|
49
49
|
number: NumberFormat;
|
|
50
|
-
}
|
|
51
|
-
export
|
|
52
|
-
|
|
50
|
+
}
|
|
51
|
+
export interface Translator {
|
|
52
|
+
(base: any): any;
|
|
53
|
+
}
|
|
54
|
+
export interface Translations {
|
|
53
55
|
[key: string]: string;
|
|
54
|
-
}
|
|
56
|
+
}
|
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|