@lingui/core 3.17.0 → 3.17.2
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/CHANGELOG.md +19 -0
- package/build/cjs/compile.js +25 -34
- package/build/cjs/compile.js.map +1 -1
- package/build/cjs/index.js +213 -370
- package/build/cjs/index.js.map +1 -1
- package/build/esm/compile.js +25 -34
- package/build/esm/compile.js.map +1 -1
- package/build/esm/index.js +213 -370
- package/build/esm/index.js.map +1 -1
- package/build/index.d.ts +18 -17
- package/package.json +3 -3
package/build/esm/index.js
CHANGED
|
@@ -1,76 +1,54 @@
|
|
|
1
|
-
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
2
|
-
import _createClass from '@babel/runtime/helpers/createClass';
|
|
3
|
-
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
|
4
|
-
import _inherits from '@babel/runtime/helpers/inherits';
|
|
5
|
-
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
6
|
-
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
|
|
7
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
8
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
9
|
-
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
10
1
|
import { compileMessage } from '@lingui/core/compile';
|
|
11
2
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
var isFunction = function isFunction(f) {
|
|
16
|
-
return typeof f === "function";
|
|
17
|
-
};
|
|
3
|
+
const isString = s => typeof s === "string";
|
|
4
|
+
const isFunction = f => typeof f === "function";
|
|
18
5
|
|
|
19
6
|
/** Memoized cache */
|
|
20
|
-
|
|
21
|
-
|
|
7
|
+
const numberFormats = new Map();
|
|
8
|
+
const dateFormats = new Map();
|
|
22
9
|
function date(locales) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return
|
|
10
|
+
let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
11
|
+
let memoize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
12
|
+
return value => {
|
|
26
13
|
if (isString(value)) value = new Date(value);
|
|
27
|
-
|
|
28
14
|
if (memoize) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
15
|
+
const key = cacheKey(locales, format);
|
|
16
|
+
const cachedFormatter = dateFormats.get(key);
|
|
32
17
|
if (cachedFormatter) {
|
|
33
18
|
return cachedFormatter.format(value);
|
|
34
19
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
dateFormats.set(key, _formatter);
|
|
39
|
-
return _formatter.format(value);
|
|
20
|
+
const formatter = new Intl.DateTimeFormat(locales, format);
|
|
21
|
+
dateFormats.set(key, formatter);
|
|
22
|
+
return formatter.format(value);
|
|
40
23
|
}
|
|
41
|
-
|
|
42
|
-
var formatter = new Intl.DateTimeFormat(locales, format);
|
|
24
|
+
const formatter = new Intl.DateTimeFormat(locales, format);
|
|
43
25
|
return formatter.format(value);
|
|
44
26
|
};
|
|
45
27
|
}
|
|
46
28
|
function number(locales) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return
|
|
29
|
+
let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
30
|
+
let memoize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
31
|
+
return value => {
|
|
50
32
|
if (memoize) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
33
|
+
const key = cacheKey(locales, format);
|
|
34
|
+
const cachedFormatter = numberFormats.get(key);
|
|
54
35
|
if (cachedFormatter) {
|
|
55
36
|
return cachedFormatter.format(value);
|
|
56
37
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
numberFormats.set(key, _formatter2);
|
|
61
|
-
return _formatter2.format(value);
|
|
38
|
+
const formatter = new Intl.NumberFormat(locales, format);
|
|
39
|
+
numberFormats.set(key, formatter);
|
|
40
|
+
return formatter.format(value);
|
|
62
41
|
}
|
|
63
|
-
|
|
64
|
-
var formatter = new Intl.NumberFormat(locales, format);
|
|
42
|
+
const formatter = new Intl.NumberFormat(locales, format);
|
|
65
43
|
return formatter.format(value);
|
|
66
44
|
};
|
|
67
45
|
}
|
|
68
|
-
/** Memoize helpers */
|
|
69
46
|
|
|
47
|
+
/** Memoize helpers */
|
|
70
48
|
function cacheKey(locales) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return
|
|
49
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
50
|
+
const localeKey = Array.isArray(locales) ? locales.sort().join("-") : locales;
|
|
51
|
+
return `${localeKey}-${JSON.stringify(options)}`;
|
|
74
52
|
}
|
|
75
53
|
|
|
76
54
|
var formats = /*#__PURE__*/Object.freeze({
|
|
@@ -79,72 +57,56 @@ var formats = /*#__PURE__*/Object.freeze({
|
|
|
79
57
|
number: number
|
|
80
58
|
});
|
|
81
59
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
var localeData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
|
|
60
|
+
const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
|
|
61
|
+
const defaultFormats = function (locale, locales) {
|
|
62
|
+
let localeData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
|
|
86
63
|
plurals: undefined
|
|
87
64
|
};
|
|
88
|
-
|
|
65
|
+
let formats = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
89
66
|
locales = locales || locale;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
var valueStr = number(locales, numberFormat)(value);
|
|
105
|
-
|
|
106
|
-
return norm.map(function (m) {
|
|
107
|
-
return isString(m) ? m.replace("#", valueStr) : m;
|
|
108
|
-
});
|
|
67
|
+
const {
|
|
68
|
+
plurals
|
|
69
|
+
} = localeData;
|
|
70
|
+
const style = format => isString(format) ? formats[format] || {
|
|
71
|
+
style: format
|
|
72
|
+
} : format;
|
|
73
|
+
const replaceOctothorpe = (value, message) => {
|
|
74
|
+
return ctx => {
|
|
75
|
+
const msg = isFunction(message) ? message(ctx) : message;
|
|
76
|
+
const norm = Array.isArray(msg) ? msg : [msg];
|
|
77
|
+
const numberFormat = Object.keys(formats).length ? style("number") : {};
|
|
78
|
+
const valueStr = number(locales, numberFormat)(value);
|
|
79
|
+
return norm.map(m => isString(m) ? m.replace("#", valueStr) : m);
|
|
109
80
|
};
|
|
110
81
|
};
|
|
111
|
-
|
|
112
82
|
if (!plurals) {
|
|
113
|
-
console.error(
|
|
83
|
+
console.error(`Plurals for locale ${locale} aren't loaded. Use i18n.loadLocaleData method to load plurals for specific locale. Using other plural rule as a fallback.`);
|
|
114
84
|
}
|
|
115
|
-
|
|
116
85
|
return {
|
|
117
|
-
plural:
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
86
|
+
plural: (value, _ref) => {
|
|
87
|
+
let {
|
|
88
|
+
offset = 0,
|
|
89
|
+
...rules
|
|
90
|
+
} = _ref;
|
|
91
|
+
const message = rules[value] || rules[plurals === null || plurals === void 0 ? void 0 : plurals(value - offset)] || rules.other;
|
|
123
92
|
return replaceOctothorpe(value - offset, message);
|
|
124
93
|
},
|
|
125
|
-
selectordinal:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
94
|
+
selectordinal: (value, _ref2) => {
|
|
95
|
+
let {
|
|
96
|
+
offset = 0,
|
|
97
|
+
...rules
|
|
98
|
+
} = _ref2;
|
|
99
|
+
const message = rules[value] || rules[plurals === null || plurals === void 0 ? void 0 : plurals(value - offset, true)] || rules.other;
|
|
131
100
|
return replaceOctothorpe(value - offset, message);
|
|
132
101
|
},
|
|
133
|
-
select:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return number(locales, style(format))(value);
|
|
138
|
-
},
|
|
139
|
-
date: function date$1(value, format) {
|
|
140
|
-
return date(locales, style(format))(value);
|
|
141
|
-
},
|
|
142
|
-
undefined: function undefined$1(value) {
|
|
143
|
-
return value;
|
|
144
|
-
}
|
|
102
|
+
select: (value, rules) => rules[value] || rules.other,
|
|
103
|
+
number: (value, format) => number(locales, style(format))(value),
|
|
104
|
+
date: (value, format) => date(locales, style(format))(value),
|
|
105
|
+
undefined: value => value
|
|
145
106
|
};
|
|
146
|
-
};
|
|
107
|
+
};
|
|
147
108
|
|
|
109
|
+
// Params -> CTX
|
|
148
110
|
/**
|
|
149
111
|
* Creates a context object, which formats ICU MessageFormat arguments based on
|
|
150
112
|
* argument type.
|
|
@@ -156,323 +118,204 @@ var defaultFormats = function defaultFormats(locale, locales) {
|
|
|
156
118
|
* @param formats - Custom format styles
|
|
157
119
|
* @returns {function(string, string, any)}
|
|
158
120
|
*/
|
|
159
|
-
|
|
160
|
-
|
|
161
121
|
function context(locale, locales, values, formats, localeData) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
var message = isFunction(formatted) ? formatted(ctx) : formatted;
|
|
122
|
+
const formatters = defaultFormats(locale, locales, localeData, formats);
|
|
123
|
+
const ctx = (name, type, format) => {
|
|
124
|
+
const value = values[name];
|
|
125
|
+
const formatted = formatters[type](value, format);
|
|
126
|
+
const message = isFunction(formatted) ? formatted(ctx) : formatted;
|
|
168
127
|
return Array.isArray(message) ? message.join("") : message;
|
|
169
128
|
};
|
|
170
|
-
|
|
171
129
|
return ctx;
|
|
172
130
|
}
|
|
173
|
-
|
|
174
131
|
function interpolate(translation, locale, locales, localeData) {
|
|
175
132
|
return function (values) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
var formatMessage = function formatMessage(message) {
|
|
133
|
+
let formats = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
134
|
+
const ctx = context(locale, locales, values, formats, localeData);
|
|
135
|
+
const formatMessage = message => {
|
|
180
136
|
if (!Array.isArray(message)) return message;
|
|
181
|
-
return message.reduce(
|
|
137
|
+
return message.reduce((message, token) => {
|
|
182
138
|
if (isString(token)) return message + token;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
name = _token[0],
|
|
186
|
-
type = _token[1],
|
|
187
|
-
format = _token[2];
|
|
188
|
-
|
|
189
|
-
var interpolatedFormat = {};
|
|
190
|
-
|
|
139
|
+
const [name, type, format] = token;
|
|
140
|
+
let interpolatedFormat = {};
|
|
191
141
|
if (format != null && !isString(format)) {
|
|
192
|
-
Object.keys(format).forEach(
|
|
142
|
+
Object.keys(format).forEach(key => {
|
|
193
143
|
interpolatedFormat[key] = formatMessage(format[key]);
|
|
194
144
|
});
|
|
195
145
|
} else {
|
|
196
146
|
interpolatedFormat = format;
|
|
197
147
|
}
|
|
198
|
-
|
|
199
|
-
var value = ctx(name, type, interpolatedFormat);
|
|
148
|
+
const value = ctx(name, type, interpolatedFormat);
|
|
200
149
|
if (value == null) return message;
|
|
201
150
|
return message + value;
|
|
202
151
|
}, "");
|
|
203
152
|
};
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (isString(result) && UNICODE_REGEX.test(result)) return JSON.parse("\"".concat(result.trim(), "\""));
|
|
153
|
+
const result = formatMessage(translation);
|
|
154
|
+
if (isString(result) && UNICODE_REGEX.test(result)) return JSON.parse(`"${result.trim()}"`);
|
|
207
155
|
if (isString(result)) return result.trim();
|
|
208
156
|
return result;
|
|
209
157
|
};
|
|
210
158
|
}
|
|
211
159
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
160
|
+
class EventEmitter {
|
|
161
|
+
_events = {};
|
|
162
|
+
on(event, listener) {
|
|
163
|
+
if (!this._hasEvent(event)) this._events[event] = [];
|
|
164
|
+
this._events[event].push(listener);
|
|
165
|
+
return () => this.removeListener(event, listener);
|
|
217
166
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
this._events[event].push(listener);
|
|
227
|
-
|
|
228
|
-
return function () {
|
|
229
|
-
return _this.removeListener(event, listener);
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
}, {
|
|
233
|
-
key: "removeListener",
|
|
234
|
-
value: function removeListener(event, listener) {
|
|
235
|
-
if (!this._hasEvent(event)) return;
|
|
236
|
-
|
|
237
|
-
var index = this._events[event].indexOf(listener);
|
|
238
|
-
|
|
239
|
-
if (~index) this._events[event].splice(index, 1);
|
|
240
|
-
}
|
|
241
|
-
}, {
|
|
242
|
-
key: "emit",
|
|
243
|
-
value: function emit(event) {
|
|
244
|
-
var _this2 = this;
|
|
245
|
-
|
|
246
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
247
|
-
args[_key - 1] = arguments[_key];
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (!this._hasEvent(event)) return;
|
|
251
|
-
|
|
252
|
-
this._events[event].map(function (listener) {
|
|
253
|
-
return listener.apply(_this2, args);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
}, {
|
|
257
|
-
key: "_hasEvent",
|
|
258
|
-
value: function _hasEvent(event) {
|
|
259
|
-
return Array.isArray(this._events[event]);
|
|
167
|
+
removeListener(event, listener) {
|
|
168
|
+
if (!this._hasEvent(event)) return;
|
|
169
|
+
const index = this._events[event].indexOf(listener);
|
|
170
|
+
if (~index) this._events[event].splice(index, 1);
|
|
171
|
+
}
|
|
172
|
+
emit(event) {
|
|
173
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
174
|
+
args[_key - 1] = arguments[_key];
|
|
260
175
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
|
269
|
-
var I18n = /*#__PURE__*/function (_EventEmitter) {
|
|
270
|
-
_inherits(I18n, _EventEmitter);
|
|
271
|
-
|
|
272
|
-
var _super = _createSuper(I18n);
|
|
273
|
-
|
|
274
|
-
function I18n(params) {
|
|
275
|
-
var _this;
|
|
276
|
-
|
|
277
|
-
_classCallCheck(this, I18n);
|
|
278
|
-
|
|
279
|
-
_this = _super.call(this);
|
|
280
|
-
|
|
281
|
-
_defineProperty(_assertThisInitialized(_this), "_locale", void 0);
|
|
282
|
-
|
|
283
|
-
_defineProperty(_assertThisInitialized(_this), "_locales", void 0);
|
|
284
|
-
|
|
285
|
-
_defineProperty(_assertThisInitialized(_this), "_localeData", void 0);
|
|
286
|
-
|
|
287
|
-
_defineProperty(_assertThisInitialized(_this), "_messages", void 0);
|
|
288
|
-
|
|
289
|
-
_defineProperty(_assertThisInitialized(_this), "_missing", void 0);
|
|
290
|
-
|
|
291
|
-
_this._messages = {};
|
|
292
|
-
_this._localeData = {};
|
|
293
|
-
if (params.missing != null) _this._missing = params.missing;
|
|
294
|
-
if (params.messages != null) _this.load(params.messages);
|
|
295
|
-
if (params.localeData != null) _this.loadLocaleData(params.localeData);
|
|
176
|
+
if (!this._hasEvent(event)) return;
|
|
177
|
+
this._events[event].map(listener => listener.apply(this, args));
|
|
178
|
+
}
|
|
179
|
+
_hasEvent(event) {
|
|
180
|
+
return Array.isArray(this._events[event]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
296
183
|
|
|
184
|
+
class I18n extends EventEmitter {
|
|
185
|
+
constructor(params) {
|
|
186
|
+
super();
|
|
187
|
+
this._messages = {};
|
|
188
|
+
this._localeData = {};
|
|
189
|
+
if (params.missing != null) this._missing = params.missing;
|
|
190
|
+
if (params.messages != null) this.load(params.messages);
|
|
191
|
+
if (params.localeData != null) this.loadLocaleData(params.localeData);
|
|
297
192
|
if (params.locale != null || params.locales != null) {
|
|
298
|
-
|
|
193
|
+
this.activate(params.locale, params.locales);
|
|
299
194
|
}
|
|
300
|
-
|
|
301
|
-
return _this;
|
|
302
195
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
196
|
+
get locale() {
|
|
197
|
+
return this._locale;
|
|
198
|
+
}
|
|
199
|
+
get locales() {
|
|
200
|
+
return this._locales;
|
|
201
|
+
}
|
|
202
|
+
get messages() {
|
|
203
|
+
return this._messages[this._locale] ?? {};
|
|
204
|
+
}
|
|
205
|
+
get localeData() {
|
|
206
|
+
return this._localeData[this._locale] ?? {};
|
|
207
|
+
}
|
|
208
|
+
_loadLocaleData(locale, localeData) {
|
|
209
|
+
if (this._localeData[locale] == null) {
|
|
210
|
+
this._localeData[locale] = localeData;
|
|
211
|
+
} else {
|
|
212
|
+
Object.assign(this._localeData[locale], localeData);
|
|
312
213
|
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
// loadLocaleData(allLocaleData)
|
|
324
|
-
// Loading all locale data at once.
|
|
325
|
-
Object.keys(localeOrAllData).forEach(function (locale) {
|
|
326
|
-
return _this2._loadLocaleData(locale, localeOrAllData[locale]);
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
this.emit("change");
|
|
214
|
+
}
|
|
215
|
+
loadLocaleData(localeOrAllData, localeData) {
|
|
216
|
+
if (localeData != null) {
|
|
217
|
+
// loadLocaleData('en', enLocaleData)
|
|
218
|
+
// Loading locale data for a single locale.
|
|
219
|
+
this._loadLocaleData(localeOrAllData, localeData);
|
|
220
|
+
} else {
|
|
221
|
+
// loadLocaleData(allLocaleData)
|
|
222
|
+
// Loading all locale data at once.
|
|
223
|
+
Object.keys(localeOrAllData).forEach(locale => this._loadLocaleData(locale, localeOrAllData[locale]));
|
|
331
224
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
225
|
+
this.emit("change");
|
|
226
|
+
}
|
|
227
|
+
_load(locale, messages) {
|
|
228
|
+
if (this._messages[locale] == null) {
|
|
229
|
+
this._messages[locale] = messages;
|
|
230
|
+
} else {
|
|
231
|
+
Object.assign(this._messages[locale], messages);
|
|
340
232
|
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// load(catalogs)
|
|
352
|
-
// Loading several locales at once.
|
|
353
|
-
Object.keys(localeOrMessages).forEach(function (locale) {
|
|
354
|
-
return _this3._load(locale, localeOrMessages[locale]);
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
this.emit("change");
|
|
233
|
+
}
|
|
234
|
+
load(localeOrMessages, messages) {
|
|
235
|
+
if (messages != null) {
|
|
236
|
+
// load('en', catalog)
|
|
237
|
+
// Loading a catalog for a single locale.
|
|
238
|
+
this._load(localeOrMessages, messages);
|
|
239
|
+
} else {
|
|
240
|
+
// load(catalogs)
|
|
241
|
+
// Loading several locales at once.
|
|
242
|
+
Object.keys(localeOrMessages).forEach(locale => this._load(locale, localeOrMessages[locale]));
|
|
359
243
|
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (!this._localeData[locale]) {
|
|
369
|
-
console.warn("Locale data for locale \"".concat(locale, "\" not loaded. Plurals won't work correctly."));
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
this._locale = locale;
|
|
374
|
-
this._locales = locales;
|
|
375
|
-
this.emit("change");
|
|
376
|
-
} // method for translation and formatting
|
|
377
|
-
|
|
378
|
-
}, {
|
|
379
|
-
key: "_",
|
|
380
|
-
value: function _(id) {
|
|
381
|
-
var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
382
|
-
|
|
383
|
-
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
|
|
384
|
-
message = _ref.message,
|
|
385
|
-
formats = _ref.formats,
|
|
386
|
-
context = _ref.context;
|
|
387
|
-
|
|
388
|
-
if (!isString(id)) {
|
|
389
|
-
values = id.values || values;
|
|
390
|
-
message = id.message;
|
|
391
|
-
context = id.context;
|
|
392
|
-
id = id.id;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
var messageMissing = !context && !this.messages[id];
|
|
396
|
-
var contextualMessageMissing = context && !this.messages[context][id];
|
|
397
|
-
var messageUnreachable = contextualMessageMissing || messageMissing; // replace missing messages with custom message for debugging
|
|
398
|
-
|
|
399
|
-
var missing = this._missing;
|
|
400
|
-
|
|
401
|
-
if (missing && messageUnreachable) {
|
|
402
|
-
return isFunction(missing) ? missing(this._locale, id, context) : missing;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if (messageUnreachable) {
|
|
406
|
-
this.emit("missing", {
|
|
407
|
-
id: id,
|
|
408
|
-
context: context,
|
|
409
|
-
locale: this._locale
|
|
410
|
-
});
|
|
244
|
+
this.emit("change");
|
|
245
|
+
}
|
|
246
|
+
activate(locale, locales) {
|
|
247
|
+
if (process.env.NODE_ENV !== "production") {
|
|
248
|
+
if (!this._messages[locale]) {
|
|
249
|
+
console.warn(`Messages for locale "${locale}" not loaded.`);
|
|
411
250
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
if (context && !contextualMessageMissing) {
|
|
416
|
-
// context is like a subdirectory of other keys
|
|
417
|
-
translation = this.messages[context][id] || message || id;
|
|
418
|
-
} else {
|
|
419
|
-
translation = this.messages[id] || message || id;
|
|
251
|
+
if (!this._localeData[locale]) {
|
|
252
|
+
console.warn(`Locale data for locale "${locale}" not loaded. Plurals won't work correctly.`);
|
|
420
253
|
}
|
|
421
|
-
|
|
422
|
-
if (process.env.NODE_ENV !== "production") {
|
|
423
|
-
translation = isString(translation) ? compileMessage(translation) : translation;
|
|
424
|
-
} // hack for parsing unicode values inside a string to get parsed in react native environments
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
if (isString(translation) && UNICODE_REGEX.test(translation)) return JSON.parse("\"".concat(translation, "\""));
|
|
428
|
-
if (isString(translation)) return translation;
|
|
429
|
-
return interpolate(translation, this._locale, this._locales, this.localeData)(values, formats);
|
|
430
254
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
255
|
+
this._locale = locale;
|
|
256
|
+
this._locales = locales;
|
|
257
|
+
this.emit("change");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// method for translation and formatting
|
|
261
|
+
_(id) {
|
|
262
|
+
let values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
263
|
+
let {
|
|
264
|
+
message,
|
|
265
|
+
formats,
|
|
266
|
+
context
|
|
267
|
+
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
268
|
+
if (!isString(id)) {
|
|
269
|
+
values = id.values || values;
|
|
270
|
+
message = id.message;
|
|
271
|
+
context = id.context;
|
|
272
|
+
id = id.id;
|
|
435
273
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
274
|
+
const messageMissing = !context && !this.messages[id];
|
|
275
|
+
const contextualMessageMissing = context && !this.messages[context][id];
|
|
276
|
+
const messageUnreachable = contextualMessageMissing || messageMissing;
|
|
277
|
+
|
|
278
|
+
// replace missing messages with custom message for debugging
|
|
279
|
+
const missing = this._missing;
|
|
280
|
+
if (missing && messageUnreachable) {
|
|
281
|
+
return isFunction(missing) ? missing(this._locale, id, context) : missing;
|
|
440
282
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
283
|
+
if (messageUnreachable) {
|
|
284
|
+
this.emit("missing", {
|
|
285
|
+
id,
|
|
286
|
+
context,
|
|
287
|
+
locale: this._locale
|
|
288
|
+
});
|
|
445
289
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
290
|
+
let translation;
|
|
291
|
+
if (context && !contextualMessageMissing) {
|
|
292
|
+
// context is like a subdirectory of other keys
|
|
293
|
+
translation = this.messages[context][id] || message || id;
|
|
294
|
+
} else {
|
|
295
|
+
translation = this.messages[id] || message || id;
|
|
450
296
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
get: function get() {
|
|
454
|
-
var _this$_messages$this$;
|
|
455
|
-
|
|
456
|
-
return (_this$_messages$this$ = this._messages[this._locale]) !== null && _this$_messages$this$ !== void 0 ? _this$_messages$this$ : {};
|
|
297
|
+
if (process.env.NODE_ENV !== "production") {
|
|
298
|
+
translation = isString(translation) ? compileMessage(translation) : translation;
|
|
457
299
|
}
|
|
458
|
-
}, {
|
|
459
|
-
key: "localeData",
|
|
460
|
-
get: function get() {
|
|
461
|
-
var _this$_localeData$thi;
|
|
462
|
-
|
|
463
|
-
return (_this$_localeData$thi = this._localeData[this._locale]) !== null && _this$_localeData$thi !== void 0 ? _this$_localeData$thi : {};
|
|
464
|
-
}
|
|
465
|
-
}]);
|
|
466
|
-
|
|
467
|
-
return I18n;
|
|
468
|
-
}(EventEmitter);
|
|
469
300
|
|
|
301
|
+
// hack for parsing unicode values inside a string to get parsed in react native environments
|
|
302
|
+
if (isString(translation) && UNICODE_REGEX.test(translation)) return JSON.parse(`"${translation}"`);
|
|
303
|
+
if (isString(translation)) return translation;
|
|
304
|
+
return interpolate(translation, this._locale, this._locales, this.localeData)(values, formats);
|
|
305
|
+
}
|
|
306
|
+
date(value, format) {
|
|
307
|
+
return date(this._locales || this._locale, format)(value);
|
|
308
|
+
}
|
|
309
|
+
number(value, format) {
|
|
310
|
+
return number(this._locales || this._locale, format)(value);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
470
313
|
function setupI18n() {
|
|
471
|
-
|
|
314
|
+
let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
472
315
|
return new I18n(params);
|
|
473
316
|
}
|
|
474
317
|
|
|
475
|
-
|
|
318
|
+
const i18n = setupI18n();
|
|
476
319
|
|
|
477
320
|
export { I18n, formats, i18n, setupI18n };
|
|
478
321
|
//# sourceMappingURL=index.js.map
|