Stichoza / google-translate-php

🔤 Free Google Translate API PHP Package. Translates totally free of charge.
MIT License
1.82k stars 383 forks source link

ErrorException (400 Bad Request) when translating to recently added languages #130

Closed murat-ti closed 1 year ago

murat-ti commented 4 years ago

Google Translate throws ErrorException with 400 Bad Request when translating to languages like Turkmen (tk), etc.

Original issue content

Exception 'ErrorException' with message 'Client error: GET https://translate.google.com/translate_a/single?client=webapp&hl=en&dt=t&dt=bd&dt=at&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&sl=ru&tl=tk&q=%D0%A0%D0%BE%D0%B7%D1%8B%D0%B3%D1%80%D1%8B%D1%88+%D0%B2+Ada+kafe&ie=UTF-8&oe=UTF-8&multires=1&otf=0&pc=1&trs=1&ssel=0&tsel=0&kc=1&tk=592424.997718 resulted in a 400 Bad Request response:


<!DOCTYPE html><html lang=en><meta charset=utf-8><meta name=viewport content="initial-scale=1, minimum-scale=1, width=de (truncated...)

in C:\xampp\htdocs\teklipyaz\vendor\stichoza\google-translate-php\src\GoogleTranslate.php:306

Stack trace:
#0 C:\xampp\htdocs\teklipyaz\vendor\stichoza\google-translate-php\src\GoogleTranslate.php(223): Stichoza\GoogleTranslate\GoogleTranslate->getResponse('\xD0\xA0\xD0\xBE\xD0\xB7\xD1\x8B\xD0\xB3\xD1\x80\xD1\x8B\xD1...')
#1 C:\xampp\htdocs\teklipyaz\backend\controllers\EventController.php(228): Stichoza\GoogleTranslate\GoogleTranslate->translate('\xD0\xA0\xD0\xBE\xD0\xB7\xD1\x8B\xD0\xB3\xD1\x80\xD1\x8B\xD1...')
#2 [internal function]: backend\controllers\EventController->actionTranslate()
#3 C:\xampp\htdocs\teklipyaz\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\teklipyaz\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#5 C:\xampp\htdocs\teklipyaz\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('translate', Array)
#6 C:\xampp\htdocs\teklipyaz\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('event/translate', Array)
#7 C:\xampp\htdocs\teklipyaz\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 C:\xampp\htdocs\teklipyaz\owner\index.php(17): yii\base\Application->run()
#9 {main}```
Stichoza commented 4 years ago

Ok, got the problem. I'll rename this issue if you don't mind

Stichoza commented 4 years ago

Replicated the error. It definitely throws an exception. I checked the website and saw that Google is sending a new xid parameter. I copied it from devtools and added it to a query parameter

'xid'  => 1782844, // Value copied from Google Translate network logs

It fixed the problem you described.

Now I need to figure out how the xid value is generated. Any help appreciated.

Stichoza commented 4 years ago

Excerpt from translate_m.js on Google Translate website:

var Nu = function(a) {
        var b = (new Wm(n.location.href)).a,
            c = b.get("e"),
            d = b.get("expid");
        b = b.get("expflags");
        var e = "";
        if (b)
            return "expflags=" + b;
        c && (e += "e=" + c);
        d && (e && (e += "&"), e += "expid=" + d);
        a.b.length && (e && (e += "&"), e += "xid=" + a.b.join());
        return e
    },
murat-ti commented 4 years ago

Thank you for your quick reply, currently I decided to use static value in my project. I try understand how xid value is generating in previous answer, but can not find values as "e" and "expid" in result generated url.

frzsombor commented 4 years ago

The Nu function gets called like this:

image

... where this.na holds an Iuobject with a b parameter, that is an array containing this "mystic" value. In this scope this refers to a PM object. If we inspect what happens when a PM object is created:

image

... on line 30026 it looks like the Iuobject that this.na is set to, gets a parameter, which is an array of "triggered experiment ids".

So it looks like this is just some kind of internal id of an "experiment" that G is running right now (and this is what xid is referring to). Later I also verified this when I found it in the source code of the page:

image

--

Further inspections: I've set a breakpoint on the first line of the Nu function and tried removing the magical number before setting it in the request. When I removed it and I translated a string from English to German, the translation was successful, but on an English to Turkmen translation, it failed, so it looks like the experiment id is required for the new (experimental?) language translations. Maybe for G to log some things, initialize other modules.

For me, this means that we should keep our eyes on the new languages and look for experiment ids, as they may change a couple of times in the near future until G decides to make them permanent.

fre2mansur commented 4 years ago

Where and how to add this parameter 'xid' => 1782844?

murat-ti commented 4 years ago

Where and how to add this parameter 'xid' => 1782844?

Hi, You need find GoogleTranslate.php, which is located in "yourproject\vendor\stichoza\google-translate-php\src\GoogleTranslate.php". Go to the getResponse(string $string) function, which is located on 290 line and xid parameter like that: $queryArray = array_merge($this->urlParams, [ 'sl' => $this->source, 'tl' => $this->target, 'tk' => $this->tokenProvider->generateToken($this->source, $this->target, $string), 'q' => $string, 'xid' => 1782844, ]);

gaborivanyiczki commented 4 years ago

image

Hello,

I'm currently facing the same issue. Did you have the chance to solve this issue?

Regards, Gabor

nadia-am commented 4 years ago

@gaborivanyiczki Did you find the solution? I am facing the same problem!

heinminhtet8138 commented 4 years ago

Did you find the solution? I am facing the same problem! for Myanmar and chinese language Please give me answer

nadia-am commented 4 years ago

@heinminhtet8138 Mine was because of sanction, I changed the server and problem solved.

thanhtoan1196 commented 2 years ago

any update?

Stichoza commented 1 year ago

Seems to be working. Added a separate test in 0d921190901165605c5a673f3d0df13fd89b96dc to check translations.