googletrans
Free and Unlimited Google translate API for node.js
![npm](https://img.shields.io/npm/v/googletrans)
Features
- Fast and reliable
- Batch translation
- Auto language detection
- Spelling correction
- HTTP/2 support
- Connection pooling
Installing
Using npm
npm i -S googletrans
Using yarn
yarn add googletrans
CommonJS usage
To gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require()
use the following approach:
const tr = require("googletrans").default;
Super simple to use
Basic Usage
const tr = require("googletrans").default;
tr("vue")
.then(function (result) {
console.log(result.text);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
async function translation() {
try {
const result = await tr("vue");
console.log(result.text);
console.log(result.src);
} catch (error) {
console.log(error);
}
}
Batch translation
An array can be used to translate a batch of texts.
tr(["Saluton", "Mondo"])
.then(function (result) {
console.log(result.textArray);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
tr(["Hello", "world"], "de")
.then(function (result) {
console.log(result.textArray);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
NOTE: The first element of the text array can not empty string.
Spelling correction
If the API suggests a correction text, hasCorrectedText
will equals to true
.
In this case, correction text will have the corrections delimited with brackets ([ ]
), correctedText
is the crrection text.
tr("I spea English", "nl")
.then(function (result) {
console.log(result.text);
console.log(result.hasCorrectedText);
console.log(result.correctedText);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
More options
from
The source language you want to translate. (Default: auto)to
The language you want to translate into.(Default: en)tld
The google translate domain name. In this case, tld:"co.jp"
it will be uses translate.google.co.jp
tr("Hello", { from: "en", to: "ja", tld: "co.jp" })
.then(function (result) {
console.log(result.text);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
Language correction
If the source language is not right, hasCorrectedLang
will equal to true
.
tr("Hero", { from: "pt", to: "nl" })
.then(function (res) {
console.log(res.hasCorrectedLang);
console.log(res.src);
})
.catch(function (err) {
console.log(err);
});
Languages support
The support languages. You can use the short name or the full name.
tr("koa", "en")
.then(function (result) {
console.log(result.text);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
tr("koa", "English")
.then(function (result) {
console.log(result.text);
console.log(result.src);
})
.catch(function (error) {
console.log(error);
});
TypeScript
googletrans includes TypeScript definitions.
import tr from "googletrans";
Method
const tr = require("googletrans").default;
tr(text, options)
Arguments
- text - The text to be translated.
- options - The translation options. If the param is string, mean the language you want to translate into.If the param is object, it can set more options.
Options
"en";
{
to: "en";
from: "fr";
tld: "co.jp";
}
Returns: Result Schema
The result of a request contains the following information.
Result {
text: string;
textArray: string[];
pronunciation: string;
hasCorrectedLang: boolean;
src: string;
hasCorrectedText: boolean;
correctedText: string;
translations: [];
raw: [];
}
NOTE
DISCLAIMER: this is an unofficial library using the web API of Google Translate and also is not associated with Google.
- The maximum character limit on a single text is 15k.
- Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times (so please use this library if you don't care about stability).
- If you want to use a stable API, I highly recommend you to use Google's official translate API.
License
MIT
Copyright (c) 2020-present, Darin Lo