Skip to content

Commit 22560b7

Browse files
fix(translation): fix an issue with translating multiple languages async
35
1 parent 00e93d7 commit 22560b7

File tree

5 files changed

+107
-54
lines changed

5 files changed

+107
-54
lines changed

lib/__tests__/google.spec.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ const init = jest.fn(TJO.init);
77
const translate = jest.fn(TJO.translate);
88

99
describe('TJO Google Translate Service', () => {
10-
it('Should transalte object: sucess', () => {
10+
it('Should translate object: success', () => {
1111
init({googleApiKey: 'apiToken'});
12-
return translate(mock.dataObject, 'es')
13-
.then(e => {
14-
expect(e).toEqual(mock.transaltedES);
15-
});
12+
return translate(mock.dataObject, 'es').then(e => {
13+
expect(e).toEqual(mock.translatedES);
14+
});
1615
});
1716

18-
it('Should not transalte number: success', () => {
17+
it('Should not translate number: success', () => {
1918
init({googleApiKey: 'apiToken'});
20-
return translate({num: 33}, 'es')
21-
.then(e => {
22-
expect(e).toEqual({num: 33});
23-
});
19+
return translate({num: 33}, 'es').then(e => {
20+
expect(e).toEqual({num: 33});
21+
});
2422
});
2523

26-
it('Should not boolean: success', () => {
24+
it('Should not translate boolean: success', () => {
2725
init({googleApiKey: 'apiToken'});
28-
return translate({bool: true}, 'es')
29-
.then(e => {
30-
expect(e).toEqual({bool: true});
31-
});
26+
return translate({bool: true}, 'es').then(e => {
27+
expect(e).toEqual({bool: true});
28+
});
3229
});
3330
});

lib/__tests__/yandex.spec.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,39 @@ const init = jest.fn(TJO.init);
77
const translate = jest.fn(TJO.translate);
88

99
describe('TJO Yandex Translate Service', () => {
10-
it('Should transalte object: success', () => {
10+
it('Should translate object: success', () => {
1111
init({yandexApiKey: 'apiToken'});
12-
return translate(mock.dataObject, 'es')
13-
.then(e => {
14-
expect(e).toBeDefined();
15-
expect(e).toEqual(mock.transaltedES);
16-
});
12+
return translate(mock.dataObject, 'es').then(e => {
13+
expect(e).toBeDefined();
14+
expect(e).toEqual(mock.translatedES);
15+
});
1716
});
1817

19-
it('Should not transalte number: success', () => {
18+
it('Should not translate number: success', () => {
2019
init({yandexApiKey: 'apiToken'});
21-
return translate({num: 33}, 'es')
22-
.then(e => {
23-
expect(e).toBeDefined();
24-
expect(e).toEqual({num: 33});
25-
});
20+
return translate({num: 33}, 'es').then(e => {
21+
expect(e).toBeDefined();
22+
expect(e).toEqual({num: 33});
23+
});
2624
});
2725

28-
it('Should not boolean: success', () => {
26+
it('Should not translate boolean: success', () => {
2927
init({yandexApiKey: 'apiToken'});
30-
return translate({bool: true}, 'es')
31-
.then(e => {
32-
expect(e).toBeDefined();
33-
expect(e).toEqual({bool: true});
34-
});
28+
return translate({bool: true}, 'es').then(e => {
29+
expect(e).toBeDefined();
30+
expect(e).toEqual({bool: true});
31+
});
32+
});
33+
34+
it('Should translate multiple languages async', () => {
35+
init({yandexApiKey: 'apiToken'});
36+
const languages = ['es', 'ar'];
37+
const all = Promise.all(
38+
languages.map(lang => translate(mock.dataObject, lang))
39+
);
40+
41+
all.then(data => {
42+
expect(data).toEqual([mock.translatedES, mock.translatedAR]);
43+
});
3544
});
3645
});

lib/fixture/data.fixture.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
const dataObject = {name: 'name', some: [{name: 'j'}], addressField: ['city', 'state', 'zipdecode'], bool: true, num: 33, badBool: false, nully: null, undefin: undefined};
2-
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};
1+
const dataObject = {
2+
name: 'name',
3+
some: [{name: 'j'}],
4+
addressField: ['city', 'state', 'zipdecode'],
5+
bool: true,
6+
num: 33,
7+
badBool: false,
8+
nully: null,
9+
undefin: undefined
10+
};
11+
12+
const translatedES = {
13+
name: 'name-es',
14+
some: [{name: 'j-es'}],
15+
addressField: ['city-es', 'state-es', 'zipdecode-es'],
16+
bool: true,
17+
num: 33,
18+
badBool: false,
19+
nully: null,
20+
undefin: undefined
21+
};
22+
23+
const translatedAR = {
24+
name: 'name-ar',
25+
some: [{name: 'j-ar'}],
26+
addressField: ['city-ar', 'state-ar', 'zipdecode-ar'],
27+
bool: true,
28+
num: 33,
29+
badBool: false,
30+
nully: null,
31+
undefin: undefined
32+
};
333

434
module.exports = {
535
dataObject,
6-
transaltedES
36+
translatedES,
37+
translatedAR
738
};

lib/translate-json-object.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ function TranslateJSONObject() {
1212
// Set the current available service for translation e.g. google, bing, yandex etc..
1313
var translateSrv;
1414
var setting;
15-
// The list of promises that should be resolve prior to returning the full `Object translation`
16-
var promises = [];
17-
var destObj = {};
15+
1816
var serviceType;
1917

2018
/**
@@ -49,16 +47,26 @@ function TranslateJSONObject() {
4947
* @return {Promise} It returns a promise with the translated object
5048
*/
5149
function translate(srcObj, language) {
50+
var promises = [];
51+
var destObj = {};
52+
5253
if (!setting.googleApiKey && !setting.yandexApiKey) {
5354
return Promise.reject(constant.ERROR.MISSING_TOKEN);
5455
}
5556

5657
if (!_.isString(language)) {
57-
return Promise.reject('Please provide a language param [type String] e.g. translate(obj, es)');
58+
return Promise.reject(
59+
'Please provide a language param [type String] e.g. translate(obj, es)'
60+
);
5861
}
5962

6063
if (!isValidLang(language, serviceType)) {
61-
return Promise.reject(serviceType + ' doesn\'t support the language code you specified [' + language + '], please try another language code (ISO-639-1)');
64+
return Promise.reject(
65+
serviceType +
66+
' doesn\'t support the language code you specified [' +
67+
language +
68+
'], please try another language code (ISO-639-1)'
69+
);
6270
}
6371

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

116124
if (valuesArray.length !== 0) {
117-
promises.push(translateSrv.object(language, key, dest, keysArray, valuesArray));
125+
promises.push(
126+
translateSrv.object(language, key, dest, keysArray, valuesArray)
127+
);
118128
}
119129
}
120130

121131
// Recursivly loop through an object
122-
recurisveTranslateObject(destObj, {
123-
ROOT: srcObj
124-
}, language);
132+
recurisveTranslateObject(
133+
destObj,
134+
{
135+
ROOT: srcObj
136+
},
137+
language
138+
);
125139

126140
return new Promise(function (resolve, reject) {
127-
Promise.all(promises).then(function () {
128-
if (ARRAY_ROOT_TYPE) {
129-
resolve(destObj.ROOT.arrayType);
130-
} else {
131-
resolve(destObj.ROOT);
132-
}
133-
}).catch(reject);
141+
Promise.all(promises)
142+
.then(function () {
143+
if (ARRAY_ROOT_TYPE) {
144+
resolve(destObj.ROOT.arrayType);
145+
} else {
146+
resolve(destObj.ROOT);
147+
}
148+
})
149+
.catch(reject);
134150
});
135151
}
136152

lib/util/valid-lang.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var constant = require('./constant');
22

3-
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'];
4-
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'];
3+
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'];
4+
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'];
55

66
module.exports = function (lang, service) {
77
switch (service) {

0 commit comments

Comments
 (0)