Giter Site home page Giter Site logo

aidanwelch / google-translate-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vitalets/google-translate-api

126.0 1.0 16.0 1.06 MB

An updated free and unlimited API for Google Translate :dollar: :no_entry_sign:

License: MIT License

JavaScript 97.84% Shell 0.14% TypeScript 2.03%

google-translate-api's Issues

Getting Weird Translation

Hi im not getting the same result when i translate in google translate and using google-translate-api.
What might be the issue?

Batch translation error

Error Info

Partial Translation Request Fail: at least one translation failed, it was either invalid or more likely- rejected by the server. You can try the request again and if it persists try a proxy, spacing out requests, and/or using a different tld. If you would like to translate other requests in a batch translation even if one fails(the failed translation will be set to null) pass the option rejectOnPartialFail: false. You can also try using the singleTranslate endpoint with: forceBatch: false

A reproducible example
https://runkit.com/eyelly/64787340c932b600084ae8ee

Thanks to the author for providing this tool library, hope this problem can be fixed

It throws error "fetch is not defined"

My demo is simple , just like this.

(async() => {
    const translate = require('google-translate-api-x');
    const res = await translate('Ik spreek Engels', {to: 'zh'});
    console.log(res.text);
})()

When run the code, it throws the error

image

Obtain translation synonyms

Is there to obtain the synonyms of a word translation. For example, let's say the preposition mit in German, which can be in multiple ways in English. Is there to obtain all of those possible translations?
Thanks!

TypeScript Error

TS2339: Property 'text' does not exist on type 'ITranslateResponse | ITranslateResponse[] | { [x: string]: ITranslateResponse; }'.   Property 'text' does not exist on type 'ITranslateResponse[]'.

I use React and TypeScript.

const translator = new Translator({from: 'en', to: 'es', forceBatch: false, tld: 'es'}); const res = await translator.translate('Ik spreek Engels', {to: user_data!.lang}); console.log(res.text)

Discrepancy with Google Translation

Hi Aidan,

Loving the API so far!

I did have one issue, when I'm translating something from Hebrew to English, I'm getting a different translation from Google Translate. The one the API is giving me is incorrect, and the one Google Translate gives me is much better. Do you think I'm using it wrong, or have you seen inconsistencies with Google Translate's website?

This is my prompt from the translate call, which returns this result: "Aryan main, I would like to the mountains at the distance"
image
However, this is what Google translate returns: "I lift my head, I will lift my eyes to the mountains in the distance"

Any ideas about this? I've also been having this issue with some other languages, but this is one that I found right now. Thanks so much!

Can't run on React.js

The error:
TypeError: google_translate_api_x__WEBPACK_IMPORTED_MODULE_1__ is not a function

I've tried using this library on vanilla js and it works just fine. But when I tried it on React it keeps getting error like in the above even I use the simpliest method to use this library. Is there any settings that I have to apply first in my React project before using this library?
Sorry for my english and I'm new to React and JavaScript, so it will be so kind of you if you can give me an assistance.

Btw, thanks for providing this library

CORS policy error

i installed your package and tried the description but unfortunately i get errors about CORS error, and i don't know how to solve it

AxiosError: Request failed with status code 429

Issue 12 wasn't finished being addressed before it was closed. My app wouldn't hit the rate limit as it sends fewer than 20 requests per day.

Error code 429 is a "too many requests" error. I would assume that your API has a Google Translate API key tied to it? My immediate thought is there are too many users on this "free api" causing the interface to hit the rate limit.

Big translation slowed down

Hi @AidanWelch, thanks for an amazing package!
One thing I noticed in the past day or two is that a big translation (like full song lyrics) considerably slowed down. It used to be almost instant, but now it's taking up to 15 seconds.
When I try vitalets package, it is still almost instant. Do you know of any reason this might be happening?

I'm using a Next.js project, still on Next 12 using an API route (server side). Is there any way to test the speed of the actual package? Thanks!

fetch is not defined after deployment

