Skip to content

Commit 5090d99

Browse files
authored
Merge pull request #35 from plainheart/v4-MET
feat: new MET mode for Microsoft Translator
2 parents 4143a29 + 55144a9 commit 5090d99

File tree

14 files changed

+823
-29
lines changed

14 files changed

+823
-29
lines changed

.github/workflows/autotest.yml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,12 @@ jobs:
1212
if: ${{ github.repository_owner == 'plainheart' }}
1313
runs-on: ubuntu-latest
1414

15-
strategy:
16-
matrix:
17-
node-version: ['20.x']
18-
1915
steps:
2016
- uses: actions/checkout@v4
2117

22-
- name: Use Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v4
24-
with:
25-
node-version: ${{ matrix.node-version }}
26-
registry-url: https://registry.npmjs.org/
27-
2818
- name: Cache node modules
2919
id: cache
30-
uses: actions/cache@v3
20+
uses: actions/cache@v4
3121
env:
3222
cache-name: cache-node-modules
3323
with:

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
<!-- AUTO-GENERATED. SEE scripts/README.tpl.md FOR ORIGINAL TEMPLATE -->
2-
31
# bing-translate-api
42
[![NPM version](https://img.shields.io/npm/v/bing-translate-api.svg?style=flat)](https://www.npmjs.org/package/bing-translate-api)
53
[![Auto Test](https://github.com/plainheart/bing-translate-api/actions/workflows/autotest.yml/badge.svg)](https://github.com/plainheart/bing-translate-api/actions/workflows/autotest.yml)
64
[![NPM Downloads](https://img.shields.io/npm/dm/bing-translate-api.svg)](https://npmcharts.com/compare/bing-translate-api?minimal=true)
75
[![License](https://img.shields.io/npm/l/bing-translate-api.svg)](https://github.com/plainheart/bing-translate-api/blob/master/LICENSE)
86

9-
A **simple** and **free** API for [Bing Translator](https://bing.com/translator) for Node.js.
7+
A **simple** and **free** API for [Bing Translator](https://bing.com/translator) and [Microsoft Translator](https://learn.microsoft.com/azure/ai-services/translator/) for Node.js.
108

11-
## Install
9+
## Install
1210

1311
```
1412
npm install bing-translate-api
1513
```
1614

1715
## Usage
1816

17+
### Microsoft Translator <sup>🆕</sup>
18+
19+
Added since v4.0.0. More stable and powerful. Both free service and paid service are supported. Feel free to take a try!
20+
21+
Read the [README](./src/met/README.md) for more details.
22+
23+
### Bing Translator
24+
1925
From auto-detected language to English:
2026

2127
```js

index.d.ts

Lines changed: 192 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// AUTO-GENERATED. SEE scripts/index.tpl.d.ts FOR ORIGINAL TYPINGS
22

3-
import type { Agents } from 'got';
3+
import type {
4+
Agents,
5+
Options as GotOptions
6+
} from 'got';
47

58
export interface TranslationResult {
69
/**
@@ -60,7 +63,7 @@ export declare function translate(
6063
raw?: boolean,
6164
userAgent?: string,
6265
proxyAgents?: Agents
63-
): Promise<TranslationResult>;
66+
): Promise<TranslationResult | undefined>;
6467

6568
export declare namespace lang {
6669
const LANGS: {
@@ -203,3 +206,190 @@ export declare namespace lang {
203206
function isSupported(lang: string): boolean;
204207
function isCorrectable(lang: string): boolean;
205208
}
209+
210+
export declare namespace MET {
211+
interface MetTranslateOptions {
212+
translateOptions?: Record<string, object>;
213+
authenticationHeaders?: Record<string, string>;
214+
userAgent?: string;
215+
gotOptions?: GotOptions
216+
}
217+
218+
/**
219+
* See https://learn.microsoft.com/azure/ai-services/translator/reference/v3-0-translate#response-body for full result structure
220+
*/
221+
interface MetTranslationResult {
222+
translations: {
223+
text: string;
224+
to: string;
225+
sentLen?: {
226+
srcSentLen: number[];
227+
transSentLen: number[];
228+
};
229+
transliteration?: {
230+
script: string;
231+
text: string;
232+
};
233+
alignment?: object;
234+
}[];
235+
detectedLanguage?: {
236+
language: string;
237+
score: number;
238+
}
239+
}
240+
241+
/**
242+
* @param text content to be translated
243+
* @param source language code
244+
* @param to target language code(s). `en` by default.
245+
* @param options optional translate options
246+
*/
247+
declare function translate(
248+
text: string | string[],
249+
from: string | null | undefined,
250+
to: string | string[],
251+
options?: MetTranslateOptions
252+
): Promise<MetTranslationResult[] | undefined>;
253+
254+
namespace lang {
255+
const LANGS: {
256+
'af': 'Afrikaans',
257+
'am': 'Amharic',
258+
'ar': 'Arabic',
259+
'as': 'Assamese',
260+
'az': 'Azerbaijani',
261+
'ba': 'Bashkir',
262+
'bg': 'Bulgarian',
263+
'bho': 'Bhojpuri',
264+
'bn': 'Bangla',
265+
'bo': 'Tibetan',
266+
'brx': 'Bodo',
267+
'bs': 'Bosnian',
268+
'ca': 'Catalan',
269+
'cs': 'Czech',
270+
'cy': 'Welsh',
271+
'da': 'Danish',
272+
'de': 'German',
273+
'doi': 'Dogri',
274+
'dsb': 'Lower Sorbian',
275+
'dv': 'Divehi',
276+
'el': 'Greek',
277+
'en': 'English',
278+
'es': 'Spanish',
279+
'et': 'Estonian',
280+
'eu': 'Basque',
281+
'fa': 'Persian',
282+
'fi': 'Finnish',
283+
'fil': 'Filipino',
284+
'fj': 'Fijian',
285+
'fo': 'Faroese',
286+
'fr': 'French',
287+
'fr-CA': 'French (Canada)',
288+
'ga': 'Irish',
289+
'gl': 'Galician',
290+
'gom': 'Konkani',
291+
'gu': 'Gujarati',
292+
'ha': 'Hausa',
293+
'he': 'Hebrew',
294+
'hi': 'Hindi',
295+
'hne': 'Chhattisgarhi',
296+
'hr': 'Croatian',
297+
'hsb': 'Upper Sorbian',
298+
'ht': 'Haitian Creole',
299+
'hu': 'Hungarian',
300+
'hy': 'Armenian',
301+
'id': 'Indonesian',
302+
'ig': 'Igbo',
303+
'ikt': 'Inuinnaqtun',
304+
'is': 'Icelandic',
305+
'it': 'Italian',
306+
'iu': 'Inuktitut',
307+
'iu-Latn': 'Inuktitut (Latin)',
308+
'ja': 'Japanese',
309+
'ka': 'Georgian',
310+
'kk': 'Kazakh',
311+
'km': 'Khmer',
312+
'kmr': 'Kurdish (Northern)',
313+
'kn': 'Kannada',
314+
'ko': 'Korean',
315+
'ks': 'Kashmiri',
316+
'ku': 'Kurdish (Central)',
317+
'ky': 'Kyrgyz',
318+
'ln': 'Lingala',
319+
'lo': 'Lao',
320+
'lt': 'Lithuanian',
321+
'lug': 'Ganda',
322+
'lv': 'Latvian',
323+
'lzh': 'Chinese (Literary)',
324+
'mai': 'Maithili',
325+
'mg': 'Malagasy',
326+
'mi': 'Māori',
327+
'mk': 'Macedonian',
328+
'ml': 'Malayalam',
329+
'mn-Cyrl': 'Mongolian (Cyrillic)',
330+
'mn-Mong': 'Mongolian (Traditional)',
331+
'mni': 'Manipuri',
332+
'mr': 'Marathi',
333+
'ms': 'Malay',
334+
'mt': 'Maltese',
335+
'mww': 'Hmong Daw',
336+
'my': 'Myanmar (Burmese)',
337+
'nb': 'Norwegian',
338+
'ne': 'Nepali',
339+
'nl': 'Dutch',
340+
'nso': 'Sesotho sa Leboa',
341+
'nya': 'Nyanja',
342+
'or': 'Odia',
343+
'otq': 'Querétaro Otomi',
344+
'pa': 'Punjabi',
345+
'pl': 'Polish',
346+
'prs': 'Dari',
347+
'ps': 'Pashto',
348+
'pt': 'Portuguese (Brazil)',
349+
'pt-PT': 'Portuguese (Portugal)',
350+
'ro': 'Romanian',
351+
'ru': 'Russian',
352+
'run': 'Rundi',
353+
'rw': 'Kinyarwanda',
354+
'sd': 'Sindhi',
355+
'si': 'Sinhala',
356+
'sk': 'Slovak',
357+
'sl': 'Slovenian',
358+
'sm': 'Samoan',
359+
'sn': 'Shona',
360+
'so': 'Somali',
361+
'sq': 'Albanian',
362+
'sr-Cyrl': 'Serbian (Cyrillic)',
363+
'sr-Latn': 'Serbian (Latin)',
364+
'st': 'Sesotho',
365+
'sv': 'Swedish',
366+
'sw': 'Swahili',
367+
'ta': 'Tamil',
368+
'te': 'Telugu',
369+
'th': 'Thai',
370+
'ti': 'Tigrinya',
371+
'tk': 'Turkmen',
372+
'tlh-Latn': 'Klingon (Latin)',
373+
'tlh-Piqd': 'Klingon (pIqaD)',
374+
'tn': 'Setswana',
375+
'to': 'Tongan',
376+
'tr': 'Turkish',
377+
'tt': 'Tatar',
378+
'ty': 'Tahitian',
379+
'ug': 'Uyghur',
380+
'uk': 'Ukrainian',
381+
'ur': 'Urdu',
382+
'uz': 'Uzbek (Latin)',
383+
'vi': 'Vietnamese',
384+
'xh': 'Xhosa',
385+
'yo': 'Yoruba',
386+
'yua': 'Yucatec Maya',
387+
'yue': 'Cantonese (Traditional)',
388+
'zh-Hans': 'Chinese Simplified',
389+
'zh-Hant': 'Chinese Traditional',
390+
'zu': 'Zulu'
391+
};
392+
function getLangCode(lang: string): string | undefined;
393+
function isSupported(lang: string): boolean;
394+
}
395+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
22
"name": "bing-translate-api",
3-
"version": "3.0.1",
4-
"description": "A simple and free API for Bing Translator for Node.js",
3+
"version": "4.0.0",
4+
"description": "A simple and free API for Bing & Microsoft Translator for Node.js",
55
"main": "src/index.js",
66
"typings": "index.d.ts",
77
"scripts": {
8-
"test": "node test/index.js && node test/lang.js",
8+
"test": "node test/index.js && node test/lang.js && node test/met.js",
99
"gen:config": "node scripts/generate-config.js"
1010
},
1111
"keywords": [
1212
"bing",
13+
"microsoft",
1314
"translator",
1415
"api",
1516
"javascript",
1617
"free",
1718
"node",
18-
"translate"
19+
"translate",
20+
"azure"
1921
],
2022
"files": [
2123
"src",

scripts/generate-config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @type {import('got').Got}
3+
*/
14
const got = require('got')
25
const cheerio = require('cheerio')
36
const fs = require('node:fs')
@@ -76,6 +79,28 @@ const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKi
7679
console.log('✔️ Generated language map\n', langMap)
7780
console.log()
7881

82+
// Languages supported by MET mode (usually it's the same with bing translator)
83+
const { translation: metTranslationLanguages } = await got('https://api.cognitive.microsofttranslator.com/languages', {
84+
searchParams: {
85+
'api-version': '3.0',
86+
scope: 'translation'
87+
},
88+
headers: {
89+
'Accept-Language': 'en-US,en',
90+
'User-Agent': DEFAULT_USER_AGENT
91+
}
92+
}).json()
93+
const metLangMap = Object.entries(metTranslationLanguages)
94+
.reduce((langCodeMap, [langCode, langItem]) => (langCodeMap[langCode] = langItem.name, langCodeMap), {})
95+
fs.writeFileSync(
96+
path.resolve(__dirname, '../src/met/lang.json'),
97+
JSON.stringify(metLangMap, null, 2),
98+
{ charset: 'utf-8' }
99+
)
100+
console.log()
101+
console.log('✔️ Generated language map (MET mode)\n', metLangMap)
102+
console.log()
103+
79104
// update ts definition
80105
require('./generate-dts')
81106
})()

scripts/generate-dts.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('node:fs')
22
const path = require('node:path')
33
const { LANGS } = require('../src/lang')
4+
const { LANGS: MET_LANGS } = require('../src/met/lang')
45

56
// update ts definition
67
const typingsTpl = fs.readFileSync(path.resolve(__dirname, './index.tpl.d.ts'), 'utf-8')
@@ -12,7 +13,12 @@ fs.writeFileSync(
1213
JSON.stringify(LANGS, null, 4)
1314
.replace('}', `${' '.repeat(2)}}`)
1415
.replace(/"/g, '\'')
15-
),
16+
).replace(
17+
'__LANG_MAP__',
18+
JSON.stringify(MET_LANGS, null, 6)
19+
.replace('}', `${' '.repeat(4)}}`)
20+
.replace(/"/g, '\'')
21+
),
1622
{ charset: 'utf-8' }
1723
)
1824

0 commit comments

Comments
 (0)