🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

google-translate-api-browser

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

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

5.0.0
latest
Source
npm
Version published
Weekly downloads
12K
-14.72%
Maintainers
1
Weekly downloads
 
Created
Source

Google translate api browser

NPM Version

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 { translate } from "google-translate-api-browser";

translate("Je ne mangé pas six jours", { to: "en", corsUrl: "http://cors-anywhere.herokuapp.com/" })
  .then(res => {
    // I do not eat six days
    console.log(res.text)
  })
  .catch(err => {
    console.error(err);
  });

For node (commonjs)

You don't need to use CORS for node

const { translate } = require('google-translate-api-browser');

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);
  });

Link to codesandbox Link to codesandbox with custom fetch function

For node (esm)

Link to codesandbox

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 = {
  rpcids: string;
  from: LangKey;
  to: LangKey;
  hl: LangKey;
  tld: string;
  raw: boolean;
}
example
const options = {
  rpcids: 'MkEWBc',
  from: 'ua',
  to: 'en',
  hl: 'en',
  tld: 'com',
  raw: true,
}

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; 
      value: string;
      didYouMean: string;
    }
  };
  raw?: any;
}

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.

Keywords

translate

FAQs

Package last updated on 23 Mar 2024

Did you know?

Socket

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