google-translate-api
Advanced tools
Comparing version 1.0.0 to 2.0.1
37
index.js
@@ -10,2 +10,3 @@ /** | ||
function translate(text, opts) { | ||
opts = opts || {}; | ||
return tk(text).then(function (tk) { | ||
@@ -37,10 +38,13 @@ var url = 'https://translate.google.com/translate_a/single'; | ||
var result = { | ||
text: { | ||
corrected: false, | ||
correction: '', | ||
value: '' | ||
}, | ||
text: '', | ||
from: { | ||
corrected: false, | ||
iso: '' | ||
language: { | ||
didYouMean: false, | ||
iso: '' | ||
}, | ||
text: { | ||
autoCorrected: false, | ||
value: '', | ||
didYouMean: false | ||
} | ||
} | ||
@@ -56,3 +60,3 @@ }; | ||
if (obj[0] !== undefined) { | ||
result.text.value += obj[0]; | ||
result.text += obj[0]; | ||
} | ||
@@ -62,9 +66,9 @@ }); | ||
if (body[2] === body[8][0][0]) { | ||
result.from.iso = body[2]; | ||
result.from.language.iso = body[2]; | ||
} else { | ||
result.from.corrected = true; | ||
result.from.iso = body[8][0][0]; | ||
result.from.language.didYouMean = true; | ||
result.from.language.iso = body[8][0][0]; | ||
} | ||
if (body[7] !== undefined) { | ||
if (body[7] !== undefined && body[7][0] !== undefined) { | ||
var str = body[7][0]; | ||
@@ -75,4 +79,9 @@ | ||
result.text.corrected = true; | ||
result.text.correction = str; | ||
result.from.text.value = str; | ||
if (body[7][5] === true) { | ||
result.from.text.autoCorrected = true; | ||
} else { | ||
result.from.text.didYouMean = true; | ||
} | ||
} | ||
@@ -79,0 +88,0 @@ |
{ | ||
"name": "google-translate-api", | ||
"version": "1.0.0", | ||
"version": "2.0.1", | ||
"description": "A free and unlimited API for Google Translate", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && nyc ava", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=text-lcov > coverage.lcov && codecov" | ||
}, | ||
@@ -38,2 +39,5 @@ "repository": { | ||
"ava": "^0.14.0", | ||
"codecov": "^1.0.1", | ||
"coveralls": "^2.11.9", | ||
"nyc": "^6.4.4", | ||
"xo": "^0.15.1" | ||
@@ -40,0 +44,0 @@ }, |
@@ -1,2 +0,42 @@ | ||
# google-translate-api [![Build Status](https://travis-ci.org/matheuss/vertaler.svg?branch=master)](https://travis-ci.org/matheuss/google-translate-api) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
A free and unlimited API for Google Translate :dollar::no_entry_sign: | ||
# google-translate-api [![Build Status](https://travis-ci.org/matheuss/google-translate-api.svg?branch=master)](https://travis-ci.org/matheuss/google-translate-api) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![Coverage Status](https://coveralls.io/repos/github/matheuss/google-translate-api/badge.svg?branch=master)](https://coveralls.io/github/matheuss/google-translate-api?branch=master) [![codecov](https://codecov.io/gh/matheuss/google-translate-api/branch/master/graph/badge.svg)](https://codecov.io/gh/matheuss/google-translate-api) | ||
A **free** and **unlimited** API for Google Translate :dollar::no_entry_sign: | ||
## Features | ||
- Auto language detection | ||
- Spelling correction | ||
- Language correction | ||
- Fast and reliable – it uses the same servers that [translate.google.com](https://translate.google.com) uses | ||
## Install | ||
``` | ||
npm install --save google-translate-api | ||
``` | ||
## Usage | ||
``` js | ||
const translate = require('google-translate-api'); | ||
translate('Ik spreek Engels', {from: 'auto', to: 'en'}).then(res => { | ||
console.log(res.text); | ||
//=> I speak English | ||
console.log(res.from.language.iso); | ||
//=> nl | ||
}); | ||
``` | ||
## API | ||
- TODO: docs | ||
## Related | ||
- [`vertaler`](https://github.com/matheuss/vertaler) – CLI for this module | ||
## License | ||
MIT © [Matheus Fernandes](http://matheus.top) |
90
test.js
@@ -8,19 +8,83 @@ /** | ||
test('translate from \'\' (auto) to \'\' (english)', async t => { | ||
const res = await translate('Ik spreek Engels', {}); | ||
t.falsy(res.text.corrected); | ||
t.falsy(res.text.correction); | ||
t.is(res.text.value, 'I speak English'); | ||
t.falsy(res.from.corrected); | ||
t.is(res.from.iso, 'nl'); | ||
test('translate without any options', async t => { | ||
try { | ||
const res = await translate('vertaler'); | ||
t.is(res.text, 'translator'); | ||
t.false(res.from.language.didYouMean); | ||
t.is(res.from.language.iso, 'nl'); | ||
t.false(res.from.text.autoCorrected); | ||
t.is(res.from.text.value, ''); | ||
t.false(res.from.text.didYouMean); | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); | ||
test('translate from auto to dutch', async t => { | ||
const res = await translate('I speak Dutch', {from: 'auto', to: 'nl'}); | ||
try { | ||
const res = await translate('translator', {from: 'auto', to: 'nl'}); | ||
t.falsy(res.text.corrected); | ||
t.falsy(res.text.correction); | ||
t.is(res.text.value, 'ik spreek Nederlands'); | ||
t.falsy(res.from.corrected); | ||
t.is(res.from.iso, 'en'); | ||
t.is(res.text, 'vertaler'); | ||
t.false(res.from.language.didYouMean); | ||
t.is(res.from.language.iso, 'en'); | ||
t.false(res.from.text.autoCorrected); | ||
t.is(res.from.text.value, ''); | ||
t.false(res.from.text.didYouMean); | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); | ||
test('translate some english text setting the source language as portuguese', async t => { | ||
try { | ||
const res = await translate('translator', {from: 'pt', to: 'nl'}); | ||
t.true(res.from.language.didYouMean); | ||
t.is(res.from.language.iso, 'en'); | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); | ||
test('translate some misspelled english text to dutch', async t => { | ||
try { | ||
const res = await translate('I spea Dutch', {from: 'en', to: 'nl'}); | ||
if (res.from.text.autoCorrected || res.from.text.didYouMean) { | ||
t.is(res.from.text.value, 'I [speak] Dutch'); | ||
} else { | ||
t.fail(); | ||
} | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); | ||
test('translate some text with an invalid tk', async t => { | ||
try { | ||
await translate('vertaler', {tk: 0}); | ||
} catch (err) { | ||
t.is(err.code, 'BAD_REQUEST'); | ||
} | ||
}); | ||
test.todo('try to translate some text without an internet connection'); | ||
test('translate some text and get only the raw output', async t => { | ||
try { | ||
const res = await translate('vertaler', {raw: true}); | ||
t.truthy(res); | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); | ||
test('translate some text and get the raw output alongside', async t => { | ||
try { | ||
const res = await translate('vertaler', {includeRaw: true}); | ||
t.truthy(res.raw); | ||
} catch (err) { | ||
t.fail(err.code); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
13215
280
43
5