cjvnjde / google-translate-api-browser

A free and unlimited API for Google Translate
https://www.npmjs.com/package/google-translate-api-browser
MIT License
213 stars 31 forks source link

[BUG] Quotes in text to translate causes the translation to fail with Bad Request #31

Closed Cohee1207 closed 12 months ago

Cohee1207 commented 12 months ago

Environment Package version: 4.2.4 Node version: v20.9.0 OS: Windows 11 22H2

Description

  1. Trying to translate a text with quotes in it fails.
  2. A translation error causes the process to crash instead of being handled with try-catch.

Steps to reproduce

import { translate } from 'google-translate-api-browser';

async function translateText(text, lang) {
    try {
        const result = await translate(text, { to: lang });
        console.log(result.text);
    } catch (error) {
        console.log('Translation error: ' + error);
    }
}

await translateText('The cat cannot speak like humans, it just makes cat noises such as "meow", and it doesn\'t even know English', 'es');

export { };

Expected result Translated text displays in the console.

Actual result Node process crashes, try-catch not executed. Crash logs read: Error: Data is either empty or corrupted

Doing a request with fetch instead gives this response:

  Bad Request )]}'

102
[["er",null,null,null,null,400,null,null,null,3],["di",10],["af.httprm",9,"3737202303121642252",34]]
25
[["e",4,null,null,138]]
Cohee1207 commented 12 months ago

FYI, not reproduced in version 3.0.1

cjvnjde commented 12 months ago

Thanks for noticing this problem. Please try version 4.2.7. This bug should be fixed in that version.