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

node-gtrans

v1.6.1

Published

Google translate web extension API in readable format

Downloads

5

Readme

node-gtrans

npm version npm downloads package size vulnerabilities

A simple yet complete Google translate web extension API in readable format

Features

  • No API Token required
  • Free & Unlimited translations
  • Allow a very long text (5000+ characters still works)
  • Almost anything from google translate website

Installing

Using npm:

$ npm install node-gtrans

using yarn:

$ yarn add node-gtrans

Example usage

Basic usage:

const gtrans = require("node-gtrans");

const words = "good morning";
// using promises:
gtrans(words, { to: "id" })
  .then((res) => console.log(res.data))
  .catch((e) => e);

// using async/await:
(async () => {
  const translated = await gtrans(words, { to: "id" }).then((res) => res.data);
  // const { data } = await gtrans(text, { to: 'id' }); // or this
  console.log(translated);
})();

Both will output this object:

{
  translated: 'Selamat pagi',
  sourceText: 'good morning',
  from: 'English',
  to: 'Indonesian',
  isCorrected: false,
  pronunciation: 'ɡo͝od ˈmôrniNG',
  definitions: { exclamation: [ [Object] ] }
}

note: res is an axios response object in case if you want to use status text, header, etc.

API

gtrans(text, [options])

Available options are:

  • from? - The language id where the language comes from the. Leave it blank for auto detection.

  • to - The language id where the text will be translated to, this option is required.

  • interfaceLang? - The language id where the interface such as verbs, noun in object key will be translated. For example in Indonesian, verbs is translated to verba, noun to nomina, etc. Default to 'en' or English.

    // example output from:
    // await gtrans('run', { to: 'id', interfaceLang: 'id' }).then((res) => res.data);
    {
      translated: 'Lari',
      sourceText: 'run',
      translations: {
        verba: [ // from verb to verba
          [Object], [Object],
        ],
        nomina: [ // from noun to nomina
          [Object], [Object],
        ]
      },
      synonyms: {
        verba: [
          [Array], [Array], [Array],
        ],
        nomina: [
          [Array], [Array],
        ]
      },
      definitions: {
        verba: [
          [Object], [Object],
        ],
        nomina: [
          [Object], [Object],
        ]
      }
    }
  • htmlTag? - A boolean state whether the text that contain html tag will be converted to markdown format. html tag appears in examples and corrected. Default to false.

    // example output from:
    // await gtrans('ssprint', { to: 'id', htmlTag: true }).then((res) => res.data);
    {
      translated: 'lari cepat',
      sourceText: 'sprint',
      isCorrected: true,
      corrected: '<b><i>sprint</i></b>', // bold and italic tags, if false it'll be ***sprint***
      pronunciation: 'sprint',
      translations: { noun: [ [Object], [Object] ], verb: [ [Object] ] },
      synonyms: { verb: [ [Array], [Array], [Array] ] },
      definitions: { verb: [ [Object] ], noun: [ [Object], [Object] ] },
      examples: [
        'a <b>sprint</b> planning session', // bold tags in all instances of examples list
        'MacFarlane won the 1,500m with a fine <b>sprint</b> finish',
        'Greg broke into a <b>sprint</b>', // if false it'll be **sprint**
        'team members discuss issues with each other at the end of every <b>sprint</b>',
        'the 100 meters <b>sprint</b>'
      ]
    }
  • resolve? - A boolean state whether the language Id is converted to full language name, e.g. from 'en' to 'English'. Default to true.

    // if true
    {
      translated: 'lari cepat',
      sourceText: 'sprint',
      from: 'English',
      to: 'Indonesian',
    }
    
    // else
    {
      translated: 'lari cepat',
      sourceText: 'sprint',
      from: 'en',
      to: 'id',
    }
  • axiosConfig? - config options for making requests from axios request config. Default to undefined.

    // example usage
    gtrans("walk", {
      to: "id",
      axiosConfig: {
        proxy: {
          host: "127.0.0.1",
          port: 8000,
          auth: {
            username: "mikeymike",
            password: "rapunz3l",
          },
        },
        timeout: 1000,
        responseEncoding: "utf8",
      },
    });
  • contents? - option to specify data to be returned. It's value for dt query parameter, which is explained in query paramters

See available language id at google translate docs or in resolver.ts

See possible property in output.js or at type definition

See implementation example in example.js

note: options that end with "?" are optional.

gtrans.validateLangId(languageCode)

Check if given string is a valid language code.
return resolved language if valid else return false

gtrans.getFixedT(languageCode)

Inspired by i18next.getFixedT function, it takes language code and return a gtrans default function but without specifying option.

On the returned function you can use it like default function as shown down below.

(async () => {
  const jp = gtrans.getFixedT('ja');
  console.log(await jp('I love you'));
  console.log(await jp(`No, I don't`));
})();
// output:
// > わたしは、あなたを愛しています
// > いいえ、しません

See also

https://www.labnol.org/code/19909-google-translate-api
https://letconex.blogspot.com/2017/12/google-translate-query-parameters.html
https://stackoverflow.com/questions/26714426/what-is-the-meaning-of-google-translate-query-params