A free and unlimited API for Google Translate
npm install @vitalets/google-translate-apiA free and unlimited API for Google Translate for Node.js.
In version 9+ library was fully rewritten. For legacy documentation please see legacy branch.
> DISCLAIMER!
To be 100% legal please use official Google Translate API. This project is mainly for pet projects and prototyping.
- Features
- Installation
- Usage
* Node.js
* React-native
* Web pages
* Browser extensions
- Limits
- API
+ Parameters
+ Response
- Related projects
- License
* auto language detection
* all Google Translate languages supported
* react-native supported
* transliteration
npm install @vitalets/google-translate-api
`Usage
$3
`ts
import { translate } from '@vitalets/google-translate-api';const { text } = await translate('Привет, мир! Как дела?', { to: 'en' });
console.log(text) // => 'Hello World! How are you?'
`$3
Since react-native has full support of fetch API translation works the same way as in Node.js.$3
This library does not work inside web pages because translate.google.com does not provide CORS headers allowing access from other domains.$3
Although library does not work in regular web pages it can be used in browser extensions.
Extensions background and popup pages are not limited with same origin policy. To use translation API you should do the following:1. Add host permissions to
manifest.json:
`diff
+ "host_permissions": [
+ "https://translate.google.com/"
+ ]
`2. Import
translate as usual in background or popup script:
`js
// background.js
import { translate } from '@vitalets/google-translate-api'; const { text } = await translate('Привет мир');
console.log(text);
`3. Bundle code (for example with
webpack):
`js
// webpack.config.js
module.exports = {
mode: 'development',
entry: './background.js',
output: {
filename: 'bundle.js',
},
};
`Limits
Google Translate has request limits. If too many requests are made from the same IP address, you will get a TooManyRequestsError (code 429). You can use proxy to bypass it:`ts
import { translate } from '@vitalets/google-translate-api';
import { HttpProxyAgent } from 'http-proxy-agent';const agent = new HttpProxyAgent('http://103.152.112.162:80');
const { text } = await translate('Привет, мир!', {
to: 'en',
fetchOptions: { agent },
});
`
See [examples/with-proxy.ts] for more details.> Available proxy list you can find here (with
anonymous in *Anonymity and yes in Google columns).Common pattern for selecting proxy is following:
`ts
try {
const { text } = await translate('Привет, мир!', {
to: 'en',
fetchOptions: { agent },
});
} catch (e) {
if (e.name === 'TooManyRequestsError') {
// retry with another proxy agent
}
}
`
See #107 for discussion.API
`ts
translate(text: string, options?: Options): Promise
`#### Parameters
text (string)* - The text to be translated
options (object)*
- from (string) - The language of text. Must be auto or one of the supported languages. Default: auto
- to (string) - The language in which the text should be translated. Must be one of the supported languages. Default: auto
- host (string) - Google translate host to be used in API calls. Default: translate.google.com
- fetchOptions (object) - Additional fetch options passed into request.#### Response
text (string)* – The translated text.
raw` (object)* - Raw responspe from the API. Contains sentences, detected original language and transliteration. Example response.