@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/cjs/index.js
CHANGED
|
@@ -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
|
-
|
|
15
|
-
|
|
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
|
-
|
|
23
|
-
|
|
9
|
+
const numberFormats = new Map();
|
|
10
|
+
const dateFormats = new Map();
|
|
24
11
|
function date(locales) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return
|
|
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
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
return
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
return
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
67
|
+
let formats = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
91
68
|
locales = locales || locale;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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(
|
|
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:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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:
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
};
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
-
|
|
179
|
-
|
|
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(
|
|
139
|
+
return message.reduce((message, token) => {
|
|
184
140
|
if (isString(token)) return message + token;
|
|
185
|
-
|
|
186
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
195
|
+
this.activate(params.locale, params.locales);
|
|
301
196
|
}
|
|
302
|
-
|
|
303
|
-
return _this;
|
|
304
197
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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
|
-
|
|
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
|
-
|
|
435
|
-
|
|
436
|
-
|
|
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
|
-
|
|
440
|
-
|
|
441
|
-
|
|
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
|
-
|
|
445
|
-
|
|
446
|
-
|
|
285
|
+
if (messageUnreachable) {
|
|
286
|
+
this.emit("missing", {
|
|
287
|
+
id,
|
|
288
|
+
context,
|
|
289
|
+
locale: this._locale
|
|
290
|
+
});
|
|
447
291
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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
|
-
|
|
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
|
-
|
|
316
|
+
let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
474
317
|
return new I18n(params);
|
|
475
318
|
}
|
|
476
319
|
|
|
477
|
-
|
|
320
|
+
const i18n = setupI18n();
|
|
478
321
|
|
|
479
322
|
exports.I18n = I18n;
|
|
480
323
|
exports.formats = formats;
|