Skip to content

Commit

Permalink
fix(google-translate): Google failed when we translate empty string a…
Browse files Browse the repository at this point in the history
…rray, skip empty string or arra
  • Loading branch information
KhaledMohamedP committed Jul 1, 2017
1 parent fb97bb0 commit 7e0ea9d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion __mocks__/yandex-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = function () {
translate: (value, lang, fn) => {
if (value) {
var translatedList = value + '-' + lang.to;
fn(null, {text: [translatedList]});
fn(null, {code: 200, text: [translatedList]});
} else {
fn({error: 'provide value'});
}
Expand Down
8 changes: 8 additions & 0 deletions __tests__/api/general.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe('Translate Module TJO: ', () => {
});
});

it('Should fail to translate because of a missing language param: fail', () => {
init({yandexApiKey: 'apiToken'});
return translate(mockDataObject)
.catch(err => {
expect(err).toBeDefined();
});
});

it('should TJO.translate fails, becuase of missing google/yandex token', () => {
init();
return translate(mockDataObject, 'ar')
Expand Down
16 changes: 14 additions & 2 deletions lib/service/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ function translateObject(language, key, destObj) {
var valuesArray = _.concat(_.values(objKeys));

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('Translation service failed', err);
reject(err);
} else {
// Google-translate doesn't return an array if we only send a single string to translate
res = _.concat(res);
Expand All @@ -32,9 +38,15 @@ function translateObject(language, key, destObj) {

function translateString(language, key, destObj, valueStr) {
return new Promise(function (resolve, reject) {
// If empty string don't call google
if (valueStr === '') {
resolve(destObj);
return;
}

translateService.translate(valueStr, language, function (err, res) {
if (err || !res) {
reject('Google translation service failed', err);
reject(err);
} else {
destObj[key] = res.translatedText;
resolve(destObj);
Expand Down
8 changes: 4 additions & 4 deletions lib/service/yandex.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function translateObject(language, key, destObj) {
return;
}
translateService.translate(valuesStr, {to: language}, function (err, res) {
if (err || !res) {
reject('Yandex translation service failed', err);
if (err || res.code !== 200) {
reject(err || res);
} else {
// yandex return an array string and so we need split back to the normal vlues
var result = res.text[0].split(hashSimple);
Expand All @@ -46,8 +46,8 @@ function translateString(language, key, destObj, valueStr) {
}

translateService.translate(valueStr, {to: language}, function (err, res) {
if (err || !res) {
reject('Yandex translation service failed', err);
if (err || res.code !== 200) {
reject(err || res);
} else {
// yandex return an array string and so we need split back to the normal vlues
destObj[key] = res.text[0];
Expand Down
6 changes: 5 additions & 1 deletion lib/translate-json-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function TranslateJSONObject() {
);
}

if (_.isString(language)) {
return Promise.reject('Please provide a language param [type String] e.g. translate(obj, es)');
}

function recurisveTranslateObject(destObj, srcObj) {
// Loop through the entire object collection
_.forEach(srcObj, loopHandler);
Expand Down Expand Up @@ -101,7 +105,7 @@ function TranslateJSONObject() {
Promise.all(promises).then(function () {
resolve(destObj.data);
}).catch(function (err) {
reject('Service failed', err);
reject(err);
});
});
}
Expand Down

0 comments on commit 7e0ea9d

Please sign in to comment.