obdfcascrape/translator.js
Martin Donnelly be5d3eae07 init
2019-05-05 20:13:56 +01:00

61 lines
1.4 KiB
JavaScript

const AWS = require('aws-sdk');
const translate = require('translate-google');
const logger = require('log4js').getLogger('Translate');
const jsonfile = require('jsonfile');
logger.level = 'debug';
require('dotenv').config({
'path': `${__dirname }/.env`
});
// https://github.com/matheuss/google-translate-api
// https://github.com/markcallen/snssqs
const langFile = jsonfile.readFileSync('./helpers/dictionary/lang.se.json');
logger.debug(langFile);
const dictionary = new Map(langFile);
const demapped = [...dictionary];
logger.debug(demapped);
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++)
await callback(array[index], index, array);
}
async function process() {
await asyncForEach(demapped, async (a, b) => {
// await waitFor(50);
if (a[1] === '') {
a[1] = await translatePhrase(a[0], 'sv');
logger.debug(a);
await waitFor(2000);
}
});
logger.debug('after', demapped);
const newDictionary = new Map(demapped);
jsonfile.writeFileSync('./helpers/dictionary/trans.se.json', [...newDictionary]);
}
async function translatePhrase(phrase, fromLang) {
return await translate(phrase, { 'from': fromLang, 'to': 'en' }).then(res => {
return res;
}).catch(err => {
console.error(err);
});
}
process();