Skip to content

Instantly share code, notes, and snippets.

@qunabu
Last active January 16, 2020 11:10
Show Gist options
  • Save qunabu/55ce89916bca1483c86788e14a4a6070 to your computer and use it in GitHub Desktop.
Save qunabu/55ce89916bca1483c86788e14a4a6070 to your computer and use it in GitHub Desktop.
getTranslationProc.js
/**
* Proccess all the input array and calls `getTranslation` onEach row
* @param {Array} arr, array of objects term to be translated in the shape of `{key:"", value:""}`, eg. `{key:"fullName", value:"Full name"}
* @returns {Promise} resolved array is in the same shape as input
*/
const processTranslation = (arr, target = "de", api_key = process.env.GKEY) => {
return new Promise((resolve) => {
/**
*
* @param {Array} arr input variables array. Terms to be translated.
* @param {Array} book output variable array. Translated terms.
* @param {Integer} currentIndex of proccesing queue.
*/
const convert = (arr, book = [], currentIndex = 0) =>
currentIndex <= arr.length - 1
? getTranslation(arr[currentIndex], target, api_key).then(obj =>
convert(arr, [...book, obj], currentIndex + 1)
)
: resolve(book);
convert(arr);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment