Last active
November 1, 2019 14:10
-
-
Save 6220119/3b3564f346a3658dfadad5ac5cffdeee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const moment = require("moment"); | |
const _ = require("lodash"); | |
const path = require("path"); | |
const agent = require("superagent-promise")(require("superagent"), Promise); | |
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages | |
const YANDEX_API_KEYS = process.env.TRANSLATION_API_KEY; | |
const inputFile = "./data/en.json"; | |
const destinationCodes = ["vi"]; | |
const apiUrl = ({ value, translationDirection }) => { | |
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${YANDEX_API_KEYS}&text=${value}&lang=${translationDirection}`; | |
return url; | |
}; | |
function transformResponse(res) { | |
return _.get(JSON.parse(res.text), "text[0]", "{N/A}"); | |
} | |
function setValue(accumulator, keyChain, value) { | |
let temp = accumulator | |
for (let i = 0; i < keyChain.length - 1; ++i) { | |
let key = keyChain[i] | |
let nextKey = keyChain[i + 1] | |
let currentNode = temp[key] | |
if (!currentNode) { | |
currentNode = _.isFinite(nextKey) ? [] : {} | |
} | |
temp[key] = currentNode | |
temp = currentNode | |
} | |
temp[keyChain[keyChain.length - 1]] = value | |
} | |
// setValue({}, ['a', '1', 0, 'k'], 'value') | |
function iterLeaves(value, keyChain, accumulator, languageKey) { | |
accumulator = accumulator || {}; | |
keyChain = keyChain || []; | |
if (_.isObject(value)) { | |
return _.chain(value) | |
.reduce((handlers, v, k) => { | |
return handlers.concat( | |
iterLeaves(v, keyChain.concat(k), accumulator, languageKey) | |
); | |
}, []) | |
.flattenDeep() | |
.value(); | |
} else { | |
return function() { | |
console.log( | |
_.template("Translating <%= value %> to <%= languageKey %>")({ | |
value, | |
languageKey | |
}) | |
); | |
//Translates individual string to language code | |
return agent( | |
"GET", | |
apiUrl({ | |
value: encodeURI(value), | |
translationDirection: `en-${languageKey}` | |
}) | |
) | |
.then(transformResponse) | |
.then(text => { | |
//Sets the value in the accumulator | |
// const convertedKeyChain = keyChain.map((k) => _.isFinite(k) ? k : `[${k}]`) | |
// console.log(convertedKeyChain); | |
// _.set(accumulator, convertedKeyChain, text); | |
setValue(accumulator, keyChain, text) | |
//This needs to be returned to it's eventually written to json | |
return accumulator; | |
}); | |
}; | |
} | |
} | |
Promise.all( | |
_.reduce( | |
destinationCodes, | |
(sum, languageKey) => { | |
const fileName = _.template( | |
"./data/<%= languageKey %>-<%= timeStamp %>.json" | |
)({ | |
languageKey, | |
timeStamp: moment().unix() | |
}); | |
//Starts with the top level strings | |
return sum.concat( | |
_.reduce( | |
iterLeaves( | |
JSON.parse(fs.readFileSync(path.resolve(inputFile), "utf-8")), | |
undefined, | |
undefined, | |
languageKey | |
), | |
(promiseChain, fn) => { | |
return promiseChain.then(fn); | |
}, | |
Promise.resolve() | |
) | |
.then(payload => { | |
fs.writeFileSync(fileName, JSON.stringify(payload)); | |
}) | |
.then( | |
_.partial( | |
console.log, | |
"Successfully translated all nodes, file output at " + fileName | |
) | |
) | |
); | |
}, | |
[] | |
) | |
).then(() => { | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment