Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

linguister

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

linguister - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

15

lib/components/translate.d.ts

@@ -112,2 +112,3 @@ declare const languages: {

text: string;
/** The original text */
from: {

@@ -130,3 +131,3 @@ language: {

/** The raw response from th egoogle translate server */
raw?: Array<any>;
raw: Array<any>;
};

@@ -138,3 +139,6 @@ /**

*/
export declare function LangFrom(query: string): langISO | string | undefined;
export declare function LangFrom(query: string): string | undefined;
/**
* Used to spawn api requests. Will handle a ratelimit
*/
export declare class TranslateAPI {

@@ -149,3 +153,5 @@ private TKK;

* @param {Object} [opts={}] Options for the request
* @returns {Translation} The translation response
* @param {String} [opts.from] The language to translate from
* @param {String} [opts.to] The language to translate to
* @returns {Promise<Translation>} The translation response
*/

@@ -157,6 +163,5 @@ translate(text: string, opts?: {

to?: string;
/** Whether to include the raw API response */
raw?: boolean;
}): Promise<Translation>;
private _translate;
}
export {};

@@ -127,5 +127,6 @@ "use strict";

/**
* Does practically nothing
* Practically never does anything. Gonna have to look into how the api actually works
* @private
* @param {string} TKK Previous TKK
* @return {string} The token.. I think
* @return {string} The fetched token
*/

@@ -175,2 +176,5 @@ function getToken(TKK) {

exports.LangFrom = LangFrom;
/**
* Used to spawn api requests. Will handle a ratelimit
*/
class TranslateAPI {

@@ -186,3 +190,5 @@ constructor(ratelimit = 1000) {

* @param {Object} [opts={}] Options for the request
* @returns {Translation} The translation response
* @param {String} [opts.from] The language to translate from
* @param {String} [opts.to] The language to translate to
* @returns {Promise<Translation>} The translation response
*/

@@ -192,4 +198,3 @@ translate(text, opts = {}) {

from: LangFrom(opts.from || 'auto') || 'auto',
to: LangFrom(opts.to || 'en') || 'en',
raw: opts.raw || false
to: LangFrom(opts.to || 'en') || 'en'
};

@@ -201,66 +206,58 @@ const startat = (this.lastreq =

return new Promise((resolve, reject) => {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
return https_1.get('https://translate.google.com/translate_a/single?' +
querystring_1.stringify({
client: 'gtx',
sl: options.from,
tl: options.to,
hl: options.to,
dt: [
'at',
'bd',
'ex',
'ld',
'md',
'qca',
'rw',
'rm',
'ss',
't'
],
ie: 'UTF-8',
oe: 'UTF-8',
otf: 1,
ssel: 0,
tsel: 0,
kc: 7,
q: text,
tk: (this.TKK = yield getToken(this.TKK))
}), res => {
let body = '';
res.on('data', chunk => (body += chunk));
res.on('error', reject);
res.on('end', () => {
let parsed;
try {
parsed = JSON.parse(body);
}
catch (error) {
return reject(new LinguistError('Failed to connect to google API', 'BAD_NETWORK'));
}
resolve({
text: parsed[0].reduce((str, arr) => (arr[0] ? str + arr[0] : str), ''),
from: {
language: {
didYouMean: parsed[2] !== parsed[8][0][0],
iso: parsed[8][0][0]
},
text: {
autoCorrected: parsed[7] ? parsed[7][5] : false,
value: parsed[7]
? parsed[7][0]
.replace(/<b><i>/g, '[')
.replace(/<\/i><\/b>/g, ']')
: '',
didYouMean: parsed[7] ? !parsed[7][5] : false
}
setTimeout(() => resolve(this._translate(text, options.from, options.to)), startat - Date.now());
});
}
_translate(query, from, to) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
https_1.get('https://translate.google.com/translate_a/single?' +
querystring_1.stringify({
client: 'gtx',
sl: from,
tl: to,
hl: to,
dt: ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'],
ie: 'UTF-8',
oe: 'UTF-8',
otf: 1,
ssel: 0,
tsel: 0,
kc: 7,
q: query,
tk: (this.TKK = yield getToken(this.TKK))
}), res => {
let body = '';
res.on('data', chunk => (body += chunk));
res.on('error', reject);
res.on('end', () => {
let parsed;
try {
parsed = JSON.parse(body);
}
catch (error) {
return reject(new LinguistError('Failed to connect to google API', 'BAD_NETWORK'));
}
resolve({
text: parsed[0].reduce((str, arr) => (arr[0] ? str + arr[0] : str), ''),
from: {
language: {
didYouMean: parsed[2] !== parsed[8][0][0],
iso: parsed[8][0][0]
},
raw: options.raw ? parsed : null
});
text: {
autoCorrected: parsed[7] ? parsed[7][5] : false,
value: parsed[7]
? parsed[7][0]
.replace(/<b><i>/g, '[')
.replace(/<\/i><\/b>/g, ']')
: '',
didYouMean: parsed[7] ? !parsed[7][5] : false
}
},
raw: parsed
});
});
}), startat - Date.now());
});
});
}));
}
}
exports.TranslateAPI = TranslateAPI;
import { TranslateAPI, LangFrom } from './components/translate';
declare const Default: TranslateAPI;
declare const translate: typeof Default.translate;
export { translate };
export { TranslateAPI, LangFrom } from './components/translate';
declare const _default: {
declare const translate: any;
declare const Linguister: typeof Default.translate & {
LangFrom: typeof LangFrom;
TranslateAPI: typeof TranslateAPI;
translate: (text: string, opts?: {
from?: string | undefined;
to?: string | undefined;
raw?: boolean | undefined;
}) => Promise<{
text: string;
from: {
language: {
didYouMean: boolean;
iso: "auto" | "so" | "hr" | "th" | "tr" | "af" | "sq" | "am" | "ar" | "hy" | "az" | "eu" | "be" | "bn" | "bs" | "bg" | "ca" | "ceb" | "ny" | "zh-cn" | "zh-tw" | "co" | "cs" | "da" | "nl" | "en" | "eo" | "et" | "tl" | "fi" | "fr" | "fy" | "gl" | "ka" | "de" | "el" | "gu" | "ht" | "ha" | "haw" | "iw" | "hi" | "hmn" | "hu" | "is" | "ig" | "id" | "ga" | "it" | "ja" | "jw" | "kn" | "kk" | "km" | "ko" | "ku" | "ky" | "lo" | "la" | "lv" | "lt" | "lb" | "mk" | "mg" | "ms" | "ml" | "mt" | "mi" | "mr" | "mn" | "my" | "ne" | "no" | "ps" | "fa" | "pl" | "pt" | "ma" | "ro" | "ru" | "sm" | "gd" | "sr" | "st" | "sn" | "sd" | "si" | "sk" | "sl" | "es" | "su" | "sw" | "sv" | "tg" | "ta" | "te" | "uk" | "ur" | "uz" | "vi" | "cy" | "xh" | "yi" | "yo" | "zu";
};
text: {
value: string;
autoCorrected: boolean;
didYouMean: boolean;
};
};
raw?: any[] | undefined;
}>;
LangFrom: typeof LangFrom;
};
export default _default;
export { translate, TranslateAPI, LangFrom };
export default Linguister;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const translate_1 = require("./components/translate");
exports.TranslateAPI = translate_1.TranslateAPI;
exports.LangFrom = translate_1.LangFrom;
const Default = new translate_1.TranslateAPI();
const translate = Default.translate.bind(Default);
exports.translate = translate;
var translate_2 = require("./components/translate");
exports.TranslateAPI = translate_2.TranslateAPI;
exports.LangFrom = translate_2.LangFrom;
exports.default = { TranslateAPI: translate_1.TranslateAPI, translate, LangFrom: translate_1.LangFrom };
const Linguister = translate;
Linguister.LangFrom = translate_1.LangFrom;
Linguister.TranslateAPI = translate_1.TranslateAPI;
exports.default = Linguister;
{
"name": "linguister",
"description": "Translate any text using the google translate api",
"version": "0.3.0",
"version": "0.3.1",
"main": "lib/index.js",

@@ -6,0 +6,0 @@ "scripts": {

@@ -1,2 +0,2 @@

# linguister [![Build Status](https://travis-ci.org/TomSputz/linguister.svg?branch=master)](https://travis-ci.org/TomSputz/linguister) [![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/TomSputz/linguister/badge.svg?branch=master)](https://coveralls.io/github/TomSputz/linguister?branch=master) [![Known Vulnerabilities](https://snyk.io/test/github/TomSputz/linguister/badge.svg)](https://snyk.io/test/npm/google-translate-api)
# Linguister [![Build Status](https://travis-ci.org/TomSputz/linguister.svg?branch=master)](https://travis-ci.org/TomSputz/linguister) [![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/TomSputz/linguister/badge.svg?branch=master)](https://coveralls.io/github/TomSputz/linguister?branch=master) [![Known Vulnerabilities](https://snyk.io/test/github/TomSputz/linguister/badge.svg)](https://snyk.io/test/npm/google-translate-api)

@@ -7,7 +7,7 @@ A **free** and **unlimited** API for Google Translate built in typescript :dollar::no_entry_sign:

- Auto language detection
- Full typing
- Spelling correction
- Language correction
- Fast and reliable – it uses the same servers that [translate.google.com](https://translate.google.com) uses
- Auto language detection
- Full typing
- Spelling correction
- Language correction
- Fast and reliable – it uses the same servers that [translate.google.com](https://translate.google.com) uses

@@ -20,18 +20,19 @@ ## Install

[//]: # "// TODO: Replace all of this"
## Usage
The module automatically exposes a default wrapper through the `translate` export, however if you want to spawn the requests differently, you can extend `Linguister.TranslateAPI`, calling `_translate` for the API request
From automatic language detection to English:
``` js
const translate = require('linguister');
```js
import {translate} from 'linguister'
translate('Ik spreek Engels', {to: 'en'}).then(res => {
console.log(res.text);
console.log(res.text)
//=> I speak English
console.log(res.from.language.iso);
console.log(res.from.language.iso)
//=> nl
}).catch(err => {
console.error(err);
});
console.error(err)
})
```

@@ -41,15 +42,15 @@

``` js
```js
translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
console.log(res.text);
console.log(res.text)
//=> Ik spreek Nederlands!
console.log(res.from.text.autoCorrected);
console.log(res.from.text.autoCorrected)
//=> true
console.log(res.from.text.value);
console.log(res.from.text.value)
//=> I [speak] Dutch!
console.log(res.from.text.didYouMean);
console.log(res.from.text.didYouMean)
//=> false
}).catch(err => {
console.error(err);
});
console.error(err)
})
```

@@ -59,16 +60,16 @@

``` js
```js
translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
console.log(res);
console.log(res.text);
console.log(res.text)
//=> Ik spea Nederlands!
console.log(res.from.text.autoCorrected);
console.log(res.from.text.autoCorrected)
//=> false
console.log(res.from.text.value);
console.log(res.from.text.value)
//=> I [speak] Dutch!
console.log(res.from.text.didYouMean);
console.log(res.from.text.didYouMean)
//=> true
}).catch(err => {
console.error(err);
});
console.error(err)
})
```

@@ -78,58 +79,44 @@

### translate(text, options)
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### text
#### Table of Contents
Type: `string`
- [LangFrom](#langfrom)
- [Parameters](#parameters)
- [TranslateAPI](#translateapi)
- [Parameters](#parameters-1)
- [translate](#translate)
- [Parameters](#parameters-2)
The text to be translated
### LangFrom
#### options
Returns the ISO 639-1 code of the desiredLang – if it is supported by Google Translate
Type: `object`
#### Parameters
##### from
- `query` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** – the name or the code of the desired language
Type: `string` Default: `auto`
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The ISO 639-1 code of the language
The `text` language. Must be `auto` or one of the codes/names (not case sensitive) contained in [languages.js](https://github.com/matheuss/google-translate-api/blob/master/languages.js)
### TranslateAPI
##### to
Used to spawn api requests. Will handle a ratelimit
Type: `string` Default: `en`
#### Parameters
The language in which the text should be translated. Must be one of the codes/names (not case sensitive) contained in [languages.js](https://github.com/matheuss/google-translate-api/blob/master/languages.js).
- `ratelimit` (optional, default `1000`)
##### raw
#### translate
Type: `boolean` Default: `false`
Translate a string using google's translate API
If `true`, the returned object will have a `raw` property with the raw response (`string`) from Google Translate.
##### Parameters
### Returns an `object`
- `text` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The text to translate
- `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Options for the request (optional, default `{}`)
- `opts.from` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** The language to translate from
- `opts.to` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** The language to translate to
- `text` *(string)* – The translated text.
- `from` *(object)*
- `language` *(object)*
- `didYouMean` *(boolean)* - `true` if the API suggest a correction in the source language
- `iso` *(string)* - The [code of the language](https://github.com/matheuss/google-translate-api/blob/master/languages.js) that the API has recognized in the `text`
- `text` *(object)*
- `autoCorrected` *(boolean)* – `true` if the API has auto corrected the `text`
- `value` *(string)* – The auto corrected `text` or the `text` with suggested corrections
- `didYouMean` *(booelan)* – `true` if the API has suggested corrections to the `text`
- `raw` *(string)* - If `options.raw` is true, the raw response from Google Translate servers. Otherwise, `''`.
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;Translation>** The translation response
Note that `res.from.text` will only be returned if `from.text.autoCorrected` or `from.text.didYouMean` equals to `true`. In this case, it will have the corrections delimited with brackets (`[ ]`):
``` js
translate('I spea Dutch').then(res => {
console.log(res.from.text.value);
//=> I [speak] Dutch
}).catch(err => {
console.error(err);
});
```
Otherwise, it will be an empty `string` (`''`).
## Coverage

@@ -136,0 +123,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc