Use Google Translate for FREE in your Apps

作者: admin 分类: 未分类 发布时间: 2013-06-19 13:51 ė2,076 浏览数 6没有评论
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)

The Google Translate API  are not free.We built a HACK which allows us to use it for FREE!!

Note:This hack is a result of one night work done for submission of some college project and has been used only for 2 days for a max of 10 queries. It is based on pure observance and is not intended to cause any harm. The author is in no way responsible for the actions of the reader.

TL;DR: if you are looking for the code that allows you to use Google Translate in your app for FREE then go to the end of this post. :)

This is how we went about building it:

If you go to translate.google.com and try to translate a sentence for example:(just click on the image for better view)ggltranslate
Then for some one who has basic understanding of web might think that sending a request  of the form “translate.google.com/<source language code>/<target language code>/<sentence to be translated>” will get them the results , but this does not workout.

So we go a little bit deeper in our exploration, we use one of our favorite development tool “Firebug” to examine how the page responds to our actions. So now our plan is to observe the AJAX requests that the page makes. For observing these we Switch on our firebug and go to the ‘net’ tab:
google_translate_free1

In case it says its off then just switch it on and reload the page.

Now we try to change the text to anything we want and observe the requests being made. So we get something of this kind.
google_translate_free2

So these are the requests sent during our text change, now we need to figure out which one of these are useful for us. For this we checkout the requests going to translate.google.com and for which the status is ‘OK’, and this is what we observe:

google_translate_free3

TAdAA…!!! we got it !!
This was the url where the request was sent:

http://translate.google.com/translate_a/t?client=t&text=hi%20all&hl=en&sl=auto&tl=ta&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=0&tsel=0&uptl=ta&alttl=hi&sc=1

Now most of the work is done , the only thing that remains is to generalize the request url and extract the desired ans from the response which is pretty simple.

This is a small C# code snippet that does everything for you:

string text="i am loving it";
string url = "http://translate.google.com/translate_a/t?client=t&text=" + text.Replace(" ", "%20") + "&hl=en&sl=en&tl=ta&ie=UTF-8&oe=UTF-8&multires=1&otf=2&ssel=0&tsel=0&sc=1";
WebRequest wr = WebRequest.Create(url);
Stream resp= wr.GetResponse().GetResponseStream();
StreamReader respReader = new StreamReader(resp);
string s = "",ans=respReader.ReadToEnd();
string[] arr=ans.Split(',');
s = arr[0];
s=s.Substring(3);
return s;// this is the translated text!!

Just change the variable ‘text’ to the sentence you want to translate. For translation to the language of your choice just look up the language code with the help of firebug and substitute it in place of ‘ta’ in ‘tl=ta’ in the request url.
Have Fun!!!



只回答业务咨询点击这里给我发消息 点击这里给我发消息

王牌软件,兼职软件设计,软件修改,毕业设计。

本文出自 王牌软件,转载时请注明出处及相应链接。

本文永久链接: http://www.softwareace.cn/?p=472

0

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">


Ɣ回顶部

无觅相关文章插件,快速提升流量