From 483d384d4ab79595129a41728b7cdd59cf99911d Mon Sep 17 00:00:00 2001 From: Fares Bessrour Date: Thu, 12 Oct 2017 14:15:36 -0400 Subject: [PATCH 1/4] Bug with Special Characters I found a bug where the Script stopped when it found a # (Specific case of special characters). Using the encodeURIComponent instead of encodeURI solves this issue. --- translate-json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/translate-json b/translate-json index 09d40e0..963cd78 100644 --- a/translate-json +++ b/translate-json @@ -34,7 +34,7 @@ if (process.argv.length >= 5) { //Translates individual string to language code return agent('GET', apiUrl({ - value: encodeURI(value), + value: encodeURIComponent(value), languageKey, apiKey })).then(transformResponse).then((text) => { @@ -66,4 +66,4 @@ if (process.argv.length >= 5) { } else { console.error('You must provide an input json file and a comma-separated list of destination language codes.'); -} \ No newline at end of file +} From 51266a0ad6683008197c150d7e78c960db2283d8 Mon Sep 17 00:00:00 2001 From: Fares Bessrour Date: Fri, 13 Oct 2017 12:14:40 -0400 Subject: [PATCH 2/4] 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)); From 8f89dab4ae2a201969bd0a6084c6f92489faf4e4 Mon Sep 17 00:00:00 2001 From: faresbessrour Date: Fri, 13 Oct 2017 14:40:11 -0400 Subject: [PATCH 3/4] Fixed issue where the initial object was undefined when output language file do not exist already --- translate-json | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/translate-json b/translate-json index 79894db..92fbefd 100644 --- a/translate-json +++ b/translate-json @@ -21,20 +21,17 @@ if (process.argv.length >= 5) { return _.get(JSON.parse(res.text), ['data', 'translations', 0, 'translatedText'], ''); } - function checkIfExists(jsonObject, keyChain){ - - } - function iterLeaves(value, keyChain, accumulator, languageKey, output) { accumulator = accumulator || {}; keyChain = keyChain || []; - const cached = !(output == null) + const cached = output != null if (_.isObject(value)) { + if(!_.isObject(output)) output = {} return _.chain(value).reduce((handlers, v, k) => { return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey, output[k])); - }, []).flattenDeep().value(); + }, []).flattenDeep().value(); } else if (cached) { - return function() { + return function () { const cachedValue = output console.log(_.template('Using cached value for <%= value %> in <%= languageKey %>: <%= cachedValue %>')({ value, @@ -46,8 +43,8 @@ if (process.argv.length >= 5) { } } else { return function () { - - console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey})); + + console.log(_.template('Translating <%= value %> to <%= languageKey %>')({ value, languageKey })); //Translates individual string to language code return agent('GET', apiUrl({ value: encodeURIComponent(value), @@ -68,10 +65,15 @@ if (process.argv.length >= 5) { const fileName = _.template('./tmp/translation-<%= languageKey %>.json')({ languageKey }); - - const outputJSON = JSON.parse(fs.readFileSync(path.resolve(_.template('./tmp/translation-<%= languageKey %>.json')({ - languageKey - })), 'utf-8')) + const outputJSON = (function () { + try { + return JSON.parse(fs.readFileSync(path.resolve(_.template('./tmp/translation-<%= languageKey %>.json')({ + languageKey + })), 'utf-8')) + } catch (e) { + return {} + } + })() //Starts with the top level strings return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey, outputJSON), (promiseChain, fn) => { @@ -85,4 +87,4 @@ if (process.argv.length >= 5) { } else { console.error('You must provide an input json file and a comma-separated list of destination language codes.'); -} +} \ No newline at end of file From e3ca0ca9c9df6505ef80789916c178cdabd6619c Mon Sep 17 00:00:00 2001 From: faresbessrour Date: Fri, 13 Oct 2017 14:44:57 -0400 Subject: [PATCH 4/4] Added empty tmp file where translated json will be stored --- tmp/translation-es.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 tmp/translation-es.json diff --git a/tmp/translation-es.json b/tmp/translation-es.json new file mode 100644 index 0000000..04414d3 --- /dev/null +++ b/tmp/translation-es.json @@ -0,0 +1 @@ +{"okay":"Hola"} \ No newline at end of file