yet-another-yandex-translate
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

yet-another-yandex-translate Build Status Greenkeeper badge

Translate any type of data (string, string[], json) into one or more languages.

Installation

  1. Sign up for a Yandex API key at https://translate.yandex.com/developers/keys

  2. 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

Package Sidebar

Install

npm i yet-another-yandex-translate

Weekly Downloads

104

Version

1.2.3

License

MIT

Unpacked Size

30.1 kB

Total Files

18

Last publish

Collaborators

  • reiserford