Skip to content

Commit

Permalink
fix(translate-json): refactoring, abstracting the services list
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledMohamedP committed Jul 1, 2017
1 parent 72be8d2 commit 654bed0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
12 changes: 1 addition & 11 deletions lib/service/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@ function init(setting) {
translateService = require('google-translate')(setting.googleApiKey);
}

function translateObject(language, key, destObj) {
// Find the keys of the current object that has string value (str is translatable)
var objKeys = _.pickBy(destObj[key], _.isString);
var keysArray = _.keys(objKeys);
var valuesArray = _.concat(_.values(objKeys));
function translateObject(language, key, destObj, keysArray, valuesArray) {

return new Promise(function (resolve, reject) {
// If empty array don't call google
if (valuesArray.length === 0) {
resolve(destObj);
return;
}

translateService.translate(valuesArray, language, function (err, res) {
if (err || !res) {
reject(err);
Expand Down
13 changes: 2 additions & 11 deletions lib/service/yandex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@ function init(setting) {
translateService = require('yandex-translate')(setting.yandexApiKey);
}

function translateObject(language, key, destObj) {
// Find the keys of the current object that has string value (str is translatable)
var objKeys = _.pickBy(destObj[key], _.isString);
var keysArray = _.keys(objKeys);
var valuesArray = _.concat(_.values(objKeys));

function translateObject(language, key, destObj, keysArray, valuesArray) {
return new Promise(function (resolve, reject) {
var valuesStr = valuesArray.join(hashSimple);
if (valuesStr === '') {
// there is nothing to translate, so don't ask Yandex (Yandex will respond with an error)
resolve(destObj);
return;
}

translateService.translate(valuesStr, {to: language}, function (err, res) {
if (err || res.code !== 200) {
reject(err || res);
Expand Down
14 changes: 10 additions & 4 deletions lib/translate-json-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ function TranslateJSONObject() {
dest[key] = {};
_.merge(dest[key], value);
recurisveTranslateObject(dest[key], value);
promises.push(translateSrv.object(language, key, dest));

// Find the keys of the current object that has string value (str is translatable)
var objKeys = _.pickBy(dest[key], _.isString);
var keysArray = _.keys(objKeys);
var valuesArray = _.concat(_.values(objKeys));

if (valuesArray.length !== 0) {
promises.push(translateSrv.object(language, key, dest, keysArray, valuesArray));
}
}

// Recursivly loop through an object
Expand All @@ -104,9 +112,7 @@ function TranslateJSONObject() {
return new Promise(function (resolve, reject) {
Promise.all(promises).then(function () {
resolve(destObj.data);
}).catch(function (err) {
reject(err);
});
}).catch(reject);
});
}

Expand Down

0 comments on commit 654bed0

Please sign in to comment.