@AidanWelch This package works fine when I start my server in terminal, but as soon as I deploy to Render it says ERROR ReferenceError: fetch is not defined at Object.requestFunction (/opt/render/project/src/node_modules/google-translate-api-x/lib/defaults.cjs:9:36), seems it isn't truly cross-platform or is there something I could do different to get it to work in a pure Node environment?

All the help will be appreciated and the batching feature is truly an improvement on the vitalets package. Thank you! ✨

Autocorrect example provides different output

The documentation example from https://github.com/AidanWelch/google-translate-api?tab=readme-ov-file#autocorrect shows different output than the API.

Documentation

const res = await translate('I spea Dutch!', { from: 'en', to: 'nl', autoCorrect: true });

console.log(res.from.text.didYouMean); // => false
console.log(res.from.text.autoCorrected); // => true
console.log(res.from.text.value); // => 'I [speak] Dutch!'

console.log(res.text); // => 'Ik spreek Nederlands!'

Output of version 10.6.8

const res = await translate('I spea Dutch!', { from: 'en', to: 'nl', autoCorrect: true });

console.log(res.from.text.didYouMean); // => true
console.log(res.from.text.autoCorrected); // => false
console.log(res.from.text.value); // => 'I [speak] Dutch!'

console.log(res.text); // => 'Ik speed Nederlands!'

So the flags didYouMean and autoCorrected are exactly opposite, and the res.text is not corrected, despite autoCorrect: true.

UPDATE

It did autocorrect while using "Card gaame" as English source text with a typo:

const res = await translate('Card gaame', { from: 'en', to: 'nl', autoCorrect: true });

console.log(res.from.text.didYouMean); // => false
console.log(res.from.text.autoCorrected); // => true
console.log(res.from.text.value); // => 'Card [game]'

console.log(res.text); // => 'Kaartspel'

After reading the description of the returned parameters more thoroughly, it's important to realise that although autoCorrect: true is requested, Google might not be able to correct it.
In that case it will return res.from.text.autoCorrected = false and a possibly incorrect translation in res.text.

Maybe it's an idea to update the example in the docu, since it does not reflect the API output.

Caught an error from batch translate

Hi @AidanWelch

I caught a new error from the below codes.

trInput is an array for batch translation.

It doesn't happen all the time, maybe 1 out of 4 or 5 times. Even when all the variable values are exactly the same for all the 4 or 5 times.

    const translation = await translate(trInput, {from: userLanguage, to: addHelperLanguage && helperLanguage, autoCorrect: true})
                                .catch((error) => {
                                    console.error('translate| translate-google-api-x error: ', error.message)
                                    throw error
                                });

Error caught in below

translate| translate-google-api-x error:  Cannot read properties of null (reading '1')
Url: https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&source-path=%2F&f.sid=3998618730767360818&bl=boq_translate-webserver_20230409.08_p0&hl=en-US&soc-app=1&soc-platform=1&soc-device=1&_reqid=7749&rt=c

The value returned from the Url in the error in below

)]}'

103
[["er",null,null,null,null,405,null,null,null,9],["di",17],["af.httprm",17,"-2930294764364961249",6]]
25
[["e",4,null,null,139]]

CORS policy

I'm using ReactJs.
When I use the request example. I'm getting CORS error.

import translate from 'google-translate-api-x';

const res = await translate('Ik spreek Engels', { to: 'en' });

console.log(res.text); //=> I speak English
console.log(res.from.language.iso);  //=> nl

That code return

Access to fetch at 'https://translate.google.com/' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is that an API error or I need to use some param on the request translate()?

  • with node that error doesn't occur.

Autocorrect not properly applied to translations from certain networks

Certain networks require the X-Goog-BatchExecute-Bgr header to be sent on requests, or the autocorrect will not be applied to some translations(seemingly typos where a letter is dropped, such as "I spea Dutch!" instead of "I speak Dutch!").

The code for generating this header I believe is found in this static script.

I believe in xH.prototype.s()

