Skip to content

Commit

Permalink
fix(translation): fix an issue with translating multiple languages async
Browse files Browse the repository at this point in the history
35
  • Loading branch information
KhaledMohamedP committed Sep 28, 2020
1 parent 00e93d7 commit 22560b7
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 54 deletions.
27 changes: 12 additions & 15 deletions lib/__tests__/google.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ const init = jest.fn(TJO.init);
const translate = jest.fn(TJO.translate);

describe('TJO Google Translate Service', () => {
it('Should transalte object: sucess', () => {
it('Should translate object: success', () => {
init({googleApiKey: 'apiToken'});
return translate(mock.dataObject, 'es')
.then(e => {
expect(e).toEqual(mock.transaltedES);
});
return translate(mock.dataObject, 'es').then(e => {
expect(e).toEqual(mock.translatedES);
});
});

it('Should not transalte number: success', () => {
it('Should not translate number: success', () => {
init({googleApiKey: 'apiToken'});
return translate({num: 33}, 'es')
.then(e => {
expect(e).toEqual({num: 33});
});
return translate({num: 33}, 'es').then(e => {
expect(e).toEqual({num: 33});
});
});

it('Should not boolean: success', () => {
it('Should not translate boolean: success', () => {
init({googleApiKey: 'apiToken'});
return translate({bool: true}, 'es')
.then(e => {
expect(e).toEqual({bool: true});
});
return translate({bool: true}, 'es').then(e => {
expect(e).toEqual({bool: true});
});
});
});
45 changes: 27 additions & 18 deletions lib/__tests__/yandex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,39 @@ const init = jest.fn(TJO.init);
const translate = jest.fn(TJO.translate);

describe('TJO Yandex Translate Service', () => {
it('Should transalte object: success', () => {
it('Should translate object: success', () => {
init({yandexApiKey: 'apiToken'});
return translate(mock.dataObject, 'es')
.then(e => {
expect(e).toBeDefined();
expect(e).toEqual(mock.transaltedES);
});
return translate(mock.dataObject, 'es').then(e => {
expect(e).toBeDefined();
expect(e).toEqual(mock.translatedES);
});
});

it('Should not transalte number: success', () => {
it('Should not translate number: success', () => {
init({yandexApiKey: 'apiToken'});
return translate({num: 33}, 'es')
.then(e => {
expect(e).toBeDefined();
expect(e).toEqual({num: 33});
});
return translate({num: 33}, 'es').then(e => {
expect(e).toBeDefined();
expect(e).toEqual({num: 33});
});
});

it('Should not boolean: success', () => {
it('Should not translate boolean: success', () => {
init({yandexApiKey: 'apiToken'});
return translate({bool: true}, 'es')
.then(e => {
expect(e).toBeDefined();
expect(e).toEqual({bool: true});
});
return translate({bool: true}, 'es').then(e => {
expect(e).toBeDefined();
expect(e).toEqual({bool: true});
});
});

it('Should translate multiple languages async', () => {
init({yandexApiKey: 'apiToken'});
const languages = ['es', 'ar'];
const all = Promise.all(
languages.map(lang => translate(mock.dataObject, lang))
);

all.then(data => {
expect(data).toEqual([mock.translatedES, mock.translatedAR]);
});
});
});
37 changes: 34 additions & 3 deletions lib/fixture/data.fixture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
const dataObject = {name: 'name', some: [{name: 'j'}], addressField: ['city', 'state', 'zipdecode'], bool: true, num: 33, badBool: false, nully: null, undefin: undefined};
const transaltedES = {name: 'name-es', some: [{name: 'j-es'}], addressField: ['city-es', 'state-es', 'zipdecode-es'], bool: true, num: 33, badBool: false, nully: null, undefin: undefined};
const dataObject = {
name: 'name',
some: [{name: 'j'}],
addressField: ['city', 'state', 'zipdecode'],
bool: true,
num: 33,
badBool: false,
nully: null,
undefin: undefined
};

const translatedES = {
name: 'name-es',
some: [{name: 'j-es'}],
addressField: ['city-es', 'state-es', 'zipdecode-es'],
bool: true,
num: 33,
badBool: false,
nully: null,
undefin: undefined
};

const translatedAR = {
name: 'name-ar',
some: [{name: 'j-ar'}],
addressField: ['city-ar', 'state-ar', 'zipdecode-ar'],
bool: true,
num: 33,
badBool: false,
nully: null,
undefin: undefined
};

module.exports = {
dataObject,
transaltedES
translatedES,
translatedAR
};
48 changes: 32 additions & 16 deletions lib/translate-json-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ function TranslateJSONObject() {
// Set the current available service for translation e.g. google, bing, yandex etc..
var translateSrv;
var setting;
// The list of promises that should be resolve prior to returning the full `Object translation`
var promises = [];
var destObj = {};

var serviceType;

/**
Expand Down Expand Up @@ -49,16 +47,26 @@ function TranslateJSONObject() {
* @return {Promise} It returns a promise with the translated object
*/
function translate(srcObj, language) {
var promises = [];
var destObj = {};

if (!setting.googleApiKey && !setting.yandexApiKey) {
return Promise.reject(constant.ERROR.MISSING_TOKEN);
}

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

if (!isValidLang(language, serviceType)) {
return Promise.reject(serviceType + ' doesn\'t support the language code you specified [' + language + '], please try another language code (ISO-639-1)');
return Promise.reject(
serviceType +
' doesn\'t support the language code you specified [' +
language +
'], please try another language code (ISO-639-1)'
);
}

var ARRAY_ROOT_TYPE = _.isArray(srcObj);
Expand Down Expand Up @@ -114,23 +122,31 @@ function TranslateJSONObject() {
var valuesArray = _.concat(_.values(objKeys));

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

// Recursivly loop through an object
recurisveTranslateObject(destObj, {
ROOT: srcObj
}, language);
recurisveTranslateObject(
destObj,
{
ROOT: srcObj
},
language
);

return new Promise(function (resolve, reject) {
Promise.all(promises).then(function () {
if (ARRAY_ROOT_TYPE) {
resolve(destObj.ROOT.arrayType);
} else {
resolve(destObj.ROOT);
}
}).catch(reject);
Promise.all(promises)
.then(function () {
if (ARRAY_ROOT_TYPE) {
resolve(destObj.ROOT.arrayType);
} else {
resolve(destObj.ROOT);
}
})
.catch(reject);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/valid-lang.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var constant = require('./constant');

var google = ['af', 'am', 'ar', 'az', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'co', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'fy', 'ga', 'gd', 'gl', 'gu', 'ha', 'haw', 'hi', 'hmn', 'hr', 'ht', 'hu', 'hy', 'id', 'ig', 'is', 'it', 'iw', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'ma', 'mg', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'ny', 'pa', 'pl', 'ps', 'pt', 'ro', 'ru', 'sd', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', 'zh-CN', 'zh-TW', 'zu'];
var yandex = ['af', 'am', 'ar', 'az', 'ba', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'ga', 'gd', 'gl', 'gu', 'he', 'hi', 'hr', 'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'mg', 'mhr', 'mi', 'mk', 'ml', 'mn', 'mr', 'mrj', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'pa', 'pap', 'pl', 'pt', 'ro', 'ru', 'si', 'sk', 'sl', 'sq', 'sr', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'tt', 'udm', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'zh'];
var google = ['af', 'am', 'ar', 'az', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'co', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'fy', 'ga', 'gd', 'gl', 'gu', 'ha', 'haw', 'hi', 'hmn', 'hr', 'ht', 'hu', 'hy', 'id', 'ig', 'is', 'it', 'iw', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'ma', 'mg', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'ny', 'pl', 'ps', 'pt', 'ro', 'ru', 'sd', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', 'zh-CN', 'zh-TW', 'zu'];
var yandex = ['af', 'am', 'ar', 'az', 'ba', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'cs', 'cv', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'ga', 'gd', 'gl', 'gu', 'he', 'hi', 'hr', 'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'mg', 'mhr', 'mi', 'mk', 'ml', 'mn', 'mr', 'mrj', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'pa', 'pap', 'pl', 'pt', 'ro', 'ru', 'sah', 'si', 'sk', 'sl', 'sq', 'sr', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'tt', 'udm', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'zh'];

module.exports = function (lang, service) {
switch (service) {
Expand Down

0 comments on commit 22560b7

Please sign in to comment.