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

googletrans2

v1.0.2

Published

Free and Unlimited Google translate API for node.js

Downloads

2

Readme

googletrans

Free and Unlimited Google translate API for node.js

Build Status Coverage Status install size GitHub npm

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;

// Promise
tr("vue")
  .then(function (result) {
    console.log(result.text); // view
    console.log(result.src); // fr
  })
  .catch(function (error) {
    console.log(error);
  });

// Want to use async/await?
async function translation() {
  try {
    const result = await tr("vue");
    console.log(result.text); // view
    console.log(result.src); // fr
  } catch (error) {
    console.log(error);
  }
}

Batch translation

An array can be used to translate a batch of texts.

// eo => en
tr(["Saluton", "Mondo"])
  .then(function (result) {
    console.log(result.textArray); // [ 'Hello', 'world' ]
    console.log(result.src); // eo
  })
  .catch(function (error) {
    console.log(error);
  });

// en => de
tr(["Hello", "world"], "de")
  .then(function (result) {
    console.log(result.textArray); // [ 'Hallo', 'Welt' ]
    console.log(result.src); // en
  })
  .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.

// en => nl
tr("I spea English", "nl")
  .then(function (result) {
    console.log(result.text); // ik spreek Engels
    console.log(result.hasCorrectedText); // true
    console.log(result.correctedText); // I [speak] English
    console.log(result.src); // en
  })
  .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
// en => ja
tr("Hello", { from: "en", to: "ja", tld: "co.jp" })
  .then(function (result) {
    console.log(result.text); // こんにちは
    console.log(result.src); // en
  })
  .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); // true
    console.log(res.src); // en
  })
  .catch(function (err) {
    console.log(err);
  });

Languages support

The support languages. You can use the short name or the full name.

// short name
tr("koa", "en")
  .then(function (result) {
    console.log(result.text); // also
    console.log(result.src); // mg
  })
  .catch(function (error) {
    console.log(error);
  });

// full name
tr("koa", "English")
  .then(function (result) {
    console.log(result.text); // also
    console.log(result.src); // mg
  })
  .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

// string
"en";

// object
{
  // The language you want to translate into.(Default: en)
  to: "en";
  // The source language you want to translate. (Default: auto)
  from: "fr";
  // The google translate domain name
  tld: "co.jp";
}

Returns: Result Schema

The result of a request contains the following information.

Result {
  // the translated text.
  text: string;

  // array of the translated text.
  textArray: string[];

  // pronunciation
  pronunciation: string;

  // has correct source language?
  hasCorrectedLang: boolean;

  // source language
  src: string;

  // has correct source text?
  hasCorrectedText: boolean;

  // correct source text
  correctedText: string;

  // multiple translations
  translations: [];

  // the raw response from Google Translate servers.
  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