1

Translate text using Yandex Translation API

Function JavaScript 1 revisions 226 2 years ago 2 years ago
async function yandexTranslate(text, language) {
	return (await fetch('https://translate.yandex.net/api/v1.5/tr.json/translate?key='
			+ process.env.YANDEX_API_KEY
			+ '&lang='
			+ encodeURIComponent(language)
			+ '&text='
			+ encodeURIComponent(text))
		.then(response => response.json())).text;
}

This function translates a text to language using Yandex API.

It assumes that you have your translation API key set in process.env.YANDEX_API_KEY as well as it uses the Fetch API. Also this is an async function so you probably should use await or .then() when using it.

It returns an array of translations returned but the API, and not a string.