Coder Social home page Coder Social logo

hibar3 / google-translate-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vitalets/google-translate-api

0.0 0.0 0.0 746 KB

A free and unlimited API for Google Translate :dollar: :no_entry_sign:

License: MIT License

Shell 2.37% JavaScript 9.73% TypeScript 66.01% HTML 21.90%

google-translate-api's Introduction

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.

DISCLAIMER! To be 100% legal please use official Google Translate API. This project is mainly for pet projects and prototyping.

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 from the same IP address, you will get a TooManyRequestsError (code 429). You can use proxy to bypass it:

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:

  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

translate(text: string, options?: Options): Promise<Response>

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.

Related projects

License

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

Buy Me A Coffee

google-translate-api's People

Contributors

vitalets avatar matheuss avatar rawr51919 avatar vkedwardli avatar jukeboxrhino avatar noname001 avatar olavoparno avatar adam-clrk avatar aidanwelch avatar arthurlacoste avatar zihadmahiuddin avatar dougley avatar tgyou avatar tobiasmuehl avatar ciiiii avatar usrtax avatar xfslove avatar yuler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.