An open API service indexing awesome lists of open source software.

https://github.com/chewawi/microsoft-translate-api

A simple, powerful and free API for Microsoft Translator for Node.js
https://github.com/chewawi/microsoft-translate-api

microsoft translation

Last synced: 8 months ago
JSON representation

A simple, powerful and free API for Microsoft Translator for Node.js

Awesome Lists containing this project

README

          

# Microsoft Translate API

[![NPM version](https://img.shields.io/npm/v/microsoft-translate-api.svg?style=flat)](https://www.npmjs.org/package/microsoft-translate-api)
[![Auto Test](https://github.com/chewawi/microsoft-translate-api/actions/workflows/autotest.yml/badge.svg)](https://github.com/chewawi/microsoft-translate-api/actions/workflows/autotest.yml)
[![Build](https://github.com/chewawi/microsoft-translate-api/actions/workflows/transpile.yml/badge.svg)](https://github.com/chewawi/microsoft-translate-api/actions/workflows/transpile.yml)
[![NPM Downloads](https://img.shields.io/npm/dm/microsoft-translate-api.svg)](https://npmcharts.com/compare/microsoft-translate-api?minimal=true)
[![License](https://img.shields.io/npm/l/microsoft-translate-api.svg)](https://github.com/tuusuario/microsoft-translate-api/blob/master/LICENSE)

A stable and powerful zero-dependency **free** translator for [Microsoft Translator](https://learn.microsoft.com/azure/ai-services/translator/) designed for Node.js.

## Install

NPM

```sh
[npm | bun | pnpm | yarn] install microsoft-translate-api
```

### Basic Usage

#### Translate from Auto-Detected Language to Another Language

```javascript
const { translate } = require('microsoft-translate-api')

translate('你好,很高兴认识你!', null, 'en').then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
```

Translation result

```json
[
{
"detectedLanguage": {
"language": "zh-Hans",
"score": 1
},
"translations": [
{
"text": "Hello, nice to meet you!",
"to": "en"
}
]
}
]
```

#### Translate from Auto-Detected Language to Multiple Languages

```javascript
const { translate } = require('microsoft-translate-api')

translate('你好,很高兴认识你!', null, ['en', 'ja']).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
```

Translation result

```json
[
{
"detectedLanguage": {
"language": "zh-Hans",
"score": 1
},
"translations": [
{
"text": "Hello, nice to meet you!",
"to": "en"
},
{
"text": "こんにちは、はじめまして!",
"to": "ja"
}
]
}
]
```

#### Translate HTML text

```javascript
const { translate } = require('microsoft-translate-api')

const htmlText = `

This will not be translated.

This will be translated.

`;
translate(htmlText, null, 'zh-Hans', {
translateOptions: {
// Explicitly set textType as `html`. Defaults to `plain`.
textType: 'html'
}
}).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
```

Translation result

```json
[
{
"detectedLanguage": {
"language": "en",
"score": 1
},
"translations": [
{
"text": "

This will not be translated.
\n
这将被翻译。
",
"to": "zh-Hans"
}
]
}
]
```


### Optional Translation Options

> [Reference](https://learn.microsoft.com/azure/ai-services/translator/reference/v3-0-translate#optional-parameters)

```typescript
interface TranslateOptions {
translateOptions?: Record;
authenticationHeaders?: Record;
userAgent?: string;
fetchOptions?: RequestInit;
}
```

### Full Translation Results

> [Reference](https://learn.microsoft.com/azure/ai-services/translator/reference/v3-0-translate#response-body)

```typescript
interface TranslationResult {
translations: {
text: string;
to: string;
sentLen?: {
srcSentLen: number[];
transSentLen: number[];
};
transliteration?: {
script: string;
text: string;
};
alignment?: object;
}[];
detectedLanguage?: {
language: string;
score: number;
};
}
```


### Supported Languages

Refer to [langs](src/lib/langs.ts).

### Service Limits

[Character and array limits per request](https://learn.microsoft.com/azure/ai-services/translator/service-limits#character-and-array-limits-per-request)


> [!NOTE]
> Note that the correction service is not available.


### Use Paid Service With Your Private Keys

```javascript
const { translate } = require('microsoft-translate-api')

translate('你好,很高兴认识你!', null, 'en', {
authenticationHeaders: {
// Use private subscription key
'Ocp-Apim-Subscription-Key': 'YOUR KEY',
// Or use a JWT token
'Authorization': 'YOUR TOKEN'
}
}).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
```

See also [Authentication](https://learn.microsoft.com/azure/ai-services/translator/reference/v3-0-reference#authentication)

> [!NOTE]
> Note that using your private keys, the translator will skip to fetch the free authorization and you will have to check if the authorization is expired by yourself.


## Thanks
>
> [bing-translate-api](https://github.com/plainheart/bing-translate-api/)