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] Translate doesn't work on texts that contain multiple paragraphs #40

Closed Cohee1207 closed 9 months ago

Cohee1207 commented 9 months ago

Environment Package version: 4.3.0 Node version: v21.6.1 OS: Windows 11 22H2

Description Translating a text that contains a newline character fails with 400 Bad Request from Google.

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(`Hello\nWorld`, 'es');

export { };

Expected result Full translated text displayed in the console.

Actual result Translation fails with an error. Here's an actual response body returned by Google

)]}'

102
[["er",null,null,null,null,400,null,null,null,3],["di",7],["af.httprm",7,"-8758994509752400712",34]]
25
[["e",4,null,null,138]]
cjvnjde commented 9 months ago

This should be fixed in version 4.3.1