UNPKG

2.79 kBMarkdownView Raw
1### About
2
3[do-translate](https://www.npmjs.com/package/do-translate) is *npm* package for translating text samples via translation providers like *Google* and *Yandex*.
4
5### Install
6
7```bash
8npm install do-translate
9```
10
11### Usage
12
13Each *Translator* instance defines a bunch of public methods. Mostly, only
14*Translato::lookup* method should be used to perform an actual translation.
15*Translator::getLangs* and *Translato::translate* methods perform requests without
16caching. *Translator::lookup* is expecting three params: *textToTranslate*, *langTo* and *langFrom*. Translation is performed asynchronously, result is immediately
17resolved as [Promise](http://www.html5rocks.com/en/tutorials/es6/promises/) object.
18
19```js
20var Translator = require('do-translate').Translator;
21```
22Provider *alias* is passed to *Translator* upon creation along with API *key*
23and *uri*.
24
25### With NoopProvider for testing
26
27```js
28var noopTranslator = new Translator('noop');
29
30noopTranslator.lookup('sample', 'ru', 'en')
31 .then(function(result) {
32 // "sample_en-ru" from request
33 return noopTranslator.lookup('sample', 'ru', 'en');
34 })
35 .then(function(result) {
36 // "sample_en-ru" from cache (should resolve faster)
37 })
38 .catch(function(error) {
39 // debug error here
40 })
41;
42```
43
44### With Google Translate
45
46```js
47var googleTranslator = new Translator('google', 'google-key', 'google-uri');
48
49googleTranslator.lookup('sample', 'ru', 'en')
50 .then(function(result) {
51 // "образец" from request
52 return googleTranslator.lookup('sample', 'ru', 'en');
53 })
54 .then(function(result) {
55 // "образец" from cache (should resolve faster)
56 })
57 .catch(function(error) {
58 // debug error here
59 })
60;
61```
62
63### With Yandex Translate
64
65```js
66var yandexTranslator = new Translator('yandex', 'yandex-key', 'yandex-uri');
67
68yandexTranslator.lookup('sample', 'ru', 'en')
69 .then(function(result) {
70 // "образец" from request
71 return yandexTranslator.lookup('sample', 'ru', 'en');
72 })
73 .then(function(result) {
74 // "образец" from cache (should resolve faster)
75 })
76 .catch(function(error) {
77 // debug error here
78 })
79;
80```
81
82### Cache
83
84Every response is cached. Cached value is returned if result for current request
85is present locally. Note that to process translation, two requests at minimum
86are to perform:
87- Request supported languages
88- Perfrom translation
89
90### API errors
91
92API errors, in general, like "translation failure", "api limit excess" are silent
93and not reported in any form for now.
94
95### Todo
96
97- Browser version
98- Add new provider lookup strategy (e.g. "web scrapping")
99- Use native ES6 without transpiler.
100- Use native *fetch* fn.
101- Headless browser tests.