Skip to content

Instantly share code, notes, and snippets.

@azasypkin
Forked from yankouskia/en.json
Last active August 30, 2018 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azasypkin/e3e3526c7bb73223ac53b5cb126ecfae to your computer and use it in GitHub Desktop.
Save azasypkin/e3e3526c7bb73223ac53b5cb126ecfae to your computer and use it in GitHub Desktop.
translate (npx https://gist.github.com/azasypkin/e3e3526c7bb73223ac53b5cb126ecfae /absolute-path/en.json zh-cn)
#!/usr/bin/env node
const fs = require('fs');
const translate = require('google-translate-api');
const [,, enFile, targetLocale] = process.argv;
console.log(`File ${enFile}, translate to locale ${targetLocale}`);
const en = require(enFile);
const target = targetLocale;
const translations = {};
const keys = Object.keys(en);
function translateNextKey() {
if (keys.length === 0) {
console.log(translations);
const str = JSON.stringify(translations, null, 2);
fs.writeFile(`${target}.json`, str, { encoding: 'utf-8'}, () => console.log('done'));
return;
}
const key = keys.pop();
if (key === 'formats') {
translations[key] = en[key];
return translateNextKey();
}
console.log(`Translating ${key}:${en[key]} to ${target} (left ${keys.length})`);
translate(en[key], { from: 'en', to: target })
.then(trans => {
translations[key] = trans.text;
setTimeout(() => translateNextKey(), 100);
}, err => {
console.error(`Error during translation of ${key}: `, err);
console.log('Change IP address and press any key');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.once('data', () => {
keys.push(key);
setTimeout(() => translateNextKey(), 100);
});
});
}
translateNextKey();
{
"name": "tr",
"version": "1.0.0",
"bin": "./index.js",
"license": "MIT",
"dependencies": {
"google-translate-api": "^2.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment