Refactor the IL10n implementations to utilize async methods rather than manually returning Promises

This changes the methods signatures of `GenericL10n`, `MozL10n`, and `NullL10n`.
This commit is contained in:
Jonas Jenwald 2018-07-30 16:28:39 +02:00
parent af89ec271d
commit b0fa02e845
4 changed files with 30 additions and 37 deletions

View File

@ -145,21 +145,20 @@ class MozL10n {
this.mozL10n = mozL10n; this.mozL10n = mozL10n;
} }
getLanguage() { async getLanguage() {
return Promise.resolve(this.mozL10n.getLanguage()); return this.mozL10n.getLanguage();
} }
getDirection() { async getDirection() {
return Promise.resolve(this.mozL10n.getDirection()); return this.mozL10n.getDirection();
} }
get(property, args, fallback) { async get(property, args, fallback) {
return Promise.resolve(this.mozL10n.get(property, args, fallback)); return this.mozL10n.get(property, args, fallback);
} }
translate(element) { async translate(element) {
this.mozL10n.translate(element); this.mozL10n.translate(element);
return Promise.resolve();
} }
} }

View File

@ -27,28 +27,24 @@ class GenericL10n {
}); });
} }
getLanguage() { async getLanguage() {
return this._ready.then((l10n) => { const l10n = await this._ready;
return l10n.getLanguage(); return l10n.getLanguage();
});
} }
getDirection() { async getDirection() {
return this._ready.then((l10n) => { const l10n = await this._ready;
return l10n.getDirection(); return l10n.getDirection();
});
} }
get(property, args, fallback) { async get(property, args, fallback) {
return this._ready.then((l10n) => { const l10n = await this._ready;
return l10n.get(property, args, fallback); return l10n.get(property, args, fallback);
});
} }
translate(element) { async translate(element) {
return this._ready.then((l10n) => { const l10n = await this._ready;
return l10n.translate(element); return l10n.translate(element);
});
} }
} }

View File

@ -163,12 +163,12 @@ class IL10n {
/** /**
* @returns {Promise<string>} - Resolves to the current locale. * @returns {Promise<string>} - Resolves to the current locale.
*/ */
getLanguage() {} async getLanguage() {}
/** /**
* @returns {Promise<string>} - Resolves to 'rtl' or 'ltr'. * @returns {Promise<string>} - Resolves to 'rtl' or 'ltr'.
*/ */
getDirection() {} async getDirection() {}
/** /**
* Translates text identified by the key and adds/formats data using the args * Translates text identified by the key and adds/formats data using the args
@ -179,12 +179,12 @@ class IL10n {
* @param {string} fallback * @param {string} fallback
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
get(key, args, fallback) { } async get(key, args, fallback) { }
/** /**
* Translates HTML element. * Translates HTML element.
* @param {HTMLElement} element * @param {HTMLElement} element
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
translate(element) { } async translate(element) { }
} }

View File

@ -56,21 +56,19 @@ function formatL10nValue(text, args) {
* @implements {IL10n} * @implements {IL10n}
*/ */
let NullL10n = { let NullL10n = {
getLanguage() { async getLanguage() {
return Promise.resolve('en-us'); return 'en-us';
}, },
getDirection() { async getDirection() {
return Promise.resolve('ltr'); return 'ltr';
}, },
get(property, args, fallback) { async get(property, args, fallback) {
return Promise.resolve(formatL10nValue(fallback, args)); return formatL10nValue(fallback, args);
}, },
translate(element) { async translate(element) { },
return Promise.resolve();
},
}; };
/** /**