npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vitalets/google-translate-api

v9.2.0

Published

A free and unlimited API for Google Translate

Downloads

249,719

Readme

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 createHttpProxyAgent from 'http-proxy-agent';

const agent = createHttpProxyAgent('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 yes in Google column).

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.