Skip to content

Commit

Permalink
fix(i18n): use a promise to wait for i18next to be ready before updat…
Browse files Browse the repository at this point in the history
…ing value
  • Loading branch information
Jenselme committed Apr 3, 2016
1 parent 5640091 commit 2b69cfe
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 127 deletions.
1 change: 1 addition & 0 deletions dist/amd/aurelia-i18n.d.ts
Expand Up @@ -29,6 +29,7 @@ declare module 'aurelia-i18n' {
/*eslint no-cond-assign: 0*/
export class I18N {
globalVars: any;
i18nextDefered: any;
constructor(ea: any, signaler: any);
setup(options?: any): any;
setLocale(locale: any): any;
Expand Down
43 changes: 30 additions & 13 deletions dist/amd/i18n.js
Expand Up @@ -22,18 +22,27 @@ define(['exports', 'i18next'], function (exports, _i18next) {

var I18N = exports.I18N = function () {
function I18N(ea, signaler) {
var _this = this;

_classCallCheck(this, I18N);

this.globalVars = {};
this.i18nextDefered = {
resolve: null,
promise: null
};

this.i18next = _i18next2.default;
this.ea = ea;
this.Intl = window.Intl;
this.signaler = signaler;
this.i18nextDefered.promise = new Promise(function (resolve) {
return _this.i18nextDefered.resolve = resolve;
});
}

I18N.prototype.setup = function setup(options) {
var _this = this;
var _this2 = this;

var defaultOptions = {
compatibilityAPI: 'v1',
Expand All @@ -44,25 +53,25 @@ define(['exports', 'i18next'], function (exports, _i18next) {
debug: false
};

return new Promise(function (resolve) {
_i18next2.default.init(options || defaultOptions, function (err, t) {
if (_i18next2.default.options.attributes instanceof String) {
_i18next2.default.options.attributes = [_i18next2.default.options.attributes];
}
_i18next2.default.init(options || defaultOptions, function (err, t) {
if (_i18next2.default.options.attributes instanceof String) {
_i18next2.default.options.attributes = [_i18next2.default.options.attributes];
}

resolve(_this.i18next);
});
_this2.i18nextDefered.resolve(_this2.i18next);
});

return this.i18nextDefered.promise;
};

I18N.prototype.setLocale = function setLocale(locale) {
var _this2 = this;
var _this3 = this;

return new Promise(function (resolve) {
var oldLocale = _this2.getLocale();
_this2.i18next.changeLanguage(locale, function (err, tr) {
_this2.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this2.signaler.signal('aurelia-translation-signal');
var oldLocale = _this3.getLocale();
_this3.i18next.changeLanguage(locale, function (err, tr) {
_this3.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this3.signaler.signal('aurelia-translation-signal');
resolve(tr);
});
});
Expand Down Expand Up @@ -136,6 +145,14 @@ define(['exports', 'i18next'], function (exports, _i18next) {
};

I18N.prototype.updateValue = function updateValue(node, value, params) {
var _this4 = this;

this.i18nextDefered.promise.then(function () {
return _this4._updateValue(node, value, params);
});
};

I18N.prototype._updateValue = function _updateValue(node, value, params) {
if (value === null || value === undefined) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions dist/aurelia-i18n.d.ts
Expand Up @@ -29,6 +29,7 @@ declare module 'aurelia-i18n' {
/*eslint no-cond-assign: 0*/
export class I18N {
globalVars: any;
i18nextDefered: any;
constructor(ea: any, signaler: any);
setup(options?: any): any;
setLocale(locale: any): any;
Expand Down
25 changes: 17 additions & 8 deletions dist/aurelia-i18n.js
Expand Up @@ -293,12 +293,17 @@ export class LazyOptional {
export class I18N {

globalVars = {};
i18nextDefered = {
resolve: null,
promise: null
};

constructor(ea, signaler) {
this.i18next = i18next;
this.ea = ea;
this.Intl = window.Intl;
this.signaler = signaler;
this.i18nextDefered.promise = new Promise((resolve) => this.i18nextDefered.resolve = resolve);
}

setup(options?) {
Expand All @@ -311,16 +316,16 @@ export class I18N {
debug: false
};

return new Promise((resolve) => {
i18next.init(options || defaultOptions, (err, t) => {
//make sure attributes is an array in case a string was provided
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}
i18next.init(options || defaultOptions, (err, t) => {
//make sure attributes is an array in case a string was provided
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}

resolve(this.i18next);
});
this.i18nextDefered.resolve(this.i18next);
});

return this.i18nextDefered.promise;
}

setLocale(locale) {
Expand Down Expand Up @@ -419,6 +424,10 @@ export class I18N {
}

updateValue(node, value, params) {
this.i18nextDefered.promise.then(() => this._updateValue(node, value, params));
}

_updateValue(node, value, params) {
if (value === null || value === undefined) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions dist/commonjs/aurelia-i18n.d.ts
Expand Up @@ -29,6 +29,7 @@ declare module 'aurelia-i18n' {
/*eslint no-cond-assign: 0*/
export class I18N {
globalVars: any;
i18nextDefered: any;
constructor(ea: any, signaler: any);
setup(options?: any): any;
setLocale(locale: any): any;
Expand Down
43 changes: 30 additions & 13 deletions dist/commonjs/i18n.js
Expand Up @@ -15,18 +15,27 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var I18N = exports.I18N = function () {
function I18N(ea, signaler) {
var _this = this;

_classCallCheck(this, I18N);

this.globalVars = {};
this.i18nextDefered = {
resolve: null,
promise: null
};

this.i18next = _i18next2.default;
this.ea = ea;
this.Intl = window.Intl;
this.signaler = signaler;
this.i18nextDefered.promise = new Promise(function (resolve) {
return _this.i18nextDefered.resolve = resolve;
});
}

I18N.prototype.setup = function setup(options) {
var _this = this;
var _this2 = this;

var defaultOptions = {
compatibilityAPI: 'v1',
Expand All @@ -37,25 +46,25 @@ var I18N = exports.I18N = function () {
debug: false
};

return new Promise(function (resolve) {
_i18next2.default.init(options || defaultOptions, function (err, t) {
if (_i18next2.default.options.attributes instanceof String) {
_i18next2.default.options.attributes = [_i18next2.default.options.attributes];
}
_i18next2.default.init(options || defaultOptions, function (err, t) {
if (_i18next2.default.options.attributes instanceof String) {
_i18next2.default.options.attributes = [_i18next2.default.options.attributes];
}

resolve(_this.i18next);
});
_this2.i18nextDefered.resolve(_this2.i18next);
});

return this.i18nextDefered.promise;
};

I18N.prototype.setLocale = function setLocale(locale) {
var _this2 = this;
var _this3 = this;

return new Promise(function (resolve) {
var oldLocale = _this2.getLocale();
_this2.i18next.changeLanguage(locale, function (err, tr) {
_this2.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this2.signaler.signal('aurelia-translation-signal');
var oldLocale = _this3.getLocale();
_this3.i18next.changeLanguage(locale, function (err, tr) {
_this3.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this3.signaler.signal('aurelia-translation-signal');
resolve(tr);
});
});
Expand Down Expand Up @@ -129,6 +138,14 @@ var I18N = exports.I18N = function () {
};

I18N.prototype.updateValue = function updateValue(node, value, params) {
var _this4 = this;

this.i18nextDefered.promise.then(function () {
return _this4._updateValue(node, value, params);
});
};

I18N.prototype._updateValue = function _updateValue(node, value, params) {
if (value === null || value === undefined) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions dist/es2015/aurelia-i18n.d.ts
Expand Up @@ -29,6 +29,7 @@ declare module 'aurelia-i18n' {
/*eslint no-cond-assign: 0*/
export class I18N {
globalVars: any;
i18nextDefered: any;
constructor(ea: any, signaler: any);
setup(options?: any): any;
setLocale(locale: any): any;
Expand Down
23 changes: 16 additions & 7 deletions dist/es2015/i18n.js
Expand Up @@ -5,11 +5,16 @@ export let I18N = class I18N {

constructor(ea, signaler) {
this.globalVars = {};
this.i18nextDefered = {
resolve: null,
promise: null
};

this.i18next = i18next;
this.ea = ea;
this.Intl = window.Intl;
this.signaler = signaler;
this.i18nextDefered.promise = new Promise(resolve => this.i18nextDefered.resolve = resolve);
}

setup(options) {
Expand All @@ -22,15 +27,15 @@ export let I18N = class I18N {
debug: false
};

return new Promise(resolve => {
i18next.init(options || defaultOptions, (err, t) => {
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}
i18next.init(options || defaultOptions, (err, t) => {
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}

resolve(this.i18next);
});
this.i18nextDefered.resolve(this.i18next);
});

return this.i18nextDefered.promise;
}

setLocale(locale) {
Expand Down Expand Up @@ -111,6 +116,10 @@ export let I18N = class I18N {
}

updateValue(node, value, params) {
this.i18nextDefered.promise.then(() => this._updateValue(node, value, params));
}

_updateValue(node, value, params) {
if (value === null || value === undefined) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions dist/system/aurelia-i18n.d.ts
Expand Up @@ -29,6 +29,7 @@ declare module 'aurelia-i18n' {
/*eslint no-cond-assign: 0*/
export class I18N {
globalVars: any;
i18nextDefered: any;
constructor(ea: any, signaler: any);
setup(options?: any): any;
setLocale(locale: any): any;
Expand Down
43 changes: 30 additions & 13 deletions dist/system/i18n.js
Expand Up @@ -16,18 +16,27 @@ System.register(['i18next'], function (_export, _context) {
execute: function () {
_export('I18N', I18N = function () {
function I18N(ea, signaler) {
var _this = this;

_classCallCheck(this, I18N);

this.globalVars = {};
this.i18nextDefered = {
resolve: null,
promise: null
};

this.i18next = i18next;
this.ea = ea;
this.Intl = window.Intl;
this.signaler = signaler;
this.i18nextDefered.promise = new Promise(function (resolve) {
return _this.i18nextDefered.resolve = resolve;
});
}

I18N.prototype.setup = function setup(options) {
var _this = this;
var _this2 = this;

var defaultOptions = {
compatibilityAPI: 'v1',
Expand All @@ -38,25 +47,25 @@ System.register(['i18next'], function (_export, _context) {
debug: false
};

return new Promise(function (resolve) {
i18next.init(options || defaultOptions, function (err, t) {
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}
i18next.init(options || defaultOptions, function (err, t) {
if (i18next.options.attributes instanceof String) {
i18next.options.attributes = [i18next.options.attributes];
}

resolve(_this.i18next);
});
_this2.i18nextDefered.resolve(_this2.i18next);
});

return this.i18nextDefered.promise;
};

I18N.prototype.setLocale = function setLocale(locale) {
var _this2 = this;
var _this3 = this;

return new Promise(function (resolve) {
var oldLocale = _this2.getLocale();
_this2.i18next.changeLanguage(locale, function (err, tr) {
_this2.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this2.signaler.signal('aurelia-translation-signal');
var oldLocale = _this3.getLocale();
_this3.i18next.changeLanguage(locale, function (err, tr) {
_this3.ea.publish('i18n:locale:changed', { oldValue: oldLocale, newValue: locale });
_this3.signaler.signal('aurelia-translation-signal');
resolve(tr);
});
});
Expand Down Expand Up @@ -130,6 +139,14 @@ System.register(['i18next'], function (_export, _context) {
};

I18N.prototype.updateValue = function updateValue(node, value, params) {
var _this4 = this;

this.i18nextDefered.promise.then(function () {
return _this4._updateValue(node, value, params);
});
};

I18N.prototype._updateValue = function _updateValue(node, value, params) {
if (value === null || value === undefined) {
return;
}
Expand Down

0 comments on commit 2b69cfe

Please sign in to comment.