Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Dec 14, 2018
1 parent b232a7c commit bc58338
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,13 @@
### 13.0.0

- pass options to missingInterpolationHandler [1146](https://github.com/i18next/i18next/pull/1146)
- refactor non valid keys handling [1143](https://github.com/i18next/i18next/pull/1143)

BREAKING:

- adds typescript definitions directly into this repo - no longer need to grab them from definitlytyped [1142](https://github.com/i18next/i18next/pull/1142)
- promise API added for all functions providing a callback [https://github.com/i18next/i18next/pull/1130](1130) -> means those will now return a Promise and not this -> so you can't chain eg. i18next.init().on() anylonger as init returns a Promise

### 12.1.0

- adds partialBundledLanguages flag in init options [1136](https://github.com/i18next/i18next/pull/1136)
Expand Down
81 changes: 67 additions & 14 deletions i18next.js
Expand Up @@ -295,6 +295,22 @@ var EventEmitter = function () {
return EventEmitter;
}();

// http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/
function defer() {
var res = void 0;
var rej = void 0;

var promise = new Promise(function (resolve, reject) {
res = resolve;
rej = reject;
});

promise.resolve = res;
promise.reject = rej;

return promise;
}

function makeString(object) {
if (object == null) return '';
/* eslint prefer-template: 0 */
Expand Down Expand Up @@ -618,9 +634,8 @@ var Translator = function (_EventEmitter) {
if (!options) options = {};

// non valid keys handling
if (keys === undefined || keys === null || keys === '') return '';
if (typeof keys === 'number') keys = String(keys);
if (typeof keys === 'string') keys = [keys];
if (keys === undefined || keys === null) return '';
if (!Array.isArray(keys)) keys = [String(keys)];

// separators
var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
Expand Down Expand Up @@ -1275,7 +1290,7 @@ var Interpolator = function () {
value = handleFormat(match[1].trim());
if (value === undefined) {
if (typeof missingInterpolationHandler === 'function') {
var temp = missingInterpolationHandler(str, match);
var temp = missingInterpolationHandler(str, match, options);
value = typeof temp === 'string' ? temp : '';
} else {
this.logger.warn('missed to pass in variable ' + match[1] + ' for interpolating ' + str);
Expand Down Expand Up @@ -1679,10 +1694,13 @@ var I18n = function (_EventEmitter) {
_this.modules = { external: [] };

if (callback && !_this.isInitialized && !options.isClone) {
var _ret;

// https://github.com/i18next/i18next/issues/879
if (!_this.options.initImmediate) return _ret = _this.init(options, callback), possibleConstructorReturn(_this, _ret);
if (!_this.options.initImmediate) {
var _ret;

_this.init(options, callback);
return _ret = _this, possibleConstructorReturn(_this, _ret);
}
setTimeout(function () {
_this.init(options, callback);
}, 0);
Expand Down Expand Up @@ -1774,12 +1792,15 @@ var I18n = function (_EventEmitter) {
};
});

var deferred = defer();

var load = function load() {
_this2.changeLanguage(_this2.options.lng, function (err, t) {
_this2.isInitialized = true;
_this2.logger.log('initialized', _this2.options);
_this2.emit('initialized', _this2.options);

deferred.resolve(t); // not rejecting on err (as err is only a loading translation failed warning)
callback(err, t);
});
};
Expand All @@ -1790,7 +1811,7 @@ var I18n = function (_EventEmitter) {
setTimeout(load, 0);
}

return this;
return deferred;
};

/* eslint consistent-return: 0 */
Expand Down Expand Up @@ -1837,10 +1858,15 @@ var I18n = function (_EventEmitter) {
};

I18n.prototype.reloadResources = function reloadResources(lngs, ns, callback) {
var deferred = defer();
if (!lngs) lngs = this.languages;
if (!ns) ns = this.options.ns;
if (!callback) callback = function callback() {};
this.services.backendConnector.reload(lngs, ns, callback);
if (!callback) callback = noop;
this.services.backendConnector.reload(lngs, ns, function () {
deferred.resolve();
callback(null);
});
return deferred;
};

I18n.prototype.use = function use(module) {
Expand Down Expand Up @@ -1874,6 +1900,8 @@ var I18n = function (_EventEmitter) {
I18n.prototype.changeLanguage = function changeLanguage(lng, callback) {
var _this4 = this;

var deferred = defer();

var done = function done(err, l) {
_this4.translator.changeLanguage(l);

Expand All @@ -1882,6 +1910,9 @@ var I18n = function (_EventEmitter) {
_this4.logger.log('languageChanged', l);
}

deferred.resolve(function () {
return _this4.t.apply(_this4, arguments);
});
if (callback) callback(err, function () {
return _this4.t.apply(_this4, arguments);
});
Expand All @@ -1908,6 +1939,8 @@ var I18n = function (_EventEmitter) {
} else {
setLng(lng);
}

return deferred;
};

I18n.prototype.getFixedT = function getFixedT(lng, ns) {
Expand Down Expand Up @@ -1956,28 +1989,48 @@ var I18n = function (_EventEmitter) {
I18n.prototype.loadNamespaces = function loadNamespaces(ns, callback) {
var _this6 = this;

if (!this.options.ns) return callback && callback();
var deferred = defer();

if (!this.options.ns) {
callback && callback();
return Promise.resolve();
}
if (typeof ns === 'string') ns = [ns];

ns.forEach(function (n) {
if (_this6.options.ns.indexOf(n) < 0) _this6.options.ns.push(n);
});

this.loadResources(callback);
this.loadResources(function (err) {
deferred.resolve();
if (callback) callback(err);
});

return deferred;
};

I18n.prototype.loadLanguages = function loadLanguages(lngs, callback) {
var deferred = defer();

if (typeof lngs === 'string') lngs = [lngs];
var preloaded = this.options.preload || [];

var newLngs = lngs.filter(function (lng) {
return preloaded.indexOf(lng) < 0;
});
// Exit early if all given languages are already preloaded
if (!newLngs.length) return callback();
if (!newLngs.length) {
callback();
return Promise.resolve();
}

this.options.preload = preloaded.concat(newLngs);
this.loadResources(callback);
this.loadResources(function (err) {
deferred.resolve();
if (callback) callback(err);
});

return deferred;
};

I18n.prototype.dir = function dir(lng) {
Expand Down
4 changes: 2 additions & 2 deletions i18next.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
@@ -1,8 +1,9 @@
{
"name": "i18next",
"version": "12.1.0",
"version": "13.0.0",
"description": "i18next internationalization framework",
"main": "./index.js",
"types": "index.d.ts",
"jsnext:main": "dist/es/index.js",
"module": "dist/es/index.js",
"keywords": [
Expand Down

0 comments on commit bc58338

Please sign in to comment.