@webiny/i18n 0.0.0-mt-2 → 0.0.0-unstable.085ff6572f
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 +7 -40
- package/I18n.js +86 -152
- package/I18n.js.map +1 -0
- package/extractor/extract.d.ts +1 -1
- package/extractor/extract.js +7 -13
- package/extractor/extract.js.map +1 -0
- package/extractor/index.d.ts +6 -3
- package/extractor/index.js +15 -31
- package/extractor/index.js.map +1 -0
- package/index.js +1 -6
- package/index.js.map +1 -0
- package/modifiers/countModifier.d.ts +1 -1
- package/modifiers/countModifier.js +24 -27
- package/modifiers/countModifier.js.map +1 -0
- package/modifiers/dateModifier.d.ts +2 -4
- package/modifiers/dateModifier.js +0 -4
- package/modifiers/dateModifier.js.map +1 -0
- package/modifiers/dateTimeModifier.d.ts +2 -4
- package/modifiers/dateTimeModifier.js +0 -4
- package/modifiers/dateTimeModifier.js.map +1 -0
- package/modifiers/genderModifier.d.ts +1 -1
- package/modifiers/genderModifier.js +7 -7
- package/modifiers/genderModifier.js.map +1 -0
- package/modifiers/ifModifier.d.ts +1 -1
- package/modifiers/ifModifier.js +7 -7
- package/modifiers/ifModifier.js.map +1 -0
- package/modifiers/index.d.ts +2 -1
- package/modifiers/index.js +2 -14
- package/modifiers/index.js.map +1 -0
- package/modifiers/numberModifier.d.ts +2 -4
- package/modifiers/numberModifier.js +0 -4
- package/modifiers/numberModifier.js.map +1 -0
- package/modifiers/pluralModifier.d.ts +1 -1
- package/modifiers/pluralModifier.js +22 -26
- package/modifiers/pluralModifier.js.map +1 -0
- package/modifiers/priceModifier.d.ts +2 -4
- package/modifiers/priceModifier.js +0 -4
- package/modifiers/priceModifier.js.map +1 -0
- package/modifiers/timeModifier.d.ts +2 -4
- package/modifiers/timeModifier.js +0 -4
- package/modifiers/timeModifier.js.map +1 -0
- package/package.json +19 -16
- package/processors/default.js +8 -25
- package/processors/default.js.map +1 -0
- package/types.d.ts +27 -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,83 +14,60 @@ 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
|
-
mergeTranslations(translations: Translations):
|
|
70
|
+
mergeTranslations(translations: Translations): Translations;
|
|
93
71
|
/**
|
|
94
72
|
* Returns currently selected locale (locale's key).
|
|
95
73
|
*/
|
|
@@ -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
|
@@ -1,104 +1,91 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.default = void 0;
|
|
9
|
-
|
|
10
9
|
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
10
|
var _accounting = _interopRequireDefault(require("accounting"));
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
var fecha = _interopRequireWildcard(require("fecha"));
|
|
12
|
+
var _shortHash = _interopRequireDefault(require("short-hash"));
|
|
13
|
+
var _assign = _interopRequireDefault(require("lodash/assign"));
|
|
14
|
+
var _get = _interopRequireDefault(require("lodash/get"));
|
|
15
|
+
/**
|
|
16
|
+
* Package short-hash has no types.
|
|
17
|
+
*/
|
|
18
|
+
// @ts-ignore
|
|
25
19
|
|
|
26
20
|
/**
|
|
27
21
|
* Main class used for all I18n needs.
|
|
28
22
|
*/
|
|
29
23
|
class I18N {
|
|
30
24
|
constructor() {
|
|
31
|
-
(0, _defineProperty2.default)(this, "locale",
|
|
25
|
+
(0, _defineProperty2.default)(this, "locale", null);
|
|
32
26
|
(0, _defineProperty2.default)(this, "defaultFormats", void 0);
|
|
33
27
|
(0, _defineProperty2.default)(this, "translations", void 0);
|
|
34
28
|
(0, _defineProperty2.default)(this, "modifiers", void 0);
|
|
35
29
|
(0, _defineProperty2.default)(this, "processors", void 0);
|
|
36
|
-
this.locale = null;
|
|
37
30
|
/**
|
|
38
31
|
* If we fail to fetch formats for currently selected locale, these default formats will be used.
|
|
39
32
|
* @type {{date: string, time: string, datetime: string, number: string}}
|
|
40
33
|
*/
|
|
41
|
-
|
|
42
34
|
this.defaultFormats = this.getDefaultFormats();
|
|
35
|
+
|
|
43
36
|
/**
|
|
44
37
|
* All currently-loaded translations, for easier (synchronous) access.
|
|
45
38
|
* @type {{}}
|
|
46
39
|
*/
|
|
47
|
-
|
|
48
40
|
this.translations = {};
|
|
41
|
+
|
|
49
42
|
/**
|
|
50
43
|
* All registered modifiers.
|
|
51
44
|
* @type {{}}
|
|
52
45
|
*/
|
|
53
|
-
|
|
54
46
|
this.modifiers = {};
|
|
47
|
+
|
|
55
48
|
/**
|
|
56
49
|
* All registered processors.
|
|
57
50
|
* Default built-in processors are registered immediately below.
|
|
58
51
|
* @type {{}}
|
|
59
52
|
*/
|
|
60
|
-
|
|
61
53
|
this.processors = {};
|
|
62
54
|
}
|
|
63
|
-
|
|
64
55
|
translate(base, namespace) {
|
|
65
56
|
// Returns full translation for given base text in given namespace (optional).
|
|
66
57
|
// If translation isn't found, base text will be returned.
|
|
67
58
|
// We create a key out of given namespace and base text.
|
|
59
|
+
|
|
68
60
|
if (!namespace) {
|
|
69
61
|
throw Error("I18N text namespace not defined.");
|
|
70
62
|
}
|
|
71
|
-
|
|
72
|
-
base = (0, _get2.default)(base, "raw.0", base);
|
|
63
|
+
base = (0, _get.default)(base, "raw.0", base);
|
|
73
64
|
let translation = this.getTranslation(namespace + "." + (0, _shortHash.default)(base));
|
|
74
|
-
|
|
75
65
|
if (!translation) {
|
|
76
66
|
translation = base;
|
|
77
67
|
}
|
|
78
|
-
|
|
79
68
|
const hasVariables = base.includes("{") && base.includes("}");
|
|
80
|
-
|
|
81
69
|
if (hasVariables) {
|
|
82
|
-
// @
|
|
83
|
-
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
71
|
+
const $this = this;
|
|
72
|
+
return function i18n(values) {
|
|
84
73
|
const data = {
|
|
85
|
-
translation,
|
|
74
|
+
translation: translation,
|
|
86
75
|
base,
|
|
87
76
|
namespace,
|
|
88
77
|
values,
|
|
89
|
-
i18n: this
|
|
78
|
+
i18n: $this
|
|
90
79
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
return
|
|
80
|
+
for (const key in $this.processors) {
|
|
81
|
+
const processor = $this.processors[key];
|
|
82
|
+
if (processor.canExecute(data)) {
|
|
83
|
+
return processor.execute(data);
|
|
95
84
|
}
|
|
96
85
|
}
|
|
97
|
-
|
|
98
86
|
return null;
|
|
99
87
|
};
|
|
100
88
|
}
|
|
101
|
-
|
|
102
89
|
const data = {
|
|
103
90
|
translation,
|
|
104
91
|
base,
|
|
@@ -106,283 +93,237 @@ class I18N {
|
|
|
106
93
|
values: {},
|
|
107
94
|
i18n: this
|
|
108
95
|
};
|
|
109
|
-
|
|
110
96
|
for (const key in this.processors) {
|
|
111
97
|
if (this.processors[key].canExecute(data)) {
|
|
112
98
|
return this.processors[key].execute(data);
|
|
113
99
|
}
|
|
114
100
|
}
|
|
115
|
-
|
|
116
101
|
return null;
|
|
117
102
|
}
|
|
118
|
-
|
|
119
103
|
namespace(namespace) {
|
|
120
104
|
return base => {
|
|
121
105
|
return this.translate(base, namespace);
|
|
122
106
|
};
|
|
123
107
|
}
|
|
124
|
-
|
|
125
108
|
ns(namespace) {
|
|
126
109
|
return this.namespace(namespace);
|
|
127
110
|
}
|
|
111
|
+
|
|
128
112
|
/**
|
|
129
113
|
* Formats and outputs date.
|
|
130
114
|
* 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
115
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
116
|
date(value, outputFormat, inputFormat) {
|
|
138
117
|
if (!outputFormat) {
|
|
139
118
|
outputFormat = this.getDateFormat();
|
|
140
119
|
}
|
|
141
|
-
|
|
120
|
+
if (!inputFormat) {
|
|
121
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
122
|
+
}
|
|
142
123
|
let parsedValue;
|
|
143
|
-
|
|
144
124
|
if (typeof value === "string") {
|
|
145
125
|
parsedValue = fecha.parse(value, inputFormat);
|
|
126
|
+
} else {
|
|
127
|
+
parsedValue = value;
|
|
146
128
|
}
|
|
147
|
-
|
|
148
129
|
return fecha.format(parsedValue, outputFormat);
|
|
149
130
|
}
|
|
131
|
+
|
|
150
132
|
/**
|
|
151
133
|
* Formats and outputs time.
|
|
152
134
|
* 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
135
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
time(value, outputFormat = null, inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ") {
|
|
136
|
+
time(value, outputFormat, inputFormat) {
|
|
160
137
|
if (!outputFormat) {
|
|
161
138
|
outputFormat = this.getTimeFormat();
|
|
162
139
|
}
|
|
163
|
-
|
|
140
|
+
if (!inputFormat) {
|
|
141
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
142
|
+
}
|
|
164
143
|
let parsedValue;
|
|
165
|
-
|
|
166
144
|
if (typeof value === "string") {
|
|
167
145
|
parsedValue = fecha.parse(value, inputFormat);
|
|
146
|
+
} else {
|
|
147
|
+
parsedValue = value;
|
|
168
148
|
}
|
|
169
|
-
|
|
170
149
|
return fecha.format(parsedValue, outputFormat);
|
|
171
150
|
}
|
|
151
|
+
|
|
172
152
|
/**
|
|
173
153
|
* Formats and outputs date/time.
|
|
174
154
|
* 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
155
|
*/
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
dateTime(value, outputFormat = null, inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ") {
|
|
156
|
+
dateTime(value, outputFormat, inputFormat) {
|
|
182
157
|
if (!outputFormat) {
|
|
183
158
|
outputFormat = this.getDateTimeFormat();
|
|
184
159
|
}
|
|
185
|
-
|
|
160
|
+
if (!inputFormat) {
|
|
161
|
+
inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
162
|
+
}
|
|
186
163
|
let parsedValue;
|
|
187
|
-
|
|
188
164
|
if (typeof value === "string") {
|
|
189
165
|
parsedValue = fecha.parse(value, inputFormat);
|
|
166
|
+
} else {
|
|
167
|
+
parsedValue = value;
|
|
190
168
|
}
|
|
191
|
-
|
|
192
169
|
return fecha.format(parsedValue, outputFormat);
|
|
193
170
|
}
|
|
171
|
+
|
|
194
172
|
/**
|
|
195
173
|
* Outputs formatted number as amount of price.
|
|
196
|
-
* @param value
|
|
197
|
-
* @param outputFormat
|
|
198
174
|
*/
|
|
199
|
-
|
|
200
|
-
|
|
201
175
|
price(value, outputFormat) {
|
|
202
176
|
if (!outputFormat) {
|
|
203
177
|
outputFormat = this.getPriceFormat();
|
|
204
178
|
} else {
|
|
205
|
-
outputFormat = (0,
|
|
206
|
-
}
|
|
207
|
-
|
|
179
|
+
outputFormat = (0, _assign.default)({}, this.defaultFormats.price, outputFormat);
|
|
180
|
+
}
|
|
208
181
|
|
|
182
|
+
// Convert placeholders to accounting's placeholders.
|
|
209
183
|
let format = outputFormat.format;
|
|
210
184
|
format = format.replace("{symbol}", "%s");
|
|
211
185
|
format = format.replace("{amount}", "%v");
|
|
212
186
|
return _accounting.default.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
|
|
213
187
|
}
|
|
188
|
+
|
|
214
189
|
/**
|
|
215
190
|
* Outputs formatted number.
|
|
216
|
-
* @param value
|
|
217
|
-
* @param outputFormat
|
|
218
191
|
*/
|
|
219
|
-
|
|
220
|
-
|
|
221
192
|
number(value, outputFormat) {
|
|
222
193
|
if (!outputFormat) {
|
|
223
194
|
outputFormat = this.getNumberFormat();
|
|
224
195
|
} else {
|
|
225
|
-
outputFormat = (0,
|
|
196
|
+
outputFormat = (0, _assign.default)({}, this.defaultFormats.number, outputFormat);
|
|
226
197
|
}
|
|
227
|
-
|
|
228
|
-
|
|
198
|
+
return _accounting.default.formatNumber(
|
|
199
|
+
/**
|
|
200
|
+
* Cast as number because method transforms it internally.
|
|
201
|
+
*/
|
|
202
|
+
value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
|
|
229
203
|
}
|
|
204
|
+
|
|
230
205
|
/**
|
|
231
206
|
* Returns translation for given text key.
|
|
232
|
-
* @param key
|
|
233
|
-
* @returns {*|string}
|
|
234
207
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
208
|
getTranslation(key) {
|
|
209
|
+
if (!key) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
238
212
|
return this.translations[key];
|
|
239
213
|
}
|
|
214
|
+
|
|
240
215
|
/**
|
|
241
216
|
* Returns all translations for current locale.
|
|
242
|
-
* @returns {*|{}}
|
|
243
217
|
*/
|
|
244
|
-
|
|
245
|
-
|
|
246
218
|
getTranslations() {
|
|
247
219
|
return this.translations;
|
|
248
220
|
}
|
|
221
|
+
|
|
249
222
|
/**
|
|
250
223
|
* Returns true if given key has a translation for currently set locale.
|
|
251
|
-
* @param key
|
|
252
224
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
225
|
hasTranslation(key) {
|
|
256
226
|
return key in this.translations;
|
|
257
227
|
}
|
|
228
|
+
|
|
258
229
|
/**
|
|
259
230
|
* Sets translation for given text key.
|
|
260
|
-
* @param key
|
|
261
|
-
* @param translation
|
|
262
|
-
* @returns {I18N}
|
|
263
231
|
*/
|
|
264
|
-
|
|
265
|
-
|
|
266
232
|
setTranslation(key, translation) {
|
|
267
233
|
this.translations[key] = translation;
|
|
268
234
|
return this;
|
|
269
235
|
}
|
|
236
|
+
|
|
270
237
|
/**
|
|
271
238
|
* Sets translations that will be used.
|
|
272
|
-
* @returns {*|{}}
|
|
273
239
|
*/
|
|
274
|
-
|
|
275
|
-
|
|
276
240
|
setTranslations(translations) {
|
|
277
241
|
this.translations = translations;
|
|
278
242
|
return this;
|
|
279
243
|
}
|
|
244
|
+
|
|
280
245
|
/**
|
|
281
246
|
* Clears all translations.
|
|
282
|
-
* @returns {*|{}}
|
|
283
247
|
*/
|
|
284
|
-
|
|
285
|
-
|
|
286
248
|
clearTranslations() {
|
|
287
249
|
this.setTranslations({});
|
|
288
250
|
return this;
|
|
289
251
|
}
|
|
252
|
+
|
|
290
253
|
/**
|
|
291
254
|
* Merges given translations object with already existing.
|
|
292
|
-
* @returns {*|{}}
|
|
293
255
|
*/
|
|
294
256
|
|
|
295
|
-
|
|
296
257
|
mergeTranslations(translations) {
|
|
297
|
-
return (0,
|
|
258
|
+
return (0, _assign.default)(this.translations, translations);
|
|
298
259
|
}
|
|
260
|
+
|
|
299
261
|
/**
|
|
300
262
|
* Returns currently selected locale (locale's key).
|
|
301
263
|
*/
|
|
302
|
-
|
|
303
|
-
|
|
304
264
|
getLocale() {
|
|
305
265
|
return this.locale;
|
|
306
266
|
}
|
|
267
|
+
|
|
307
268
|
/**
|
|
308
269
|
* Sets current locale.
|
|
309
270
|
*/
|
|
310
|
-
|
|
311
|
-
|
|
312
271
|
setLocale(locale) {
|
|
313
272
|
this.locale = locale;
|
|
314
273
|
return this;
|
|
315
274
|
}
|
|
275
|
+
|
|
316
276
|
/**
|
|
317
277
|
* Registers single modifier.
|
|
318
|
-
* @returns {I18N}
|
|
319
278
|
*/
|
|
320
|
-
|
|
321
|
-
|
|
322
279
|
registerModifier(modifier) {
|
|
323
280
|
this.modifiers[modifier.name] = modifier;
|
|
324
281
|
return this;
|
|
325
282
|
}
|
|
283
|
+
|
|
326
284
|
/**
|
|
327
285
|
* Registers all modifiers in given array.
|
|
328
|
-
* @param modifiers
|
|
329
|
-
* @returns {I18N}
|
|
330
286
|
*/
|
|
331
|
-
|
|
332
|
-
|
|
333
287
|
registerModifiers(modifiers) {
|
|
334
288
|
modifiers.forEach(modifier => this.registerModifier(modifier));
|
|
335
289
|
return this;
|
|
336
290
|
}
|
|
291
|
+
|
|
337
292
|
/**
|
|
338
293
|
* Unregisters given modifier.
|
|
339
|
-
* @param name
|
|
340
|
-
* @returns {I18N}
|
|
341
294
|
*/
|
|
342
|
-
|
|
343
|
-
|
|
344
295
|
unregisterModifier(name) {
|
|
345
296
|
delete this.modifiers[name];
|
|
346
297
|
return this;
|
|
347
298
|
}
|
|
299
|
+
|
|
348
300
|
/**
|
|
349
301
|
* Registers single processor.
|
|
350
|
-
* @returns {I18N}
|
|
351
302
|
*/
|
|
352
|
-
|
|
353
|
-
|
|
354
303
|
registerProcessor(processor) {
|
|
355
304
|
this.processors[processor.name] = processor;
|
|
356
305
|
return this;
|
|
357
306
|
}
|
|
307
|
+
|
|
358
308
|
/**
|
|
359
309
|
* Registers all processors in given array.
|
|
360
|
-
* @param processors
|
|
361
|
-
* @returns {I18N}
|
|
362
310
|
*/
|
|
363
|
-
|
|
364
|
-
|
|
365
311
|
registerProcessors(processors) {
|
|
366
312
|
processors.forEach(processor => this.registerProcessor(processor));
|
|
367
313
|
return this;
|
|
368
314
|
}
|
|
315
|
+
|
|
369
316
|
/**
|
|
370
317
|
* Unregisters given processor.
|
|
371
|
-
* @param name
|
|
372
|
-
* @returns {I18N}
|
|
373
318
|
*/
|
|
374
|
-
|
|
375
|
-
|
|
376
319
|
unregisterProcessor(name) {
|
|
377
320
|
delete this.processors[name];
|
|
378
321
|
return this;
|
|
379
322
|
}
|
|
323
|
+
|
|
380
324
|
/**
|
|
381
325
|
* Returns default formats
|
|
382
|
-
* @returns {{date: string, time: string, datetime: string, number: string}}
|
|
383
326
|
*/
|
|
384
|
-
|
|
385
|
-
|
|
386
327
|
getDefaultFormats() {
|
|
387
328
|
return {
|
|
388
329
|
date: "DD/MM/YYYY",
|
|
@@ -402,47 +343,40 @@ class I18N {
|
|
|
402
343
|
}
|
|
403
344
|
};
|
|
404
345
|
}
|
|
346
|
+
|
|
405
347
|
/**
|
|
406
348
|
* Returns current format to be used when outputting dates.
|
|
407
349
|
*/
|
|
408
|
-
|
|
409
|
-
|
|
410
350
|
getDateFormat() {
|
|
411
|
-
return (0,
|
|
351
|
+
return (0, _get.default)(this.locale, "formats.date", this.defaultFormats.date);
|
|
412
352
|
}
|
|
353
|
+
|
|
413
354
|
/**
|
|
414
355
|
* Returns current format to be used when outputting time.
|
|
415
356
|
*/
|
|
416
|
-
|
|
417
|
-
|
|
418
357
|
getTimeFormat() {
|
|
419
|
-
return (0,
|
|
358
|
+
return (0, _get.default)(this.locale, "formats.time", this.defaultFormats.time);
|
|
420
359
|
}
|
|
360
|
+
|
|
421
361
|
/**
|
|
422
362
|
* Returns current format to be used when outputting date/time.
|
|
423
363
|
*/
|
|
424
|
-
|
|
425
|
-
|
|
426
364
|
getDateTimeFormat() {
|
|
427
|
-
return (0,
|
|
365
|
+
return (0, _get.default)(this.locale, "formats.datetime", this.defaultFormats.datetime);
|
|
428
366
|
}
|
|
367
|
+
|
|
429
368
|
/**
|
|
430
369
|
* Returns current format to be used when outputting prices.
|
|
431
370
|
*/
|
|
432
|
-
|
|
433
|
-
|
|
434
371
|
getPriceFormat() {
|
|
435
|
-
return (0,
|
|
372
|
+
return (0, _assign.default)({}, this.defaultFormats.price, (0, _get.default)(this.locale, "formats.price", {}));
|
|
436
373
|
}
|
|
374
|
+
|
|
437
375
|
/**
|
|
438
376
|
* Returns current format to be used when outputting numbers.
|
|
439
377
|
*/
|
|
440
|
-
|
|
441
|
-
|
|
442
378
|
getNumberFormat() {
|
|
443
|
-
return (0,
|
|
379
|
+
return (0, _assign.default)({}, this.defaultFormats.number, (0, _get.default)(this.locale, "formats.number", {}));
|
|
444
380
|
}
|
|
445
|
-
|
|
446
381
|
}
|
|
447
|
-
|
|
448
382
|
exports.default = I18N;
|