bing-translate-api
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "bing-translate-api", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A simple and free API for Bing Translator for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -37,3 +37,3 @@ # bing-translate-api | ||
The text to be translated. | ||
The text to be translated, cann't be blank. | ||
@@ -44,3 +44,3 @@ ##### from | ||
The language code of source text. | ||
Must be `auto-detect` or one of the codes/names (not case sensitive) contained in [lang.js](https://github.com/plainheart/bing-translate-api/blob/master/src/lang.js) | ||
**MUST** be `auto-detect` or one of the codes/names (not case sensitive) contained in [lang.js](https://github.com/plainheart/bing-translate-api/blob/master/src/lang.js) | ||
@@ -51,4 +51,14 @@ ##### to | ||
The language in which the text should be translated. | ||
Must be one of the codes/names (not case sensitive) contained in [lang.js](https://github.com/plainheart/bing-translate-api/blob/master/src/lang.js). | ||
**MUST** be one of the codes/names (not case sensitive) contained in [lang.js](https://github.com/plainheart/bing-translate-api/blob/master/src/lang.js). | ||
##### raw | ||
Type: `boolean` Default: `false` | ||
Whether the translation result contains raw response from Bing API. | ||
##### tld | ||
Type: `string` Default: `''` | ||
Can be `'www'` | `'cn'` | `''` | ||
##### userAgent | ||
@@ -60,3 +70,3 @@ Type: `string` | ||
Default: | ||
```js | ||
``` | ||
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 | ||
@@ -63,0 +73,0 @@ ``` |
@@ -6,3 +6,3 @@ const got = require('got') | ||
const TRANSLATE_API_ROOT = 'https://bing.com' | ||
const TRANSLATE_API_ROOT = 'https://{tld}bing.com' | ||
const TRANSLATE_WEBSITE = TRANSLATE_API_ROOT + '/translator' | ||
@@ -16,11 +16,19 @@ const TRANSLATE_API = TRANSLATE_API_ROOT + '/ttranslatev3?isVertical=1' | ||
function replaceTld(url, tld) { | ||
if (tld && (tld !== 'www' && tld !== 'cn')) { | ||
console.warn(`the tld '${tld}' may not be valid.`) | ||
} | ||
return url.replace('{tld}', tld ? tld + '.' : '') | ||
} | ||
/** | ||
* fetch global config including `IG` and `IID` | ||
* @param {string} tld www | cn | '' | ||
* @param {string} userAgent | ||
*/ | ||
async function fetchGlobalConfig(userAgent) { | ||
async function fetchGlobalConfig(tld, userAgent) { | ||
let IG | ||
let IID | ||
try { | ||
const { body } = await got(TRANSLATE_WEBSITE, { | ||
const { body } = await got(replaceTld(TRANSLATE_WEBSITE, tld), { | ||
headers: { | ||
@@ -42,4 +50,4 @@ 'user-agent': userAgent || USER_AGENT | ||
function makeRequestURL(IG, IID, count) { | ||
return TRANSLATE_API | ||
function makeRequestURL(IG, IID, count, tld) { | ||
return replaceTld(TRANSLATE_API, tld) | ||
+ (IG && IG.length ? '&IG=' + IG : '') | ||
@@ -61,7 +69,9 @@ + (IID && IID.length ? '&IID=' + IID + '.' + count : '') | ||
* @param {string} text content to be translated | ||
* @param {string} from source language code. `auto-detect` by default. | ||
* @param {string} to target language code. `en` by default. | ||
* @param {string} userAgent the expected user agent header | ||
* @param {string} from <optional> source language code. `auto-detect` by default. | ||
* @param {string} to <optional> target language code. `en` by default. | ||
* @param {boolean} raw <optional> the result contains raw response if true | ||
* @param {string} tld <optional> www | cn | '' | ||
* @param {string} userAgent <optional> the expected user agent header | ||
*/ | ||
async function translate(text, from, to, userAgent) { | ||
async function translate(text, from, to, raw, tld, userAgent) { | ||
if (!text || !(text = text.trim())) { | ||
@@ -72,3 +82,3 @@ return | ||
if (!globalConfig) { | ||
await fetchGlobalConfig(userAgent) | ||
await fetchGlobalConfig(tld, userAgent) | ||
} | ||
@@ -89,3 +99,3 @@ | ||
const requestURL = makeRequestURL(globalConfig.IG, globalConfig.IID, ++globalConfig.count) | ||
const requestURL = makeRequestURL(globalConfig.IG, globalConfig.IID, ++globalConfig.count, tld) | ||
const requestBody = makeRequestBody(text, from, to === 'auto-detect' ? 'en' : to) | ||
@@ -103,3 +113,19 @@ | ||
return body | ||
const translation = body[0].translations[0] | ||
const res = { | ||
text, | ||
userLang: from, | ||
translation: translation.text, | ||
language: { | ||
to: translation.to, | ||
from: body[0].detectedLanguage.language | ||
} | ||
} | ||
if (raw) { | ||
res.raw = body | ||
} | ||
return res | ||
} | ||
@@ -106,0 +132,0 @@ |
const { translate } = require('../src/index') | ||
function printRes(res) { | ||
return console.log(res.text, '----->', res.translation, 'fromLang', res.language.from) | ||
} | ||
// default: auto-detect(zh-Hans) -> en | ||
translate('你好') | ||
.then(res => console.log('你好 ----->', res[0].translations[0].text)) | ||
translate('你好').then(printRes) | ||
// auto-detect(English) to zh-Hans | ||
translate('Hello', null, 'zh-Hans') | ||
.then(res => console.log('Hello ----->', res[0].translations[0].text)) | ||
translate('Hello', null, 'zh-Hans').then(printRes) | ||
// auto-detect(Korean) to zh-Hant | ||
translate('안녕하십니까', null, 'zh-Hant') | ||
.then(res => console.log('안녕하십니까 ----->', res[0].translations[0].text)) | ||
translate('안녕하십니까', null, 'zh-Hant').then(printRes) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10350
243
78