Socket
Socket
Sign inDemoInstall

google-translate-api-browser

Package Overview
Dependencies
0
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    google-translate-api-browser

A free and unlimited API for Google Translate that works in browser


Version published
Maintainers
1
Install size
46.3 kB
Created

Readme

Source

Google translate api browser

npm - 3.0.1

Based on google-translate-api and google-translate-token

Install

npm install google-translate-api-browser

For cross origin requests it uses cors-anywhere . You can use public cors-anywhere server https://cors-anywhere.herokuapp.com/ or set up your own. By default it does not use proxying.

Examples

For browser
import { setCORS } from "google-translate-api-browser";
// setting up cors-anywhere server address
const translate = setCORS("http://cors-anywhere.herokuapp.com/");
/*
// or
import translate, { setCORS } from "google-translate-api-browser";
setCORS("http://cors-anywhere.herokuapp.com/");
*/
translate("Je ne mangé pas six jours", { to: "en" })
  .then(res => {
    // I do not eat six days
    console.log(res.text)
  })
  .catch(err => {
    console.error(err);
  });
For node

You don't need to use CORS for node

const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const https = require('https');

const url = generateRequestUrl('Je ne mangé pas six jours', { to: "en" });

https.get(url, (resp) => {
  let data = '';

  resp.on('data', (chunk) => {
    data += chunk;
  });

  resp.on('end', () => {
    console.log(normaliseResponse(JSON.parse(data)));
  });
}).on("error", (err) => {
  console.log("Error: " + err.message);
});

API

isSupported(lang: string): boolean

Verifies if the selected language is supported by Google Translate.

translate(text: string, options: Partial): Promise

text

The text to be translated

options
type TranslateOptions = {
  client: 'gtx' | 'webapp';
  from: LangKey;
  to: LangKey;
  hl: LangKey;
  raw: boolean;
  tld: string;
}
example
const options = {
  client: 'gtx',
  from: 'ua',
  to: 'en',
  hl: 'en',
  raw: false,
  tld: 'com',
}
returns
type TranslationResult = {
  text: string; // The translated text.
  pronunciation: string;
  from: {
    language: {
      didYouMean: boolean; // `true` if the API suggest a correction in the source language
      iso: string; // The code of the language that the API has recognized in the `text`
    };
    text: {
      autoCorrected: boolean; // `true` if the API has auto corrected the `text`
      value: string; // The auto corrected `text` or the `text` with suggested corrections
      didYouMean: boolean; // `true` if the API has suggested corrections to the `text`
    }
  };
  raw?: any; // If `options.raw` is true, the raw response from Google Translate servers
}

Note that from.text will only be returned if from.text.autoCorrected or from.text.didYouMean equals to true. In this case, it will have the corrections delimited with brackets ([ ]):

normaliseResponse(body: any, raw = false): TranslationResult

Formats the google translate response.

generateRequestUrl(text: string, options: Partial<Omit<TranslateOptions, 'raw'>>): string

Generates a url to google translate api.

setCORS(CORSURL: string): translate

Keywords

FAQs

Last updated on 10 Feb 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc