aidanwelch / google-translate-api Goto Github PK
View Code? Open in Web Editor NEWThis project forked from vitalets/google-translate-api
An updated free and unlimited API for Google Translate :dollar: :no_entry_sign:
License: MIT License
This project forked from vitalets/google-translate-api
An updated free and unlimited API for Google Translate :dollar: :no_entry_sign:
License: MIT License
Most languages default to masculine, so it shouldn't return feminine just because that is what Google Translate lists first, because in the Google Translate UI that is properly represented
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).
Is there interest in the Fetch and Axios versions being combined, with auto and manual detection?
Anyone have more info on this?
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.
/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)
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()
?
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]]
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
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?
@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! ✨
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.
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":
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:
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 ❌
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
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
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!
this "https-proxy-agent" is not support with react native. Could you give any suggestions?
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)
The codes ran fine that last time I tested, about a month or so ago. And then, in my regression test today, it starts giving the above error.
Does anyone have any idea?
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.
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
.
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.
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 optionrejectOnPartialFail: 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
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"
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!
I want to pass an array of string for translation. Could you help support it?
Use proxy or pretest for access first (or if there's another solution
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!
Hi im not getting the same result when i translate in google translate and using google-translate-api.
What might be the issue?
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
is there any way to change the speak (tts) speed? its talking a bit slow
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.