Oct 27, 2009

Google Translate API

I already revealed one secret of our group project. This post will explain how we used Google Translate to translate the user's keyword into english. I will simplify it by assuming the user speaks german.
Google's webservice returns not XML but text that might be (it looks like) a JSON object.
The needed url is:
http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&format?html&q=YOUR_KEYWORD&langpair=de%7Cen

There is no userkey or application id needed. It is necessary to change the "YOUR_KEYWORD" part with your keyword.
Once again, we splited the string into two parts and recombined them with the passed keyword:
$ur1 = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&format?html&q=';
$ur2 = '&langpair=de%7Cen
$uri =$ur1.$rk.$ur2

The langpair describes the source and the result language (in this case de -> german to en-> english).

The result looks like this
{"responseData": {"translatedText":"Bicycle"}, "responseDetails": null, "responseStatus": 200}


In order to get the plain result, we decided to work on this data as string.
var text = request.responseText;
// returning string gets shortend only to the keyword
y=text.split(":"); // splits the text into different parts after a :
z=y[2].split("}"); // only the second part is intersting. we split it again at every }
z=(z[0]); // Now the first part is the translated keyword, surrounded by quotation marks
var l=z.length; // l has now the lenght of the string.
// The last letter of the string we want is the length -1, the first one is the letter at position 1
trans=z.substring(1,(l-1)); // trans has the translated word


We use the translated keyword to pass it to the ebay-search funktion.
The work on the return data is not elegant, so we are lucky to get other ways to do it better from you.
Hope this will help some of you! If there are questions, feel free to ask

Edit:
This is the link where you can get your code formated:
http://formatmysourcecode.blogspot.com/

2 comments:

  1. The ideas for your group project are really good. Choosing to translate other languages to English is very creative and useful. There are many people who would be interested and could benefit from this.

    ReplyDelete
  2. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. Traductor jurado en Barcelona

    ReplyDelete