From 51266a0ad6683008197c150d7e78c960db2283d8 Mon Sep 17 00:00:00 2001 From: Fares Bessrour Date: Fri, 13 Oct 2017 12:14:40 -0400 Subject: [PATCH] Added Caching Here, I added a caching feature. It overrides the existing translated file, if the key already exist in the previously translated file, it doesn't make the request to the Google Translate API, instead it reuses the value. The only condition is that you keep your files in the tmp folder on the root level of the project and when you update your original JSON File it will check bot the updated JSON and updates the older translated versions with new keys and values. --- translate-json | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/translate-json b/translate-json index 963cd78..79894db 100644 --- a/translate-json +++ b/translate-json @@ -21,17 +21,33 @@ if (process.argv.length >= 5) { return _.get(JSON.parse(res.text), ['data', 'translations', 0, 'translatedText'], ''); } - function iterLeaves(value, keyChain, accumulator, languageKey) { + function checkIfExists(jsonObject, keyChain){ + + } + + function iterLeaves(value, keyChain, accumulator, languageKey, output) { accumulator = accumulator || {}; keyChain = keyChain || []; + const cached = !(output == null) if (_.isObject(value)) { return _.chain(value).reduce((handlers, v, k) => { - return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey)); + return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey, output[k])); }, []).flattenDeep().value(); + } else if (cached) { + return function() { + const cachedValue = output + console.log(_.template('Using cached value for <%= value %> in <%= languageKey %>: <%= cachedValue %>')({ + value, + languageKey, + cachedValue + })) + _.set(accumulator, keyChain, cachedValue); + return accumulator + } } else { return function () { + console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey})); - //Translates individual string to language code return agent('GET', apiUrl({ value: encodeURIComponent(value), @@ -49,13 +65,16 @@ if (process.argv.length >= 5) { } Promise.all(_.reduce(destinationCodes, (sum, languageKey) => { - const fileName = _.template('/tmp/<%= languageKey %>-<%= timeStamp %>.json')({ - languageKey, - timeStamp: moment().unix() + const fileName = _.template('./tmp/translation-<%= languageKey %>.json')({ + languageKey }); + const outputJSON = JSON.parse(fs.readFileSync(path.resolve(_.template('./tmp/translation-<%= languageKey %>.json')({ + languageKey + })), 'utf-8')) + //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 sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey, outputJSON), (promiseChain, fn) => { return promiseChain.then(fn); }, Promise.resolve()).then((payload) => { fs.writeFileSync(fileName, JSON.stringify(payload));