Skip to content

Instantly share code, notes, and snippets.

@ddikman
Created November 7, 2018 08:57
Show Gist options
  • Save ddikman/a280a72f7e5d3f9a4ab3db95ce025a52 to your computer and use it in GitHub Desktop.
Save ddikman/a280a72f7e5d3f9a4ab3db95ce025a52 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const exec = require('child_process').execSync
// SUMMARY:
// This simple script uses gtranslate npm cli tool to translate an input file
// of countries in json format to another format with translated country names
// the expected input would be [ { "country": "name" } ]
let inputCountries = JSON.parse(fs.readFileSync('countries.json'))
function translate (term) {
return exec(`gtranslate --brief --source en --target es ${term}`).toString().trim()
}
// you can use inputCountries.slice(1, 5) to restrict how many entries to translate
var outputCountries = inputCountries.map(country => {
return {
name: country.country,
translation: translate(country.country)
}
})
process.stdout.write(JSON.stringify(outputCountries))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment