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
- Host: GitHub
- URL: https://github.com/chewawi/microsoft-translate-api
- Owner: Chewawi
- License: mit
- Created: 2024-04-07T08:07:30.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-15T21:28:21.000Z (over 1 year ago)
- Last Synced: 2025-03-12T14:48:43.041Z (over 1 year ago)
- Topics: microsoft, translation
- Language: TypeScript
- Homepage: https://npmjs.com/package/microsoft-translate-api
- Size: 113 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Microsoft Translate API
[](https://www.npmjs.org/package/microsoft-translate-api)
[](https://github.com/chewawi/microsoft-translate-api/actions/workflows/autotest.yml)
[](https://github.com/chewawi/microsoft-translate-api/actions/workflows/transpile.yml)
[](https://npmcharts.com/compare/microsoft-translate-api?minimal=true)
[](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/)