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