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

@vitalets/google-translate-api

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vitalets/google-translate-api

A free and unlimited API for Google Translate

  • 9.0.0-1
  • next
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
60K
decreased by-3.37%
Maintainers
1
Weekly downloads
 
Created
Source

google-translate-api

Actions Status NPM version license

A free and unlimited API for Google Translate for Node.js.

In version 9+ library was fully rewritten. For legacy documentation please see legacy branch.

Contents

Features

Installation

npm install @vitalets/google-translate-api

Usage

Node.js

import { translate } from '@vitalets/google-translate-api';

const { text } = await translate('Привет, мир! Как дела?', { to: 'en' });

console.log(text) // => 'Hello World! How are you?'

React-native

Since react-native has full support of fetch API translation works the same way as in Node.js.

Web pages

This library does not work inside web pages because translate.google.com does not provide CORS headers allowing access from other domains.

Browser extensions

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:

    + "host_permissions": [
    +    "https://translate.google.com/"
    +  ]
    
  2. Import translate as usual in background or popup script:

    // background.js
    import { translate } from '@vitalets/google-translate-api';
    
    const { text } = await translate('Привет мир');
    
    console.log(text);
    
  3. Bundle code (for example with webpack):

    // 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, you can get a TooManyRequestsError (code 429). You can use proxy to bypass it:

import { translate } from '@vitalets/google-translate-api';
import createHttpProxyAgent from 'http-proxy-agent';

const agent = createHttpProxyAgent('http://103.152.112.162:80');
const { text } = await translate('Привет, мир!', {
  to: 'en',
  fetchOptions: { agent },
});

Available proxy list you can find here.

Common pattern for selecting proxy is following:

  try {
    const { text } = await translate('Привет, мир!', {
      to: 'en',
      fetchOptions: { agent },
    });
  } catch (e) {
    if (e.name === 'TooManyRequestsError') {
      // retry with another proxy agent
    }
  }

API

tbd

License

MIT © Matheus Fernandes, forked and maintained by Vitaliy Potapov.

Buy Me A Coffee

Keywords

FAQs

Package last updated on 27 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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