From 654bed03333b473fcc68786cc9951216cd3bab68 Mon Sep 17 00:00:00 2001 From: Khaled Mohamed Date: Thu, 29 Jun 2017 22:42:55 -0400 Subject: [PATCH] fix(translate-json): refactoring, abstracting the services list --- lib/service/google.js | 12 +----------- lib/service/yandex.js | 13 ++----------- lib/translate-json-object.js | 14 ++++++++++---- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/service/google.js b/lib/service/google.js index b1d2401..18706ad 100644 --- a/lib/service/google.js +++ b/lib/service/google.js @@ -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); diff --git a/lib/service/yandex.js b/lib/service/yandex.js index 83d3142..c857d1d 100644 --- a/lib/service/yandex.js +++ b/lib/service/yandex.js @@ -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); diff --git a/lib/translate-json-object.js b/lib/translate-json-object.js index 03ac641..c1acb3c 100644 --- a/lib/translate-json-object.js +++ b/lib/translate-json-object.js @@ -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 @@ -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); }); }