Stichoza / google-translate-php

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

Problem with recent update from google api translate and complex strings #119

Closed gbelot2003 closed 4 years ago

gbelot2003 commented 4 years ago

Hello there. Just now I update the translate v3.4 module according with the changes you suggested, and for simple text works fine, but now the API uses the verb GET but the main problem is that I am using a text field with html, the answer is always: resulted in a 403 Forbidden response: Im already using something like this to clear the code:

    $inpunt1 =  str_replace("<strong>", "",  $string);
    $inpunt2 =  str_replace("</strong>", "",  $inpunt1);
    $inpunt3 =  str_replace("<br />", "",  $inpunt2);
    $inpunt4 =  str_replace("/r", "",  $inpunt3);
    $inpunt5 =  str_replace(")", "",  $inpunt4);
    $inpunt6 =  str_replace("<p>", "",  $inpunt5);
    $inpunt7 =  str_replace("</p>", "",  $inpunt6);
    $input8 = preg_replace('/\s/', ' ', $inpunt7);
    $input9 = preg_replace('/\n/', '', $input8);

even after all this cleanning Im still getting this response from the server:

Client error: GET https://translate.google.com/translate_a/single?client=webapp&hl=en&dt=t&sl=es&tl=en&q=DESCRIPCI%26Oacute%3BN+MACROSC%26Oacute%3BPICA%3A++%26nbsp%3B++Se+recibe+1+frasco+conteniendo+2+l%26aacute%3Bminas+con+frotis+fijados+con+alcohol+de+regi%26oacute%3Bn+sangrado+pez%26oacute%3Bn+derecho%2C+se+colorean+con+hematoxilina-eosina.++++DIAGN%26Oacute%3BSTICO%3A+PEZON+DERECHO%2C+ASPIRADO+AGUJA+FINA%2C+EXTENDIDOS+-+++%26nbsp%3B++COMENTARIO%3A&ie=UTF-8&oe=UTF-8&multires=1&otf=0&pc=1&trs=1&ssel=0&tsel=0&kc=1&tk=531074.928252 resulted in a 403 Forbidden response:

My cuestion, is there a workaround, option or maybe another module to fix this issue,

thanks

gbelot2003 commented 4 years ago

Ok, if anyone comes to a problem like this, for me, html_entity_decode() solve the problem!!

ederson13 commented 4 years ago

could you provide some more details regarding your fix ?

gbelot2003 commented 4 years ago

Sorry for the delay, is very simple indid, in the controller, the string about to be translated, it has to be sanitize first, remember, now is a get verb. I just create a simple function before translation. and of course, all the code about in my first entry is unnecesary, you just have clear the strings to pass for translations

private function cleanText($string)
{
    return trim(html_entity_decode($string, ENT_COMPAT, 'UTF-8'));
}

then just

$factor = $this->cleanText($muestra->body); $body = $translator->setSource('es') ->setTarget('en') ->translate($factor);

otnansirk commented 4 years ago

But I still got that error. I added the function cleanText() before using it in translate()

frzsombor commented 4 years ago

@gbelot2003 I've tried figuring out what was the original text you wanted to translate, and by decoding the url you pasted I found that it could have been the following:

DESCRIPCI&Oacute;N MACROSC&Oacute;PICA: &nbsp; Se recibe 1 frasco conteniendo 2 l&aacute;minas con frotis fijados con alcohol de regi&oacute;n sangrado pez&oacute;n derecho, se colorean con hematoxilina-eosina. DIAGN&Oacute;STICO: PEZON DERECHO, ASPIRADO AGUJA FINA, EXTENDIDOS - &nbsp; COMENTARIO:

I see that you also figured out that HTML entities caused the problem because you solved it using html_entity_decode(). But what's really interesting, if I copy&paste the text above to the official Google Translate page, I also get a translation error on that page (which I never ever saw before). See:

image

It looks like this is an error with Google Translate itself, and I verified this as Google Translate fails even on a simple string if it contains any HTML entity like Hello &nbsp; world!.

If anybody expects to translate strings containing HTML entities I suggest them to decode the strings with html_entity_decode() before passing them to the translate function, until Google fixes this issue.

@Stichoza Until then I also suggest that this should be noted in the known limitations.