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;
}
getLanguage() {
return Promise.resolve(this.mozL10n.getLanguage());
async getLanguage() {
return this.mozL10n.getLanguage();
}
getDirection() {
return Promise.resolve(this.mozL10n.getDirection());
async getDirection() {
return this.mozL10n.getDirection();
}
get(property, args, fallback) {
return Promise.resolve(this.mozL10n.get(property, args, fallback));
async get(property, args, fallback) {
return this.mozL10n.get(property, args, fallback);
}
translate(element) {
async translate(element) {
this.mozL10n.translate(element);
return Promise.resolve();
}
}

View File

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

View File

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

View File

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