bing-translate-api
Advanced tools
Comparing version 2.10.0 to 3.0.0
{ | ||
"name": "bing-translate-api", | ||
"version": "2.10.0", | ||
"version": "3.0.0", | ||
"description": "A simple and free API for Bing Translator for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -64,4 +64,8 @@ <!-- AUTO-GENERATED. SEE scripts/README.tpl.md FOR ORIGINAL TEMPLATE --> | ||
The text to be translated, can't be blank. The **maximum** text length is **5000**. | ||
The text to be translated, can't be blank. The **maximum** text length is **1000**. | ||
**NOTE:** | ||
1) The maximum text length is up to **5000** characters only in **China**. | ||
2) In EPT mode (since `v3.0.0`), the maximum text length is currently **3000**. | ||
##### _from_ | ||
@@ -74,2 +78,4 @@ Type: `string` Default: `auto-detect` | ||
**NOTE:** The EPT mode (since `v3.0.0`) only supports the languages contained in [src/config.json#eptLangs](src/config.json#L31). Any other language will fall back to the legacy mode, in which a 429 (Too Many Requests) error often occurs. | ||
##### _to_ | ||
@@ -82,2 +88,4 @@ Type: `string` Default: `en` | ||
**NOTE:** The EPT mode (since `v3.0.0`) only supports the languages contained in [src/config.json#eptLangs](src/config.json#L31). Any other language will fall back to the legacy mode, in which a 429 (Too Many Requests) error often occurs. | ||
##### _correct_ | ||
@@ -90,3 +98,3 @@ Type: `boolean` Default: `false` Since: `v1.1.0` | ||
1) There is currently a **limit** of **50 characters** for correction service. | ||
2) **Only** [the languages in the list](src/config.json#L7-L28) are supported to be corrected. | ||
2) **Only** [the languages in the list](src/config.json#L9-L30) are supported to be corrected. | ||
@@ -103,6 +111,3 @@ ##### _raw_ | ||
Default: | ||
``` | ||
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0 | ||
``` | ||
Refer to [src/config.json#userAgent](src/config.json#L111) for the default user agent. | ||
@@ -116,9 +121,11 @@ ##### _proxyAgents_ | ||
MIT © 2021-2023 [plainheart](https://github.com/plainheart). | ||
MIT © 2021-2024 [plainheart](https://github.com/plainheart). | ||
## Thanks | ||
Great thanks to [Bing Translator](https://bing.com/translator) for providing so excellent translation service. | ||
Great thanks to [Bing Translator](https://bing.com/translator) for providing such an excellent translation service. | ||
## Related projects | ||
## Related Projects | ||
- [Capacitor Bing Translator](https://github.com/sabereen/capacitor-bing-translator) - A fork of this project that works in [Capacitor](https://capacitorjs.com). | ||
- [bing-translate-api-java](https://github.com/marmot-z/bing-translate-api-java) - Bing translate API implemented in Java. | ||
- [i18n-translate-generator](https://github.com/victor-heliomar/i18n-translation-generator) - CLI tool that automates the generation of localizations for all languages supported in your project. |
@@ -5,4 +5,6 @@ { | ||
"spellCheckEndpoint": "/tspellcheckv3?isVertical=1&", | ||
"maxTextLen": 5000, | ||
"maxTextLen": 1000, | ||
"maxTextLenCN": 5000, | ||
"maxCorrectableTextLen": 50, | ||
"maxEPTTextLen": 3000, | ||
"correctableLangs": [ | ||
@@ -30,3 +32,83 @@ "da", | ||
], | ||
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0" | ||
"eptLangs": [ | ||
"af", | ||
"sq", | ||
"am", | ||
"ar", | ||
"hy", | ||
"az", | ||
"bn", | ||
"bs", | ||
"bg", | ||
"ca", | ||
"zh-Hans", | ||
"zh-Hant", | ||
"hr", | ||
"cs", | ||
"da", | ||
"prs", | ||
"nl", | ||
"en", | ||
"et", | ||
"fil", | ||
"fi", | ||
"fr", | ||
"de", | ||
"el", | ||
"gu", | ||
"ht", | ||
"he", | ||
"hi", | ||
"hu", | ||
"is", | ||
"id", | ||
"iu", | ||
"ga", | ||
"it", | ||
"ja", | ||
"kn", | ||
"kk", | ||
"km", | ||
"ko", | ||
"ku", | ||
"lo", | ||
"lv", | ||
"lt", | ||
"mg", | ||
"ms", | ||
"ml", | ||
"mt", | ||
"mr", | ||
"my", | ||
"mi", | ||
"ne", | ||
"nb", | ||
"or", | ||
"ps", | ||
"fa", | ||
"pl", | ||
"pt", | ||
"pt-PT", | ||
"pa", | ||
"ro", | ||
"ru", | ||
"sm", | ||
"sr-Cyrl", | ||
"sr-Latn", | ||
"sk", | ||
"sl", | ||
"es", | ||
"sw", | ||
"sv", | ||
"ta", | ||
"te", | ||
"th", | ||
"to", | ||
"tr", | ||
"uk", | ||
"ur", | ||
"vi", | ||
"cy" | ||
], | ||
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0" | ||
} |
@@ -6,3 +6,2 @@ /** | ||
* subdomain?: string, | ||
* cookie: string, | ||
* key: number, | ||
@@ -79,3 +78,3 @@ * token: string, | ||
try { | ||
const { body, headers, request: { redirects: [redirectUrl] } } = await got( | ||
const { body, request: { redirects: [redirectUrl] } } = await got( | ||
replaceSubdomain(TRANSLATE_WEBSITE, subdomain), | ||
@@ -99,5 +98,2 @@ { | ||
// PENDING: optional? | ||
const cookie = headers['set-cookie'].map(c => c.split(';')[0]).join('; ') | ||
const IG = body.match(/IG:"([^"]+)"/)[1] | ||
@@ -128,3 +124,2 @@ const IID = body.match(/data-iid="([^"]+)"/)[1] | ||
subdomain, | ||
cookie, | ||
// PENDING: reset count when value is large? | ||
@@ -141,8 +136,17 @@ count: 0 | ||
* @param {boolean} isSpellCheck | ||
* @param {boolean} useEPT | ||
*/ | ||
function makeRequestURL(isSpellCheck) { | ||
function makeRequestURL(isSpellCheck, useEPT) { | ||
const { IG, IID, subdomain } = globalConfig | ||
return replaceSubdomain(isSpellCheck ? TRANSLATE_API_SPELL_CHECK : TRANSLATE_API, subdomain) | ||
+ '&IG=' + IG | ||
+ '&IID=' + (IID + (isSpellCheck ? '.' + (++globalConfig.count) : '')) | ||
+ '&IID=' + (IID + (isSpellCheck || useEPT ? '.' + (++globalConfig.count) : '')) | ||
+ ( | ||
isSpellCheck || !useEPT | ||
? '' | ||
// PENDING: might no rate limit but some languages are not supported for now | ||
// (See also the `eptLangs` field in src/config.json) | ||
: '&ref=TThis' + | ||
'&edgepdftranslator=1' | ||
) | ||
} | ||
@@ -248,6 +252,2 @@ | ||
if (text.length > config.maxTextLen) { | ||
throw new Error(`The supported maximum length of text is ${config.maxTextLen}. Please shorten the text.`) | ||
} | ||
if (!globalConfigPromise) { | ||
@@ -278,9 +278,25 @@ globalConfigPromise = fetchGlobalConfig(userAgent, proxyAgents) | ||
const requestURL = makeRequestURL(false) | ||
const requestBody = makeRequestBody(false, text, from, to === 'auto-detect' ? 'en' : to) | ||
to === 'auto-detect' && (to = 'en') | ||
const canUseEPT = text.length <= config.maxEPTTextLen | ||
&& ([from, to].every(lang => lang === 'auto-detect' || config.eptLangs.includes(lang))) | ||
if (!canUseEPT) { | ||
// Currently 5000 is supported only in China | ||
// PENDING: dynamically re-generate local config.json when initializing? | ||
const maxTextLen = globalConfig.subdomain === 'cn' | ||
? config.maxTextLenCN | ||
: config.maxTextLen | ||
if (text.length > maxTextLen) { | ||
throw new Error(`The supported maximum text length is ${maxTextLen}. Please shorten the text.`) | ||
} | ||
} | ||
const requestURL = makeRequestURL(false, canUseEPT) | ||
const requestBody = makeRequestBody(false, text, from, to) | ||
const requestHeaders = { | ||
'user-agent': userAgent || config.userAgent, | ||
referer: replaceSubdomain(TRANSLATE_WEBSITE, globalConfig.subdomain), | ||
cookie: globalConfig.cookie | ||
referer: replaceSubdomain(TRANSLATE_WEBSITE, globalConfig.subdomain) | ||
} | ||
@@ -299,7 +315,7 @@ | ||
headers: requestHeaders, | ||
// got will set CONTENT_TYPE as `application/x-www-form-urlencoded`$ | ||
// got will set CONTENT_TYPE as `application/x-www-form-urlencoded` | ||
form: requestBody, | ||
responseType: 'json', | ||
agent: proxyAgents, | ||
retry: retryConfig | ||
retry: canUseEPT ? 0 : retryConfig | ||
}) | ||
@@ -306,0 +322,0 @@ ) |
const { translate } = require('../src/index') | ||
const { maxTextLen, maxCorrectableTextLen } = require('../src/config.json') | ||
const { maxTextLen, maxCorrectableTextLen, maxEPTTextLen, maxTextLenCN } = require('../src/config.json') | ||
@@ -69,1 +69,16 @@ function printRes(res) { | ||
.catch(e => onErr(e, true)) | ||
// max EPT text len -> return `Zero` | ||
translate(new Array(maxEPTTextLen).fill('0').join(''), null, 'en') | ||
.then(printRes) | ||
.catch(e => onErr(e, true)) | ||
// max EPT text len -> fall back to non-EPT mode for exceeding max length | ||
translate(new Array(maxEPTTextLen + 1).fill('0').join(''), null, 'en') | ||
.then(printRes) | ||
.catch(e => onErr(e, true)) | ||
// max text len in China -> return `undefined` for exceeding max length | ||
translate(new Array(maxTextLenCN + 1).fill('0').join(''), null, 'en') | ||
.then(printRes) | ||
.catch(e => onErr(e, true)) |
Sorry, the diff of this file is not supported yet
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
28730
880
126