This would likely take a while to fix.

google-translate-api-x gives below error

error: node_modules/google-translate-api-x/lib/Translator.cjs: /home/nirali/Desktop/farmerAssistantApp/node_modules/google-translate-api-x/lib/Translator.cjs: Class private methods are not enabled. Please add @babel/plugin-proposal-private-methods to your configuration.

ReferenceError: fetch is not defined

/root/***/node_modules/google-translate-api-x/lib/defaults.cjs:9
        requestFunction: fetch,
                         ^

ReferenceError: fetch is not defined
    at Object.<anonymous> (/root/***/node_modules/google-translate-api-x/lib/defaults.cjs:9:19)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/root/***/node_modules/google-translate-api-x/lib/translate.cjs:2:29)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)

Issue with the speak functionality

New to development,
I am getting this error
/node_modules/google-translate-api-x/lib/speak.cjs:66 const result = JSON.parse(translation[2])[0]; ^ TypeError: Cannot read properties of null (reading '0')

I figured it out , the issue appears when the text under single translate using "speak" function exceeds 195 characters.
I need help how to convert long texts to mp3.

There are other packages that are able to convert it, but I really wanted to make this package more efficient by adding inputs.

Thanks

`res.from.language.didYouMean` is always true

When I use the Google Translate website from English to Dutch, and enter the Dutch word "Kaartspel" in the English from box, it shows "Translate from: Dutch":

Screenshot 2024-06-13 at 11 18 32

See: https://translate.google.com/?sl=en&tl=nl&text=Kaartspel&op=translate

When I use this API:

const res = await translate('Kaartspel', { from: 'en', to: 'nl', autoCorrect: true, requestFunction: fetch});
console.log(res.from.language.didYouMean);
console.log(res.from.language.iso);

It prints:

true
nl

This is correct ✅

Repeat with a correct English "Card game", there is no warning in the portal:

Screenshot 2024-06-13 at 11 24 08

See: https://translate.google.com/?sl=en&tl=nl&text=Card%20game&op=translate

When I use this API:

const res = await translate('Card game', { from: 'en', to: 'nl', autoCorrect: true, requestFunction: fetch});
console.log(res.from.language.didYouMean);
console.log(res.from.language.iso);

It prints:

true
en

Although res.from.language.iso changed correctly to "en", res.from.language.didYouMean is still true.
This is incorrect ❌

requestOptions documentation outdated

google-translate-api#proxy

code:

const tunnel = require('tunnel');
const translate = require('google-translate-api-x');

translate('Ik spreek Engels', { to: 'en', tld: 'jp' }, {
    agent: tunnel.httpsOverHttp({
        proxy: {
            host: '127.0.0.1', // ip
            port: '7890', // port
        }
    }
    )
}).then(res => {
    console.log(res)
    // do something
});

console:

PS C:\Users\Administrator\Desktop\NodejsProjects> node .
node:internal/deps/undici/undici:14062
    Error.captureStackTrace(err, this);
          ^

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14062:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async runBatch (C:\Users\Administrator\Desktop\NodejsProjects\node_modules\google-translate-api-x\lib\translate.cjs:7:19) {
  cause: ConnectTimeoutError: Connect Timeout Error
      at onConnectTimeout (node:internal/deps/undici/undici:7897:28)
      at node:internal/deps/undici/undici:7855:50
      at Immediate._onImmediate (node:internal/deps/undici/undici:7884:37)
      at process.processImmediate (node:internal/timers:471:21) {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  }
}

Node.js v18.13.0

Real-time translation

Can this package translate in real-time, like users don't need to press the translate button to translate, but just need to type and wait for it to auto translate?

change speak speed

is there any way to change the speak (tts) speed? its talking a bit slow

ERROR [TypeError: Cannot convert null value to object]

Hello, I am getting the error in the title while translating text. I suspected it because I was using texts dynamically. Then I tried to translate the texts statically but this error returns 1 time out of 5 translations (same text).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.