Translate text to many languages

node v8.11.1
version: 1.2.0
endpointsharetweet
const { promisify } = require("util"); const ISO6391 = require("iso-639-1") const translator = require("google-translator"); // manually convert our function to a promise style function // usually this would be as simple as // require("util").promisify(translator) // but our library did not use the standard callback-style for functions const translate = (...args) => new Promise(resolve => translator(...args, resolve)); //__________________________________________________ // Properties const inputText = "talk"; const inputLanguageCode = 'en' //__________________________________________________ // Run // get all language codes to translate to const languageCodes = translator.languages; // translate the inputText to each language for (const outputLanguageCode in languageCodes) { var outputLanguage = require("util").promisify(ISO6391).getName(outputLanguageCode); console.log(outputLanguage + " " + (await translate(inputLanguageCode, outputLanguageCode, inputText)).text); }
Created from: https://npm.runkit.com/google-translator
Loading…

no comments

    sign in to comment