Skip to content

Instantly share code, notes, and snippets.

@WeiChiaChang
Created February 21, 2020 05:02
Show Gist options
  • Save WeiChiaChang/138fd9458275df04da5a83524650672c to your computer and use it in GitHub Desktop.
Save WeiChiaChang/138fd9458275df04da5a83524650672c to your computer and use it in GitHub Desktop.
Flags Name After Google Translate
var flags = [
{
"code": "Afghanistan",
"name": "Afghanistan",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/af.svg"
},
{
"code": "Aland Islands",
"name": "Aland Islands",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/ax.svg"
},
{
"code": "Albania",
"name": "Albania",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/al.svg"
},
{
"code": "Algeria",
"name": "Algeria",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/dz.svg"
},
{
"code": "American Samoa",
"name": "American Samoa",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/as.svg"
},
{
"code": "Andorra",
"name": "Andorra",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/ad.svg"
},
{
"code": "Angola",
"name": "Angola",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/ao.svg"
},
{
"code": "Anguilla",
"name": "Anguilla",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/ai.svg"
},
{
"code": "Antigua and Barbuda",
"name": "Antigua and Barbuda",
"img": "https://lipis.github.io/flag-icon-css/flags/4x3/ag.svg"
}
]
const translate = require('@k3rn31p4nic/google-translate-api')
const fs = require('fs')
// const UPDATE_TIMER = 200;
function test () {
for (let i = 0; i < flags.length; i++) {
translate(flags[i]['name'], {
from: 'en',
to: 'zh-TW'
}).then(res => {
flags[i]['name'] = res.text.length === 0 ? flags[i]['name'] : res.text
// setInterval(function () {
fs.readFile('./result.json', 'utf-8', function (err, data) {
if (err) throw err
var arrayOfObjects = JSON.parse(data)
arrayOfObjects.results.push(flags[i])
fs.writeFile('./result.json', JSON.stringify(arrayOfObjects), 'utf-8', function (err) {
if (err) throw err
console.log('Done!')
})
})
// }, UPDATE_TIMER)
}).catch(err => {
console.error(err)
})
}
}
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment