# google-translate-api-browser

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

Latest version **5.0.0** (published 2024-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install google-translate-api-browser
pnpm add google-translate-api-browser
yarn add google-translate-api-browser
bun add google-translate-api-browser
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2024-03-23 |
| First published | 2019-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 107.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 222 |
| Author | Vitalij Nykyforenko |
| Maintainers | nvjrsgu |
| Keywords | translate, translator, google, translate, api, free, language |

## Links

- npm: https://www.npmjs.com/package/google-translate-api-browser
- Repository: https://github.com/cjvnjde/google-translate-api-browser
- Homepage: https://github.com/cjvnjde/google-translate-api-browser#readme
- Issues: https://github.com/cjvnjde/google-translate-api-browser/issues
- npm.io page: https://npm.io/package/google-translate-api-browser

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 5.0.0 (latest) — 2024-03-23
- 4.2.3 (alpha) — 2023-12-02
- 4.3.2 — 2024-03-10
- 4.3.1 — 2024-03-10
- 4.3.0 — 2024-02-25
- 4.2.7 — 2023-12-09
- 4.2.4 — 2023-12-02
- 4.2.2 — 2023-12-02
- 4.2.1 — 2023-12-02
- 4.2.0 — 2023-12-02
- 4.1.9 — 2023-11-26
- 4.1.8 — 2023-11-26
- 4.1.7 — 2023-11-26
- 4.1.6 — 2023-11-26
- 4.1.5 — 2023-11-26
- … 40 more at https://npm.io/package/google-translate-api-browser/versions

## README

# Google translate api browser

[![NPM Version](https://img.shields.io/npm/v/google-translate-api-browser)](https://www.npmjs.com/package/google-translate-api-browser)

## Install

```bash
npm install google-translate-api-browser
```

For cross origin requests it uses [cors-anywhere](https://github.com/Rob--W/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

```javascript
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

```javascript
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](https://codesandbox.io/p/sandbox/google-translate-api-node-zgg694)
[Link to codesandbox with custom fetch function](https://codesandbox.io/p/sandbox/google-translate-api-node-custom-kr29rz)

#### For node (esm)

[Link to codesandbox](https://codesandbox.io/p/sandbox/google-translate-api-node-esm-3x22v2)

## API

### isSupported(lang: string): boolean
Verifies if the selected language is supported by Google Translate.

### translate(text: string, options: Partial<TranslateOptions>): Promise<TranslationResult>

#### text
The text to be translated

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

---
_Source: https://npm.io/package/google-translate-api-browser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
