Skip to content

Instantly share code, notes, and snippets.

View TheBooker66's full-sized avatar
💭
Vibin'

Ethan TheBooker66

💭
Vibin'
View GitHub Profile
@TheBooker66
TheBooker66 / translate.js
Last active January 9, 2022 19:41
Translate sentences or words and hear them with Google Translate and Wikipedia
const nodeFetch = require("node-fetch"), request = require("request"), say = require("say");
async function translate(text) {
text = text.replace(/^Translate to/i).trim();
//Extracts the text to translated from the text inputted
const textToTranslate = text.replace(text.split(" ")[1], "").replace(undefined, "").trim();
const url = encodeURI(`https://en.wikipedia.org/w/api.php?action=languagesearch&search=${text.split(" ")[1]}&format=json`);
//Sends a request to Wikipedia to fetch the language code
let langResponse = await nodeFetch(url, {
method: 'GET',