@webiny/i18n 0.0.0-mt-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/I18n.d.ts +160 -0
- package/I18n.js +448 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/extractor/extract.d.ts +2 -0
- package/extractor/extract.js +57 -0
- package/extractor/index.d.ts +10 -0
- package/extractor/index.js +63 -0
- package/index.d.ts +6 -0
- package/index.js +34 -0
- package/modifiers/countModifier.d.ts +3 -0
- package/modifiers/countModifier.js +39 -0
- package/modifiers/dateModifier.d.ts +5 -0
- package/modifiers/dateModifier.js +19 -0
- package/modifiers/dateTimeModifier.d.ts +5 -0
- package/modifiers/dateTimeModifier.js +19 -0
- package/modifiers/genderModifier.d.ts +3 -0
- package/modifiers/genderModifier.js +15 -0
- package/modifiers/ifModifier.d.ts +3 -0
- package/modifiers/ifModifier.js +15 -0
- package/modifiers/index.d.ts +2 -0
- package/modifiers/index.js +31 -0
- package/modifiers/numberModifier.d.ts +5 -0
- package/modifiers/numberModifier.js +19 -0
- package/modifiers/pluralModifier.d.ts +3 -0
- package/modifiers/pluralModifier.js +38 -0
- package/modifiers/priceModifier.d.ts +5 -0
- package/modifiers/priceModifier.js +19 -0
- package/modifiers/timeModifier.d.ts +5 -0
- package/modifiers/timeModifier.js +19 -0
- package/package.json +48 -0
- package/processors/default.d.ts +3 -0
- package/processors/default.js +69 -0
- package/types.d.ts +51 -0
- package/types.js +5 -0
package/I18n.d.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Formats, Modifier, NumberFormat, PriceFormat, Processor, Translations, Translator } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Main class used for all I18n needs.
|
|
4
|
+
*/
|
|
5
|
+
export default class I18N {
|
|
6
|
+
locale?: string;
|
|
7
|
+
defaultFormats: Formats;
|
|
8
|
+
translations: Translations;
|
|
9
|
+
modifiers: {
|
|
10
|
+
[name: string]: Modifier;
|
|
11
|
+
};
|
|
12
|
+
processors: {
|
|
13
|
+
[name: string]: Processor;
|
|
14
|
+
};
|
|
15
|
+
constructor();
|
|
16
|
+
translate(base: string, namespace?: string): any;
|
|
17
|
+
namespace(namespace: string): Translator;
|
|
18
|
+
ns(namespace: string): Translator;
|
|
19
|
+
/**
|
|
20
|
+
* Formats and outputs date.
|
|
21
|
+
* 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
|
+
*/
|
|
26
|
+
date(value: Date | string | number, outputFormat?: string, inputFormat?: "YYYY-MM-DDTHH:mm:ss.SSSZ"): string;
|
|
27
|
+
/**
|
|
28
|
+
* Formats and outputs time.
|
|
29
|
+
* 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
|
+
*/
|
|
34
|
+
time(value: Date | string | number, outputFormat?: string, inputFormat?: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Formats and outputs date/time.
|
|
37
|
+
* 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
|
+
*/
|
|
42
|
+
dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Outputs formatted number as amount of price.
|
|
45
|
+
* @param value
|
|
46
|
+
* @param outputFormat
|
|
47
|
+
*/
|
|
48
|
+
price(value: string | number, outputFormat?: any): string;
|
|
49
|
+
/**
|
|
50
|
+
* Outputs formatted number.
|
|
51
|
+
* @param value
|
|
52
|
+
* @param outputFormat
|
|
53
|
+
*/
|
|
54
|
+
number(value: string | number, outputFormat?: NumberFormat): string;
|
|
55
|
+
/**
|
|
56
|
+
* Returns translation for given text key.
|
|
57
|
+
* @param key
|
|
58
|
+
* @returns {*|string}
|
|
59
|
+
*/
|
|
60
|
+
getTranslation(key?: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Returns all translations for current locale.
|
|
63
|
+
* @returns {*|{}}
|
|
64
|
+
*/
|
|
65
|
+
getTranslations(): Translations;
|
|
66
|
+
/**
|
|
67
|
+
* Returns true if given key has a translation for currently set locale.
|
|
68
|
+
* @param key
|
|
69
|
+
*/
|
|
70
|
+
hasTranslation(key: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Sets translation for given text key.
|
|
73
|
+
* @param key
|
|
74
|
+
* @param translation
|
|
75
|
+
* @returns {I18N}
|
|
76
|
+
*/
|
|
77
|
+
setTranslation(key: string, translation: string): I18N;
|
|
78
|
+
/**
|
|
79
|
+
* Sets translations that will be used.
|
|
80
|
+
* @returns {*|{}}
|
|
81
|
+
*/
|
|
82
|
+
setTranslations(translations: Translations): I18N;
|
|
83
|
+
/**
|
|
84
|
+
* Clears all translations.
|
|
85
|
+
* @returns {*|{}}
|
|
86
|
+
*/
|
|
87
|
+
clearTranslations(): I18N;
|
|
88
|
+
/**
|
|
89
|
+
* Merges given translations object with already existing.
|
|
90
|
+
* @returns {*|{}}
|
|
91
|
+
*/
|
|
92
|
+
mergeTranslations(translations: Translations): any;
|
|
93
|
+
/**
|
|
94
|
+
* Returns currently selected locale (locale's key).
|
|
95
|
+
*/
|
|
96
|
+
getLocale(): null | string;
|
|
97
|
+
/**
|
|
98
|
+
* Sets current locale.
|
|
99
|
+
*/
|
|
100
|
+
setLocale(locale: string): I18N;
|
|
101
|
+
/**
|
|
102
|
+
* Registers single modifier.
|
|
103
|
+
* @returns {I18N}
|
|
104
|
+
*/
|
|
105
|
+
registerModifier(modifier: Modifier): I18N;
|
|
106
|
+
/**
|
|
107
|
+
* Registers all modifiers in given array.
|
|
108
|
+
* @param modifiers
|
|
109
|
+
* @returns {I18N}
|
|
110
|
+
*/
|
|
111
|
+
registerModifiers(modifiers: Array<Modifier>): I18N;
|
|
112
|
+
/**
|
|
113
|
+
* Unregisters given modifier.
|
|
114
|
+
* @param name
|
|
115
|
+
* @returns {I18N}
|
|
116
|
+
*/
|
|
117
|
+
unregisterModifier(name: string): I18N;
|
|
118
|
+
/**
|
|
119
|
+
* Registers single processor.
|
|
120
|
+
* @returns {I18N}
|
|
121
|
+
*/
|
|
122
|
+
registerProcessor(processor: Processor): I18N;
|
|
123
|
+
/**
|
|
124
|
+
* Registers all processors in given array.
|
|
125
|
+
* @param processors
|
|
126
|
+
* @returns {I18N}
|
|
127
|
+
*/
|
|
128
|
+
registerProcessors(processors: Array<Processor>): I18N;
|
|
129
|
+
/**
|
|
130
|
+
* Unregisters given processor.
|
|
131
|
+
* @param name
|
|
132
|
+
* @returns {I18N}
|
|
133
|
+
*/
|
|
134
|
+
unregisterProcessor(name: string): I18N;
|
|
135
|
+
/**
|
|
136
|
+
* Returns default formats
|
|
137
|
+
* @returns {{date: string, time: string, datetime: string, number: string}}
|
|
138
|
+
*/
|
|
139
|
+
getDefaultFormats(): Formats;
|
|
140
|
+
/**
|
|
141
|
+
* Returns current format to be used when outputting dates.
|
|
142
|
+
*/
|
|
143
|
+
getDateFormat(): string;
|
|
144
|
+
/**
|
|
145
|
+
* Returns current format to be used when outputting time.
|
|
146
|
+
*/
|
|
147
|
+
getTimeFormat(): string;
|
|
148
|
+
/**
|
|
149
|
+
* Returns current format to be used when outputting date/time.
|
|
150
|
+
*/
|
|
151
|
+
getDateTimeFormat(): string;
|
|
152
|
+
/**
|
|
153
|
+
* Returns current format to be used when outputting prices.
|
|
154
|
+
*/
|
|
155
|
+
getPriceFormat(): PriceFormat;
|
|
156
|
+
/**
|
|
157
|
+
* Returns current format to be used when outputting numbers.
|
|
158
|
+
*/
|
|
159
|
+
getNumberFormat(): NumberFormat;
|
|
160
|
+
}
|
package/I18n.js
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _assign2 = _interopRequireDefault(require("lodash/assign"));
|
|
13
|
+
|
|
14
|
+
var _get2 = _interopRequireDefault(require("lodash/get"));
|
|
15
|
+
|
|
16
|
+
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
17
|
+
|
|
18
|
+
var fecha = _interopRequireWildcard(require("fecha"));
|
|
19
|
+
|
|
20
|
+
var _accounting = _interopRequireDefault(require("accounting"));
|
|
21
|
+
|
|
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
|
+
|
|
24
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Main class used for all I18n needs.
|
|
28
|
+
*/
|
|
29
|
+
class I18N {
|
|
30
|
+
constructor() {
|
|
31
|
+
(0, _defineProperty2.default)(this, "locale", void 0);
|
|
32
|
+
(0, _defineProperty2.default)(this, "defaultFormats", void 0);
|
|
33
|
+
(0, _defineProperty2.default)(this, "translations", void 0);
|
|
34
|
+
(0, _defineProperty2.default)(this, "modifiers", void 0);
|
|
35
|
+
(0, _defineProperty2.default)(this, "processors", void 0);
|
|
36
|
+
this.locale = null;
|
|
37
|
+
/**
|
|
38
|
+
* If we fail to fetch formats for currently selected locale, these default formats will be used.
|
|
39
|
+
* @type {{date: string, time: string, datetime: string, number: string}}
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
this.defaultFormats = this.getDefaultFormats();
|
|
43
|
+
/**
|
|
44
|
+
* All currently-loaded translations, for easier (synchronous) access.
|
|
45
|
+
* @type {{}}
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
this.translations = {};
|
|
49
|
+
/**
|
|
50
|
+
* All registered modifiers.
|
|
51
|
+
* @type {{}}
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
this.modifiers = {};
|
|
55
|
+
/**
|
|
56
|
+
* All registered processors.
|
|
57
|
+
* Default built-in processors are registered immediately below.
|
|
58
|
+
* @type {{}}
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
this.processors = {};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
translate(base, namespace) {
|
|
65
|
+
// Returns full translation for given base text in given namespace (optional).
|
|
66
|
+
// If translation isn't found, base text will be returned.
|
|
67
|
+
// We create a key out of given namespace and base text.
|
|
68
|
+
if (!namespace) {
|
|
69
|
+
throw Error("I18N text namespace not defined.");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
base = (0, _get2.default)(base, "raw.0", base);
|
|
73
|
+
let translation = this.getTranslation(namespace + "." + (0, _shortHash.default)(base));
|
|
74
|
+
|
|
75
|
+
if (!translation) {
|
|
76
|
+
translation = base;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hasVariables = base.includes("{") && base.includes("}");
|
|
80
|
+
|
|
81
|
+
if (hasVariables) {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
return values => {
|
|
84
|
+
const data = {
|
|
85
|
+
translation,
|
|
86
|
+
base,
|
|
87
|
+
namespace,
|
|
88
|
+
values,
|
|
89
|
+
i18n: this
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
for (const key in this.processors) {
|
|
93
|
+
if (this.processors[key].canExecute(data)) {
|
|
94
|
+
return this.processors[key].execute(data);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const data = {
|
|
103
|
+
translation,
|
|
104
|
+
base,
|
|
105
|
+
namespace,
|
|
106
|
+
values: {},
|
|
107
|
+
i18n: this
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
for (const key in this.processors) {
|
|
111
|
+
if (this.processors[key].canExecute(data)) {
|
|
112
|
+
return this.processors[key].execute(data);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
namespace(namespace) {
|
|
120
|
+
return base => {
|
|
121
|
+
return this.translate(base, namespace);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
ns(namespace) {
|
|
126
|
+
return this.namespace(namespace);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Formats and outputs date.
|
|
130
|
+
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
131
|
+
* @param value
|
|
132
|
+
* @param outputFormat
|
|
133
|
+
* @param inputFormat
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
date(value, outputFormat, inputFormat) {
|
|
138
|
+
if (!outputFormat) {
|
|
139
|
+
outputFormat = this.getDateFormat();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let parsedValue;
|
|
143
|
+
|
|
144
|
+
if (typeof value === "string") {
|
|
145
|
+
parsedValue = fecha.parse(value, inputFormat);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return fecha.format(parsedValue, outputFormat);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Formats and outputs time.
|
|
152
|
+
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
153
|
+
* @param value
|
|
154
|
+
* @param outputFormat
|
|
155
|
+
* @param inputFormat
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
time(value, outputFormat = null, inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ") {
|
|
160
|
+
if (!outputFormat) {
|
|
161
|
+
outputFormat = this.getTimeFormat();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let parsedValue;
|
|
165
|
+
|
|
166
|
+
if (typeof value === "string") {
|
|
167
|
+
parsedValue = fecha.parse(value, inputFormat);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return fecha.format(parsedValue, outputFormat);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Formats and outputs date/time.
|
|
174
|
+
* It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
|
|
175
|
+
* @param value
|
|
176
|
+
* @param outputFormat
|
|
177
|
+
* @param inputFormat
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
dateTime(value, outputFormat = null, inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ") {
|
|
182
|
+
if (!outputFormat) {
|
|
183
|
+
outputFormat = this.getDateTimeFormat();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let parsedValue;
|
|
187
|
+
|
|
188
|
+
if (typeof value === "string") {
|
|
189
|
+
parsedValue = fecha.parse(value, inputFormat);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return fecha.format(parsedValue, outputFormat);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Outputs formatted number as amount of price.
|
|
196
|
+
* @param value
|
|
197
|
+
* @param outputFormat
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
price(value, outputFormat) {
|
|
202
|
+
if (!outputFormat) {
|
|
203
|
+
outputFormat = this.getPriceFormat();
|
|
204
|
+
} else {
|
|
205
|
+
outputFormat = (0, _assign2.default)({}, this.defaultFormats.price, outputFormat);
|
|
206
|
+
} // Convert placeholders to accounting's placeholders.
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
let format = outputFormat.format;
|
|
210
|
+
format = format.replace("{symbol}", "%s");
|
|
211
|
+
format = format.replace("{amount}", "%v");
|
|
212
|
+
return _accounting.default.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Outputs formatted number.
|
|
216
|
+
* @param value
|
|
217
|
+
* @param outputFormat
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
number(value, outputFormat) {
|
|
222
|
+
if (!outputFormat) {
|
|
223
|
+
outputFormat = this.getNumberFormat();
|
|
224
|
+
} else {
|
|
225
|
+
outputFormat = (0, _assign2.default)({}, this.defaultFormats.number, outputFormat);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return _accounting.default.formatNumber(value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Returns translation for given text key.
|
|
232
|
+
* @param key
|
|
233
|
+
* @returns {*|string}
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
getTranslation(key) {
|
|
238
|
+
return this.translations[key];
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Returns all translations for current locale.
|
|
242
|
+
* @returns {*|{}}
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
getTranslations() {
|
|
247
|
+
return this.translations;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Returns true if given key has a translation for currently set locale.
|
|
251
|
+
* @param key
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
hasTranslation(key) {
|
|
256
|
+
return key in this.translations;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Sets translation for given text key.
|
|
260
|
+
* @param key
|
|
261
|
+
* @param translation
|
|
262
|
+
* @returns {I18N}
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
setTranslation(key, translation) {
|
|
267
|
+
this.translations[key] = translation;
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Sets translations that will be used.
|
|
272
|
+
* @returns {*|{}}
|
|
273
|
+
*/
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
setTranslations(translations) {
|
|
277
|
+
this.translations = translations;
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Clears all translations.
|
|
282
|
+
* @returns {*|{}}
|
|
283
|
+
*/
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
clearTranslations() {
|
|
287
|
+
this.setTranslations({});
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Merges given translations object with already existing.
|
|
292
|
+
* @returns {*|{}}
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
mergeTranslations(translations) {
|
|
297
|
+
return (0, _assign2.default)(this.translations, translations);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Returns currently selected locale (locale's key).
|
|
301
|
+
*/
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
getLocale() {
|
|
305
|
+
return this.locale;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Sets current locale.
|
|
309
|
+
*/
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
setLocale(locale) {
|
|
313
|
+
this.locale = locale;
|
|
314
|
+
return this;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Registers single modifier.
|
|
318
|
+
* @returns {I18N}
|
|
319
|
+
*/
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
registerModifier(modifier) {
|
|
323
|
+
this.modifiers[modifier.name] = modifier;
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Registers all modifiers in given array.
|
|
328
|
+
* @param modifiers
|
|
329
|
+
* @returns {I18N}
|
|
330
|
+
*/
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
registerModifiers(modifiers) {
|
|
334
|
+
modifiers.forEach(modifier => this.registerModifier(modifier));
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Unregisters given modifier.
|
|
339
|
+
* @param name
|
|
340
|
+
* @returns {I18N}
|
|
341
|
+
*/
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
unregisterModifier(name) {
|
|
345
|
+
delete this.modifiers[name];
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Registers single processor.
|
|
350
|
+
* @returns {I18N}
|
|
351
|
+
*/
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
registerProcessor(processor) {
|
|
355
|
+
this.processors[processor.name] = processor;
|
|
356
|
+
return this;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Registers all processors in given array.
|
|
360
|
+
* @param processors
|
|
361
|
+
* @returns {I18N}
|
|
362
|
+
*/
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
registerProcessors(processors) {
|
|
366
|
+
processors.forEach(processor => this.registerProcessor(processor));
|
|
367
|
+
return this;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Unregisters given processor.
|
|
371
|
+
* @param name
|
|
372
|
+
* @returns {I18N}
|
|
373
|
+
*/
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
unregisterProcessor(name) {
|
|
377
|
+
delete this.processors[name];
|
|
378
|
+
return this;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Returns default formats
|
|
382
|
+
* @returns {{date: string, time: string, datetime: string, number: string}}
|
|
383
|
+
*/
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
getDefaultFormats() {
|
|
387
|
+
return {
|
|
388
|
+
date: "DD/MM/YYYY",
|
|
389
|
+
time: "HH:mm",
|
|
390
|
+
datetime: "DD/MM/YYYY HH:mm",
|
|
391
|
+
price: {
|
|
392
|
+
symbol: "",
|
|
393
|
+
format: "{symbol}{amount}",
|
|
394
|
+
decimal: ".",
|
|
395
|
+
thousand: ",",
|
|
396
|
+
precision: 2
|
|
397
|
+
},
|
|
398
|
+
number: {
|
|
399
|
+
decimal: ".",
|
|
400
|
+
thousand: ",",
|
|
401
|
+
precision: 2
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Returns current format to be used when outputting dates.
|
|
407
|
+
*/
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
getDateFormat() {
|
|
411
|
+
return (0, _get2.default)(this.locale, "formats.date", this.defaultFormats.date);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Returns current format to be used when outputting time.
|
|
415
|
+
*/
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
getTimeFormat() {
|
|
419
|
+
return (0, _get2.default)(this.locale, "formats.time", this.defaultFormats.time);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Returns current format to be used when outputting date/time.
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
getDateTimeFormat() {
|
|
427
|
+
return (0, _get2.default)(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Returns current format to be used when outputting prices.
|
|
431
|
+
*/
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
getPriceFormat() {
|
|
435
|
+
return (0, _assign2.default)({}, this.defaultFormats.price, (0, _get2.default)(this.locale, "formats.price", {}));
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Returns current format to be used when outputting numbers.
|
|
439
|
+
*/
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
getNumberFormat() {
|
|
443
|
+
return (0, _assign2.default)({}, this.defaultFormats.number, (0, _get2.default)(this.locale, "formats.number", {}));
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
exports.default = I18N;
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @webiny/i18n
|
|
2
|
+
[](https://www.npmjs.com/package/@webiny/i18n)
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/i18n)
|
|
4
|
+
[](https://github.com/prettier/prettier)
|
|
5
|
+
[](http://makeapullrequest.com)
|
|
6
|
+
|
|
7
|
+
A simple I18N library, for frontend and backend apps.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
```
|
|
11
|
+
npm install --save @webiny/i18n
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or if you prefer yarn:
|
|
15
|
+
```
|
|
16
|
+
yarn add @webiny/i18n
|
|
17
|
+
```
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Searches for all declared namespaces.
|
|
14
|
+
* Result contains an object with identifiers as keys, and namespaces they represent as values, for example:
|
|
15
|
+
* {ns1: 'Webiny.Ns1', ns2: 'Webiny.Ns2', i18n: 'NewNamespace', t: 'Some.Other.Namespace'}
|
|
16
|
+
* @param source
|
|
17
|
+
*/
|
|
18
|
+
const getNamespaces = source => {
|
|
19
|
+
const regex = /([a-zA-Z0-9]+)[ ]+=[ ]+i18n.namespace\(['"`]([a-zA-Z0-9.]+)['"`]\)/g;
|
|
20
|
+
let m;
|
|
21
|
+
const results = {};
|
|
22
|
+
|
|
23
|
+
while ((m = regex.exec(source)) !== null) {
|
|
24
|
+
if (m.index === regex.lastIndex) {
|
|
25
|
+
regex.lastIndex++;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
results[m[1]] = m[2];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return results;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var _default = source => {
|
|
35
|
+
const results = {};
|
|
36
|
+
const allDeclaredNamespaces = getNamespaces(source);
|
|
37
|
+
|
|
38
|
+
for (const variable in allDeclaredNamespaces) {
|
|
39
|
+
const regex = new RegExp(variable + "`(.*?)`", "g");
|
|
40
|
+
let m;
|
|
41
|
+
|
|
42
|
+
while ((m = regex.exec(source)) !== null) {
|
|
43
|
+
if (m.index === regex.lastIndex) {
|
|
44
|
+
regex.lastIndex++;
|
|
45
|
+
} // This is the key - namespace + hash of matched label.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
const matchedText = m[1];
|
|
49
|
+
const key = allDeclaredNamespaces[variable] + "." + (0, _shortHash.default)(matchedText);
|
|
50
|
+
results[key] = matchedText;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return results;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
exports.default = _default;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _extract = _interopRequireDefault(require("./extract"));
|
|
13
|
+
|
|
14
|
+
var _glob = _interopRequireDefault(require("glob"));
|
|
15
|
+
|
|
16
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
17
|
+
|
|
18
|
+
class Extractor {
|
|
19
|
+
constructor() {
|
|
20
|
+
(0, _defineProperty2.default)(this, "glob", void 0);
|
|
21
|
+
(0, _defineProperty2.default)(this, "content", void 0);
|
|
22
|
+
(0, _defineProperty2.default)(this, "listOnly", void 0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setGlob(glob) {
|
|
26
|
+
this.glob = glob;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setContent(content) {
|
|
31
|
+
this.content = content;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
execute() {
|
|
36
|
+
const results = {};
|
|
37
|
+
|
|
38
|
+
if (this.glob) {
|
|
39
|
+
const paths = _glob.default.sync(this.glob);
|
|
40
|
+
|
|
41
|
+
paths.forEach(path => {
|
|
42
|
+
const contents = _fs.default.readFileSync(path, "utf8");
|
|
43
|
+
|
|
44
|
+
const parsed = (0, _extract.default)(contents);
|
|
45
|
+
|
|
46
|
+
for (const key in parsed) {
|
|
47
|
+
results[key] = parsed[key];
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return results;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setListOnly(flag = true) {
|
|
56
|
+
this.listOnly = flag;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var _default = Extractor;
|
|
63
|
+
exports.default = _default;
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "I18n", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _I18n.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
exports.defaultModifiers = exports.default = void 0;
|
|
15
|
+
Object.defineProperty(exports, "defaultProcessor", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () {
|
|
18
|
+
return _default2.default;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
var _I18n = _interopRequireDefault(require("./I18n"));
|
|
23
|
+
|
|
24
|
+
var _default2 = _interopRequireDefault(require("./processors/default"));
|
|
25
|
+
|
|
26
|
+
var _modifiers = _interopRequireDefault(require("./modifiers"));
|
|
27
|
+
|
|
28
|
+
const i18n = new _I18n.default();
|
|
29
|
+
const defaultModifiers = (0, _modifiers.default)({
|
|
30
|
+
i18n
|
|
31
|
+
});
|
|
32
|
+
exports.defaultModifiers = defaultModifiers;
|
|
33
|
+
var _default = i18n;
|
|
34
|
+
exports.default = _default;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _default = {
|
|
8
|
+
name: "count",
|
|
9
|
+
|
|
10
|
+
execute(value, parameters) {
|
|
11
|
+
// Numbers can be single number or ranges.
|
|
12
|
+
for (let i = 0; i < parameters.length; i = i + 2) {
|
|
13
|
+
const current = parameters[i];
|
|
14
|
+
|
|
15
|
+
if (current === "default") {
|
|
16
|
+
return value + " " + parameters[i + 1];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const numbers = current.split("-"); // If we are dealing with a numbers range, then let's check if we are in it.
|
|
20
|
+
|
|
21
|
+
if (numbers.length === 2) {
|
|
22
|
+
if (value >= numbers[0] && value <= numbers[1]) {
|
|
23
|
+
return value + " " + parameters[i + 1];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (String(value) === numbers[0]) {
|
|
30
|
+
return value + " " + parameters[i + 1];
|
|
31
|
+
}
|
|
32
|
+
} // If we didn't match any condition, let's just remove the received value.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
};
|
|
39
|
+
exports.default = _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
i18n
|
|
10
|
+
}) => ({
|
|
11
|
+
name: "date",
|
|
12
|
+
|
|
13
|
+
execute(value) {
|
|
14
|
+
return i18n.date(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.default = _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
i18n
|
|
10
|
+
}) => ({
|
|
11
|
+
name: "dateTime",
|
|
12
|
+
|
|
13
|
+
execute(value) {
|
|
14
|
+
return i18n.dateTime(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _default = {
|
|
8
|
+
name: "gender",
|
|
9
|
+
|
|
10
|
+
execute(value, parameters) {
|
|
11
|
+
return value === "male" ? parameters[0] : parameters[1];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
};
|
|
15
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _default = {
|
|
8
|
+
name: "if",
|
|
9
|
+
|
|
10
|
+
execute(value, parameters) {
|
|
11
|
+
return value === parameters[0] ? parameters[1] : parameters[2] || "";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
};
|
|
15
|
+
exports.default = _default;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _countModifier = _interopRequireDefault(require("./countModifier"));
|
|
11
|
+
|
|
12
|
+
var _genderModifier = _interopRequireDefault(require("./genderModifier"));
|
|
13
|
+
|
|
14
|
+
var _ifModifier = _interopRequireDefault(require("./ifModifier"));
|
|
15
|
+
|
|
16
|
+
var _pluralModifier = _interopRequireDefault(require("./pluralModifier"));
|
|
17
|
+
|
|
18
|
+
var _dateModifier = _interopRequireDefault(require("./dateModifier"));
|
|
19
|
+
|
|
20
|
+
var _dateTimeModifier = _interopRequireDefault(require("./dateTimeModifier"));
|
|
21
|
+
|
|
22
|
+
var _timeModifier = _interopRequireDefault(require("./timeModifier"));
|
|
23
|
+
|
|
24
|
+
var _numberModifier = _interopRequireDefault(require("./numberModifier"));
|
|
25
|
+
|
|
26
|
+
var _priceModifier = _interopRequireDefault(require("./priceModifier"));
|
|
27
|
+
|
|
28
|
+
// Built-in modifiers
|
|
29
|
+
var _default = options => [_countModifier.default, _genderModifier.default, _ifModifier.default, _pluralModifier.default, (0, _dateModifier.default)(options), (0, _dateTimeModifier.default)(options), (0, _timeModifier.default)(options), (0, _numberModifier.default)(options), (0, _priceModifier.default)(options)];
|
|
30
|
+
|
|
31
|
+
exports.default = _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
i18n
|
|
10
|
+
}) => ({
|
|
11
|
+
name: "number",
|
|
12
|
+
|
|
13
|
+
execute(value) {
|
|
14
|
+
return i18n.number(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.default = _default;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _default = {
|
|
8
|
+
name: "plural",
|
|
9
|
+
|
|
10
|
+
execute(value, parameters) {
|
|
11
|
+
// Numbers can be single number or ranges.
|
|
12
|
+
for (let i = 0; i < parameters.length; i = i + 2) {
|
|
13
|
+
const current = parameters[i];
|
|
14
|
+
|
|
15
|
+
if (current === "default") {
|
|
16
|
+
return parameters[i + 1];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const numbers = current.split("-"); // If we are dealing with a numbers range, then let's check if we are in it.
|
|
20
|
+
|
|
21
|
+
if (numbers.length === 2) {
|
|
22
|
+
if (value >= numbers[0] && value <= numbers[1]) {
|
|
23
|
+
return parameters[i + 1];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (String(value) === numbers[0]) {
|
|
30
|
+
return parameters[i + 1];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return "";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
};
|
|
38
|
+
exports.default = _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
i18n
|
|
10
|
+
}) => ({
|
|
11
|
+
name: "price",
|
|
12
|
+
|
|
13
|
+
execute(value) {
|
|
14
|
+
return i18n.price(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.default = _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
i18n
|
|
10
|
+
}) => ({
|
|
11
|
+
name: "time",
|
|
12
|
+
|
|
13
|
+
execute(value) {
|
|
14
|
+
return i18n.time(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.default = _default;
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/i18n",
|
|
3
|
+
"version": "0.0.0-mt-1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/webiny/webiny-js.git"
|
|
8
|
+
},
|
|
9
|
+
"contributors": [
|
|
10
|
+
"Pavel Denisjuk <pavel@webiny.com>",
|
|
11
|
+
"Sven Al Hamad <sven@webiny.com>",
|
|
12
|
+
"Adrian Smijulj <adrian@webiny.com>"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@babel/runtime": "7.15.4",
|
|
17
|
+
"accounting": "0.4.1",
|
|
18
|
+
"fecha": "2.3.3",
|
|
19
|
+
"lodash": "4.17.21",
|
|
20
|
+
"short-hash": "1.0.0",
|
|
21
|
+
"yargs": "12.0.5"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "16.14.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@babel/cli": "^7.5.5",
|
|
28
|
+
"@babel/core": "^7.5.5",
|
|
29
|
+
"@babel/preset-env": "^7.5.5",
|
|
30
|
+
"@babel/preset-typescript": "^7.8.3",
|
|
31
|
+
"@babel/register": "^7.5.5",
|
|
32
|
+
"@webiny/cli": "^0.0.0-mt-1",
|
|
33
|
+
"@webiny/project-utils": "^0.0.0-mt-1",
|
|
34
|
+
"babel-plugin-lodash": "^3.3.4",
|
|
35
|
+
"glob": "^7.1.2",
|
|
36
|
+
"rimraf": "^3.0.2",
|
|
37
|
+
"typescript": "^4.1.3"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"directory": "dist"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "yarn webiny run build",
|
|
45
|
+
"watch": "yarn webiny run watch"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
|
|
48
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _has2 = _interopRequireDefault(require("lodash/has"));
|
|
11
|
+
|
|
12
|
+
var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
|
13
|
+
|
|
14
|
+
var _startsWith2 = _interopRequireDefault(require("lodash/startsWith"));
|
|
15
|
+
|
|
16
|
+
const processTextPart = (part, values, modifiers) => {
|
|
17
|
+
if (!(0, _startsWith2.default)(part, "{")) {
|
|
18
|
+
return part;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const parts = (0, _trim2.default)(part, "{}").split("|");
|
|
22
|
+
const [variable, modifier] = parts;
|
|
23
|
+
|
|
24
|
+
if (!(0, _has2.default)(values, variable)) {
|
|
25
|
+
return `{${variable}}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const output = {
|
|
29
|
+
value: values[variable]
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
if (modifier) {
|
|
33
|
+
const parameters = modifier.split(":");
|
|
34
|
+
const name = parameters.shift();
|
|
35
|
+
|
|
36
|
+
if (modifiers[name]) {
|
|
37
|
+
const modifier = modifiers[name];
|
|
38
|
+
output.value = modifier.execute(output.value, parameters);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return output.value;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const processor = {
|
|
46
|
+
name: "default",
|
|
47
|
+
|
|
48
|
+
canExecute(data) {
|
|
49
|
+
for (const key in data.values) {
|
|
50
|
+
const value = data.values[key];
|
|
51
|
+
|
|
52
|
+
if (typeof value === "string" || typeof value === "number" || value === null || value instanceof Date) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
execute(data) {
|
|
63
|
+
const parts = data.translation.split(/({.*?})/);
|
|
64
|
+
return parts.reduce((carry, part) => carry + processTextPart(part, data.values, data.i18n.modifiers), "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
};
|
|
68
|
+
var _default = processor;
|
|
69
|
+
exports.default = _default;
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import I18N from "./I18n";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
export declare type I18NData = {
|
|
4
|
+
translation: string;
|
|
5
|
+
base: string;
|
|
6
|
+
namespace: string;
|
|
7
|
+
values: {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
i18n: I18N;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @name Modifier
|
|
14
|
+
* @description I18N Modifier - used for modifying text dynamically.
|
|
15
|
+
*/
|
|
16
|
+
export declare type Modifier = {
|
|
17
|
+
name: string;
|
|
18
|
+
execute: (...args: any[]) => string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @name Processor
|
|
22
|
+
* @description I18N Processor - used for outputting text.
|
|
23
|
+
*/
|
|
24
|
+
export declare type Processor = {
|
|
25
|
+
name: string;
|
|
26
|
+
canExecute?: (data: I18NData) => boolean;
|
|
27
|
+
execute: (data: I18NData) => string | ReactElement;
|
|
28
|
+
};
|
|
29
|
+
export declare type NumberFormat = {
|
|
30
|
+
decimal: string;
|
|
31
|
+
thousand: string;
|
|
32
|
+
precision: number;
|
|
33
|
+
};
|
|
34
|
+
export declare type PriceFormat = {
|
|
35
|
+
symbol: string;
|
|
36
|
+
format: string;
|
|
37
|
+
decimal: string;
|
|
38
|
+
thousand: string;
|
|
39
|
+
precision: number;
|
|
40
|
+
};
|
|
41
|
+
export declare type Formats = {
|
|
42
|
+
date: string;
|
|
43
|
+
time: string;
|
|
44
|
+
datetime: string;
|
|
45
|
+
price: PriceFormat;
|
|
46
|
+
number: NumberFormat;
|
|
47
|
+
};
|
|
48
|
+
export declare type Translator = (base: any) => any;
|
|
49
|
+
export declare type Translations = {
|
|
50
|
+
[key: string]: string;
|
|
51
|
+
};
|