Yandex.Translate
Translate any type of data (string, string[], json) into one or more languages.
Yandex.Translate API for Node.js
Installation
-
Sign up for a Yandex API key at https://translate.yandex.com/developers/keys
-
Install this package in your project:
$ npm install --save yet-another-yandex-translate
Adding to your project
In Node.js
Call require to get the instance:
const { YandexTranslate } = require('yet-another-yandex-translate');
Or in ES6 and TS:
import YandexTranslate from 'yet-another-yandex-translate';
Usage
const yt = new YandexTranslate('<< YOUR YANDEX API KEY HERE >>');
Translate
Translate any type of data (string, string[], json) into one or more languages:
// Hello world!
await yt.translate('Привет мир!', {from: 'ru', to: 'en', format: 'html'});
// [ 'foo', 'bar' ]
await yt.translate([ 'foo', 'bar' ], {to: 'en', format: 'plain'});
// [{text: 'Hello world!', lang: 'en'}, {text: 'Bonjour tout le monde!', lang: 'fr'}]
await yt.translate('Привет мир!', {to: ['en', 'fr']});
// [{text: ['Hello world!', 'Hello world!'], lang: 'en'}, {text: ['Bonjour tout le monde!', 'Bonjour tout le monde!'], lang: 'fr'}]
await yt.translate(['Привет мир!', 'Привет мир!'], {to: ['en', 'fr']});
// {
// key1: 'Hello 1',
// key2: 'hi 2',
// key3: [ false, 'Hello 1', true, 'hi 2', null ],
// key4: 123,
// привет: [ 'hi 4' ]
// }
const test = {
key1: 'привет 1',
key2: 'привет 2',
key3: [false, 'привет 1', true, 'привет 2', null],
key4: 123,
привет: ['привет 4']
};
await yt.translate(text, {to: 'en'});
Detect the language
Detects the language of the specified any type of data (string, string[], json).
// ru
await yt.detect('Привет мир!');
// ru
await yt.detect('Привет мир!', {hint: 'en,fr'});
// ru
await yt.detect(test, {hint: 'en,fr'});
// [{lang: 'ru'}, {lang: 'en'}]
await yt.detect(['Привет мир!', 'Hello world!']);
Get the list of supported languages
// {dirs: [], langs: {}}
await yt.getLangs();
await yt.getLangs({ui: 'en'});
Yandex Translate
Demo | Docs | List of supported languages | API Key | Statistics