Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and parse json answer from google translate

I want to translate word with ajax request to google translate If i use curl, it willl be like:

curl_init("http://translate.google.com/translate_a/t?client=t&text=hello&sl=en&tl=ru");

But i cant use server-side scripts with queries because they will be from same IP
But i cant use JSONP request too, because it's not allowed i think. Here's server response:

[[["привет","hello","privet",""]],[["",["Алло","Здравствуйте."]],["имя существительное",["приветствие","приветственный возглас","возглас удивления"]],["глагол",["здороваться","звать","окликать"]],["междометие",["привет","здравствуйте","алло"]]],"en",,[["привет",[5],1,0,1000,0,1,0]],[["hello",4,,,""],["hello",5,[["привет",1000,1,0],["Здравствуй",0,1,0],["Здравствуйте",0,1,0],["Hello",0,1,0]],[[0,5]],"hello"]],,,[["en"]],13]

And i cant load content to iframe

<iframe src="http://translate.google.com/translate_a/t?client=t&text=hello&sl=en&tl=ru"></iframe>

because response content type is "text/javascript" and browser downloads a file

PS. i cant decode content with php function json_decode() or with jquery.ajax because response json is corrupt. You can test it here http://json.parser.online.fr/

Give me an advice, please

like image 327
Shara Avatar asked Apr 26 '12 13:04

Shara


People also ask

How do I translate a JSON file to Google Translate?

The translate-json.js node script is simple. It takes an api key, input .json file, and a comma separated list of language codes supported by Google Translate. Full list here. Here’s an example from the repo:

How to use translate-JSON in Node JS?

The translate-json.js node script is simple. It takes an api key, input.json file, and a comma separated list of language codes supported by Google Translate.

What does it take to get Google Translate to work?

It takes an api key, input .json file, and a comma separated list of language codes supported by Google Translate. Full list here. Here’s an example from the repo:

How do I use the translations array in go translation?

The translations array contains two translatedText fields with translations provided in the requested target language ( de: German). The translations are listed in the same order as the corresponding source array in the request. Before trying this sample, follow the Go setup instructions in the Translation quickstart using client libraries .


2 Answers

Try:

http://translate.google.com/translate_a/t?client=p

if the client parameter is anything other than 't' it returns valid JSON data. Of course, you could always transform the 't' return variable into a valid array or JSON but this is easier.

like image 183
puromonogatari Avatar answered Oct 06 '22 01:10

puromonogatari


Make sure to add header param "User-Agent" = "Mozilla/4.0". Google adds captcha if you do not

like image 33
Aaron Mclean Avatar answered Oct 05 '22 23:10

Aaron Mclean