i18next 7.1.3 → 7.2.3

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 CHANGED
@@ -1,3 +1,17 @@
1
+ ### 7.2.3
2
+ - rebuild seems we had a mistake in pushing latest build
3
+
4
+ ### 7.2.2
5
+ - fixes issue in accepting string as a result of "nested" lookup [PR909](https://github.com/i18next/i18next/pull/909)
6
+
7
+ ### 7.2.1
8
+ - fixes iterating over object's own properties [#904](https://github.com/i18next/i18next/pull/904)
9
+
10
+ ### 7.2.0
11
+ - new init option simplifyPluralSuffix - setting it to false will treat all plurals using suffix numbers even for locals only having singular and plural
12
+ - even if no lng set or detected at least load the fallback languages
13
+ - delay init call on createInstance if not set initImmediate to false [#879](https://github.com/i18next/i18next/issues/879)
14
+
1
15
  ### 7.1.3
2
16
  - fixes issue in returnObject tree called with options including ns: [react-i18next #240](https://github.com/i18next/react-i18next/issues/240)
3
17
 
@@ -215,27 +215,29 @@ var Connector = function (_EventEmitter) {
215
215
 
216
216
  // load one by one
217
217
  else {
218
- var readOne = function readOne(name) {
219
- var _this6 = this;
218
+ (function () {
219
+ var readOne = function readOne(name) {
220
+ var _this6 = this;
220
221
 
221
- var _name$split5 = name.split('|'),
222
- _name$split6 = _slicedToArray(_name$split5, 2),
223
- lng = _name$split6[0],
224
- ns = _name$split6[1];
222
+ var _name$split5 = name.split('|'),
223
+ _name$split6 = _slicedToArray(_name$split5, 2),
224
+ lng = _name$split6[0],
225
+ ns = _name$split6[1];
225
226
 
226
- this.read(lng, ns, 'read', null, null, function (err, data) {
227
- if (err) _this6.logger.warn('loading namespace ' + ns + ' for language ' + lng + ' failed', err);
228
- if (!err && data) _this6.logger.log('loaded namespace ' + ns + ' for language ' + lng, data);
227
+ this.read(lng, ns, 'read', null, null, function (err, data) {
228
+ if (err) _this6.logger.warn('loading namespace ' + ns + ' for language ' + lng + ' failed', err);
229
+ if (!err && data) _this6.logger.log('loaded namespace ' + ns + ' for language ' + lng, data);
229
230
 
230
- _this6.loaded(name, err, data);
231
- });
232
- };
231
+ _this6.loaded(name, err, data);
232
+ });
233
+ };
233
234
 
234
- ;
235
+ ;
235
236
 
236
- toLoad.toLoad.forEach(function (name) {
237
- readOne.call(_this5, name);
238
- });
237
+ toLoad.toLoad.forEach(function (name) {
238
+ readOne.call(_this5, name);
239
+ });
240
+ })();
239
241
  }
240
242
  };
241
243
 
@@ -273,29 +275,31 @@ var Connector = function (_EventEmitter) {
273
275
 
274
276
  // load one by one
275
277
  else {
276
- var readOne = function readOne(name) {
277
- var _this8 = this;
278
+ (function () {
279
+ var readOne = function readOne(name) {
280
+ var _this8 = this;
278
281
 
279
- var _name$split7 = name.split('|'),
280
- _name$split8 = _slicedToArray(_name$split7, 2),
281
- lng = _name$split8[0],
282
- ns = _name$split8[1];
282
+ var _name$split7 = name.split('|'),
283
+ _name$split8 = _slicedToArray(_name$split7, 2),
284
+ lng = _name$split8[0],
285
+ ns = _name$split8[1];
283
286
 
284
- this.read(lng, ns, 'read', null, null, function (err, data) {
285
- if (err) _this8.logger.warn('reloading namespace ' + ns + ' for language ' + lng + ' failed', err);
286
- if (!err && data) _this8.logger.log('reloaded namespace ' + ns + ' for language ' + lng, data);
287
+ this.read(lng, ns, 'read', null, null, function (err, data) {
288
+ if (err) _this8.logger.warn('reloading namespace ' + ns + ' for language ' + lng + ' failed', err);
289
+ if (!err && data) _this8.logger.log('reloaded namespace ' + ns + ' for language ' + lng, data);
287
290
 
288
- _this8.loaded(name, err, data);
289
- });
290
- };
291
+ _this8.loaded(name, err, data);
292
+ });
293
+ };
291
294
 
292
- ;
295
+ ;
293
296
 
294
- languages.forEach(function (l) {
295
- namespaces.forEach(function (n) {
296
- readOne.call(_this7, l + '|' + n);
297
+ languages.forEach(function (l) {
298
+ namespaces.forEach(function (n) {
299
+ readOne.call(_this7, l + '|' + n);
300
+ });
297
301
  });
298
- });
302
+ })();
299
303
  }
300
304
  };
301
305
 
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
 
7
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8
+
7
9
  var _logger = require('./logger');
8
10
 
9
11
  var _logger2 = _interopRequireDefault(_logger);
@@ -131,40 +133,58 @@ var PluralResolver = function () {
131
133
  var rule = this.getRule(code);
132
134
 
133
135
  if (rule) {
134
- if (rule.numbers.length === 1) return ''; // only singular
135
-
136
- var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
137
- var suffix = rule.numbers[idx];
138
-
139
- // special treatment for lngs only having singular and plural
140
- if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
141
- if (suffix === 2) {
142
- suffix = 'plural';
143
- } else if (suffix === 1) {
144
- suffix = '';
136
+ var _ret = function () {
137
+ if (rule.numbers.length === 1) return {
138
+ v: ''
139
+ }; // only singular
140
+
141
+ var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
142
+ var suffix = rule.numbers[idx];
143
+
144
+ // special treatment for lngs only having singular and plural
145
+ if (_this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
146
+ if (suffix === 2) {
147
+ suffix = 'plural';
148
+ } else if (suffix === 1) {
149
+ suffix = '';
150
+ }
145
151
  }
146
- }
147
-
148
- var returnSuffix = function returnSuffix() {
149
- return _this.options.prepend && suffix.toString() ? _this.options.prepend + suffix.toString() : suffix.toString();
150
- };
151
152
 
152
- // COMPATIBILITY JSON
153
- // v1
154
- if (this.options.compatibilityJSON === 'v1') {
155
- if (suffix === 1) return '';
156
- if (typeof suffix === 'number') return '_plural_' + suffix.toString();
157
- return returnSuffix();
158
- }
159
- // v2
160
- else if (this.options.compatibilityJSON === 'v2' || rule.numbers.length === 2 && rule.numbers[0] === 1) {
161
- return returnSuffix();
153
+ var returnSuffix = function returnSuffix() {
154
+ return _this.options.prepend && suffix.toString() ? _this.options.prepend + suffix.toString() : suffix.toString();
155
+ };
156
+
157
+ // COMPATIBILITY JSON
158
+ // v1
159
+ if (_this.options.compatibilityJSON === 'v1') {
160
+ if (suffix === 1) return {
161
+ v: ''
162
+ };
163
+ if (typeof suffix === 'number') return {
164
+ v: '_plural_' + suffix.toString()
165
+ };
166
+ return {
167
+ v: returnSuffix()
168
+ };
162
169
  }
163
- // v3 - gettext index
164
- else if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
165
- return returnSuffix();
170
+ // v2
171
+ else if (_this.options.compatibilityJSON === 'v2' || rule.numbers.length === 2 && rule.numbers[0] === 1) {
172
+ return {
173
+ v: returnSuffix()
174
+ };
166
175
  }
167
- return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
176
+ // v3 - gettext index
177
+ else if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
178
+ return {
179
+ v: returnSuffix()
180
+ };
181
+ }
182
+ return {
183
+ v: _this.options.prepend && idx.toString() ? _this.options.prepend + idx.toString() : idx.toString()
184
+ };
185
+ }();
186
+
187
+ if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
168
188
  } else {
169
189
  this.logger.warn('no plural rule found for: ' + code);
170
190
  return '';
@@ -147,7 +147,9 @@ var Translator = function (_EventEmitter) {
147
147
  var copy = resType === '[object Array]' ? [] : {}; // apply child translation on a copy
148
148
 
149
149
  for (var m in res) {
150
- copy[m] = this.translate('' + key + keySeparator + m, _extends({}, options, { joinArrays: false, ns: namespaces }));
150
+ if (res.hasOwnProperty(m)) {
151
+ copy[m] = this.translate('' + key + keySeparator + m, _extends({}, options, { joinArrays: false, ns: namespaces }));
152
+ }
151
153
  }
152
154
  res = copy;
153
155
  }
@@ -20,6 +20,7 @@ function get() {
20
20
  load: 'all', // | currentOnly | languageOnly
21
21
  preload: false, // array with preload languages
22
22
 
23
+ simplifyPluralSuffix: true,
23
24
  keySeparator: '.',
24
25
  nsSeparator: ':',
25
26
  pluralSeparator: '_',
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
 
7
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8
+
7
9
  var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
10
 
9
11
  var _logger = require('./logger');
@@ -82,7 +84,15 @@ var I18n = function (_EventEmitter) {
82
84
  _this.logger = _logger2.default;
83
85
  _this.modules = { external: [] };
84
86
 
85
- if (callback && !_this.isInitialized && !options.isClone) _this.init(options, callback);
87
+ if (callback && !_this.isInitialized && !options.isClone) {
88
+ var _ret;
89
+
90
+ // https://github.com/i18next/i18next/issues/879
91
+ if (!_this.options.initImmediate) return _ret = _this.init(options, callback), _possibleConstructorReturn(_this, _ret);
92
+ setTimeout(function () {
93
+ _this.init(options, callback);
94
+ }, 0);
95
+ }
86
96
  return _this;
87
97
  }
88
98
 
@@ -128,7 +138,7 @@ var I18n = function (_EventEmitter) {
128
138
  s.cacheConnector.save();
129
139
  });
130
140
  s.languageUtils = lu;
131
- s.pluralResolver = new _PluralResolver2.default(lu, { prepend: this.options.pluralSeparator, compatibilityJSON: this.options.compatibilityJSON });
141
+ s.pluralResolver = new _PluralResolver2.default(lu, { prepend: this.options.pluralSeparator, compatibilityJSON: this.options.compatibilityJSON, simplifyPluralSuffix: this.options.simplifyPluralSuffix });
132
142
  s.interpolator = new _Interpolator2.default(this.options);
133
143
 
134
144
  s.backendConnector = new _BackendConnector2.default(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
@@ -211,29 +221,43 @@ var I18n = function (_EventEmitter) {
211
221
  var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
212
222
 
213
223
  if (!this.options.resources) {
214
- if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode
215
-
216
- var toLoad = [];
217
-
218
- var append = function append(lng) {
219
- if (!lng) return;
220
- var lngs = _this3.services.languageUtils.toResolveHierarchy(lng);
221
- lngs.forEach(function (l) {
222
- if (toLoad.indexOf(l) < 0) toLoad.push(l);
223
- });
224
- };
224
+ var _ret2 = function () {
225
+ if (_this3.language && _this3.language.toLowerCase() === 'cimode') return {
226
+ v: callback()
227
+ }; // avoid loading resources for cimode
228
+
229
+ var toLoad = [];
230
+
231
+ var append = function append(lng) {
232
+ if (!lng) return;
233
+ var lngs = _this3.services.languageUtils.toResolveHierarchy(lng);
234
+ lngs.forEach(function (l) {
235
+ if (toLoad.indexOf(l) < 0) toLoad.push(l);
236
+ });
237
+ };
238
+
239
+ if (!_this3.language) {
240
+ // at least load fallbacks in this case
241
+ var fallbacks = _this3.services.languageUtils.getFallbackCodes(_this3.options.fallbackLng);
242
+ fallbacks.forEach(function (l) {
243
+ return append(l);
244
+ });
245
+ } else {
246
+ append(_this3.language);
247
+ }
225
248
 
226
- append(this.language);
249
+ if (_this3.options.preload) {
250
+ _this3.options.preload.forEach(function (l) {
251
+ return append(l);
252
+ });
253
+ }
227
254
 
228
- if (this.options.preload) {
229
- this.options.preload.forEach(function (l) {
230
- append(l);
255
+ _this3.services.cacheConnector.load(toLoad, _this3.options.ns, function () {
256
+ _this3.services.backendConnector.load(toLoad, _this3.options.ns, callback);
231
257
  });
232
- }
258
+ }();
233
259
 
234
- this.services.cacheConnector.load(toLoad, this.options.ns, function () {
235
- _this3.services.backendConnector.load(toLoad, _this3.options.ns, callback);
236
- });
260
+ if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
237
261
  } else {
238
262
  callback(null);
239
263
  }
@@ -27,16 +27,20 @@ function getLastOfPath(object, path, Empty) {
27
27
  return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key;
28
28
  }
29
29
 
30
+ function canNotTraverseDeeper() {
31
+ return !object || typeof object === 'string';
32
+ }
33
+
30
34
  var stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
31
35
  while (stack.length > 1) {
32
- if (!object) return {};
36
+ if (canNotTraverseDeeper()) return {};
33
37
 
34
38
  var key = cleanKey(stack.shift());
35
39
  if (!object[key] && Empty) object[key] = new Empty();
36
40
  object = object[key];
37
41
  }
38
42
 
39
- if (!object) return {};
43
+ if (canNotTraverseDeeper()) return {};
40
44
  return {
41
45
  obj: object,
42
46
  k: cleanKey(stack.shift())
@@ -197,27 +197,29 @@ var Connector = function (_EventEmitter) {
197
197
 
198
198
  // load one by one
199
199
  else {
200
- var readOne = function readOne(name) {
201
- var _this6 = this;
200
+ (function () {
201
+ var readOne = function readOne(name) {
202
+ var _this6 = this;
202
203
 
203
- var _name$split5 = name.split('|'),
204
- _name$split6 = _slicedToArray(_name$split5, 2),
205
- lng = _name$split6[0],
206
- ns = _name$split6[1];
204
+ var _name$split5 = name.split('|'),
205
+ _name$split6 = _slicedToArray(_name$split5, 2),
206
+ lng = _name$split6[0],
207
+ ns = _name$split6[1];
207
208
 
208
- this.read(lng, ns, 'read', null, null, function (err, data) {
209
- if (err) _this6.logger.warn('loading namespace ' + ns + ' for language ' + lng + ' failed', err);
210
- if (!err && data) _this6.logger.log('loaded namespace ' + ns + ' for language ' + lng, data);
209
+ this.read(lng, ns, 'read', null, null, function (err, data) {
210
+ if (err) _this6.logger.warn('loading namespace ' + ns + ' for language ' + lng + ' failed', err);
211
+ if (!err && data) _this6.logger.log('loaded namespace ' + ns + ' for language ' + lng, data);
211
212
 
212
- _this6.loaded(name, err, data);
213
- });
214
- };
213
+ _this6.loaded(name, err, data);
214
+ });
215
+ };
215
216
 
216
- ;
217
+ ;
217
218
 
218
- toLoad.toLoad.forEach(function (name) {
219
- readOne.call(_this5, name);
220
- });
219
+ toLoad.toLoad.forEach(function (name) {
220
+ readOne.call(_this5, name);
221
+ });
222
+ })();
221
223
  }
222
224
  };
223
225
 
@@ -255,29 +257,31 @@ var Connector = function (_EventEmitter) {
255
257
 
256
258
  // load one by one
257
259
  else {
258
- var readOne = function readOne(name) {
259
- var _this8 = this;
260
+ (function () {
261
+ var readOne = function readOne(name) {
262
+ var _this8 = this;
260
263
 
261
- var _name$split7 = name.split('|'),
262
- _name$split8 = _slicedToArray(_name$split7, 2),
263
- lng = _name$split8[0],
264
- ns = _name$split8[1];
264
+ var _name$split7 = name.split('|'),
265
+ _name$split8 = _slicedToArray(_name$split7, 2),
266
+ lng = _name$split8[0],
267
+ ns = _name$split8[1];
265
268
 
266
- this.read(lng, ns, 'read', null, null, function (err, data) {
267
- if (err) _this8.logger.warn('reloading namespace ' + ns + ' for language ' + lng + ' failed', err);
268
- if (!err && data) _this8.logger.log('reloaded namespace ' + ns + ' for language ' + lng, data);
269
+ this.read(lng, ns, 'read', null, null, function (err, data) {
270
+ if (err) _this8.logger.warn('reloading namespace ' + ns + ' for language ' + lng + ' failed', err);
271
+ if (!err && data) _this8.logger.log('reloaded namespace ' + ns + ' for language ' + lng, data);
269
272
 
270
- _this8.loaded(name, err, data);
271
- });
272
- };
273
+ _this8.loaded(name, err, data);
274
+ });
275
+ };
273
276
 
274
- ;
277
+ ;
275
278
 
276
- languages.forEach(function (l) {
277
- namespaces.forEach(function (n) {
278
- readOne.call(_this7, l + '|' + n);
279
+ languages.forEach(function (l) {
280
+ namespaces.forEach(function (n) {
281
+ readOne.call(_this7, l + '|' + n);
282
+ });
279
283
  });
280
- });
284
+ })();
281
285
  }
282
286
  };
283
287
 
@@ -1,3 +1,5 @@
1
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
2
+
1
3
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
4
 
3
5
  import baseLogger from './logger';
@@ -121,40 +123,58 @@ var PluralResolver = function () {
121
123
  var rule = this.getRule(code);
122
124
 
123
125
  if (rule) {
124
- if (rule.numbers.length === 1) return ''; // only singular
125
-
126
- var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
127
- var suffix = rule.numbers[idx];
128
-
129
- // special treatment for lngs only having singular and plural
130
- if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
131
- if (suffix === 2) {
132
- suffix = 'plural';
133
- } else if (suffix === 1) {
134
- suffix = '';
126
+ var _ret = function () {
127
+ if (rule.numbers.length === 1) return {
128
+ v: ''
129
+ }; // only singular
130
+
131
+ var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
132
+ var suffix = rule.numbers[idx];
133
+
134
+ // special treatment for lngs only having singular and plural
135
+ if (_this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
136
+ if (suffix === 2) {
137
+ suffix = 'plural';
138
+ } else if (suffix === 1) {
139
+ suffix = '';
140
+ }
135
141
  }
136
- }
137
-
138
- var returnSuffix = function returnSuffix() {
139
- return _this.options.prepend && suffix.toString() ? _this.options.prepend + suffix.toString() : suffix.toString();
140
- };
141
142
 
142
- // COMPATIBILITY JSON
143
- // v1
144
- if (this.options.compatibilityJSON === 'v1') {
145
- if (suffix === 1) return '';
146
- if (typeof suffix === 'number') return '_plural_' + suffix.toString();
147
- return returnSuffix();
148
- }
149
- // v2
150
- else if (this.options.compatibilityJSON === 'v2' || rule.numbers.length === 2 && rule.numbers[0] === 1) {
151
- return returnSuffix();
143
+ var returnSuffix = function returnSuffix() {
144
+ return _this.options.prepend && suffix.toString() ? _this.options.prepend + suffix.toString() : suffix.toString();
145
+ };
146
+
147
+ // COMPATIBILITY JSON
148
+ // v1
149
+ if (_this.options.compatibilityJSON === 'v1') {
150
+ if (suffix === 1) return {
151
+ v: ''
152
+ };
153
+ if (typeof suffix === 'number') return {
154
+ v: '_plural_' + suffix.toString()
155
+ };
156
+ return {
157
+ v: returnSuffix()
158
+ };
152
159
  }
153
- // v3 - gettext index
154
- else if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
155
- return returnSuffix();
160
+ // v2
161
+ else if (_this.options.compatibilityJSON === 'v2' || rule.numbers.length === 2 && rule.numbers[0] === 1) {
162
+ return {
163
+ v: returnSuffix()
164
+ };
156
165
  }
157
- return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
166
+ // v3 - gettext index
167
+ else if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
168
+ return {
169
+ v: returnSuffix()
170
+ };
171
+ }
172
+ return {
173
+ v: _this.options.prepend && idx.toString() ? _this.options.prepend + idx.toString() : idx.toString()
174
+ };
175
+ }();
176
+
177
+ if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
158
178
  } else {
159
179
  this.logger.warn('no plural rule found for: ' + code);
160
180
  return '';
@@ -123,7 +123,9 @@ var Translator = function (_EventEmitter) {
123
123
  var copy = resType === '[object Array]' ? [] : {}; // apply child translation on a copy
124
124
 
125
125
  for (var m in res) {
126
- copy[m] = this.translate('' + key + keySeparator + m, _extends({}, options, { joinArrays: false, ns: namespaces }));
126
+ if (res.hasOwnProperty(m)) {
127
+ copy[m] = this.translate('' + key + keySeparator + m, _extends({}, options, { joinArrays: false, ns: namespaces }));
128
+ }
127
129
  }
128
130
  res = copy;
129
131
  }
@@ -13,6 +13,7 @@ export function get() {
13
13
  load: 'all', // | currentOnly | languageOnly
14
14
  preload: false, // array with preload languages
15
15
 
16
+ simplifyPluralSuffix: true,
16
17
  keySeparator: '.',
17
18
  nsSeparator: ':',
18
19
  pluralSeparator: '_',