Usage of Google Translator Api For Free !!!!!!!

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

Hello Friends,

I found a hack to use Google Translator services.

As I was working on my scrapper project, category titles were  needed to translated from Dutch to English and we do not wanted to use any paid service.

I have tracked the requests made From   translate.google.com  and gathered them, then fetched responses from my curl function which I have Posted before .

below is the final code. It is working perfectly. Please let the page loaded for proper source code view.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
function curl($url,$params = array(),$is_coockie_set = false)
{
if(!$is_coockie_set){
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}
$str = ''; $str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);
/* STEP 3. visit cookiepage.php */
$Url = $url.$str;
$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
return $output;
}
function Translate($word,$conversion = 'hi_to_en')
{
$word = urlencode($word);
// dutch to english
if($conversion == 'nl_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';
// english to hindi
if($conversion == 'en_to_hi')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1';
// hindi to english
if($conversion == 'hi_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=hi&tl=en&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1';
//$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';
$name_en = curl($url);
$name_en = explode('"',$name_en);
return  $name_en[1];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo "<br><br> Hindi To English <br>";
echo  Translate('कानूनी नोटिस: यह गूगल के अनुवादक सेवाओं की एक दुरुपयोग है, आप इस के लिए भुगतान करना होगा.');
echo "<br><br> English To Hindi <br> ";
echo  Translate('legal notice: This is an abuse of google translator services ,  you must pay for this.','en_to_hi');
echo "<br><br> Dutch To English <br>";
echo  Translate('Disclaimer: Dit is een misbruik van Google Translator diensten, moet u betalen.','nl_to_en');
echo "<br><br> Just Kidding ....... <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)"> ";
?>
</body>
</html>

Copy this code in php file and hit it.
You will need to enable curl for this code to work.

Currently It supports Three translations

1)Hindi To English       2)English To Hindi       3) Dutch To English

If you want to add more translations Follow the steps below

1) Go to http://translate.google.com/ by Mozilla Firefox

2) open firebug and type any thing them copy the URL of Ajax request made by the browser.

3) Place it in the translate function with proper condition and replace &text parameter in the url with $word variable.
Let me know if it is helpful or give your valuable suggestions.

And Please don’t tell this to Google Ha ha …….

AS per rob’s suggestion I am placing the  updated translate function to support all kind of languages supported by Google. I have not tested it but hope it works fine

1
2
3
4
5
6
7
8
9
10
11
function Translate($word,$conversion = 'hi_to_en')
{
$word = urlencode($word);
$arr_langs = explode(‘_to_’, $conversion);
 $url = “http://translate.google.com/translate_a/t?client=t&text=$word&hl=".$arr_langs[1]."&sl=".$arr_langs[0]."&tl=".$arr_langs[1]."&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1″;
$name_en = curl($url);
$name_en = explode('"',$name_en);
return  $name_en[1];
}

Thanks And Regards,
Rupesh Patel

Rate this:

27 Votes

 

Share this:

Like this:

87 comments to Usage of Google Translator Api For Free !!!!!!!

  1. Steve Teale says:

    I was in character encoding hell with this for a while until I spotted an alternate URL in the comments. My browser is set to default UTF-8. Works better with …&ie=UTF-8&oe=UTF-8&multires…

    You may see what I mean if you use one of the muli-sentence variants using source:

    Your dog ate my cat. How come? What the hell!

    Try translating from en to es.

    2
    1
    Rate This

     

  2. w2009 says:

    Hi mate,

    I have a “fatal error: Call to undefined function curl_init() on line 11″… Do you have any idea why it doesn’t work for me…?

    Thx…;-)

    0
    0
    Rate This

     

    • curl is disable in your server … google “enable curl in php”

      0
      0
      Rate This

       

      • w2009 says:

        Sorry, I’m stupid… I should UTFG next time;-) Works fine now…The only problem seems to be with a character limit (already mentioned above by Guilherme Cunha etc.)… Have you dealt with it…?

        0
        0
        Rate This

         



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

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

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

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

0

3条评论

  1. admin 2013 年 6 月 19 日 下午 2:16 回复

    http://rupeshpatel.wordpress.com/2012/06/23/usage-of-google-translator-api-for-free/

  2. admin 2013 年 6 月 19 日 下午 2:21 回复

    http://www.codeproject.com/Articles/12711/Google-Translator

  3. admin 2013 年 8 月 20 日 下午 7:42 回复

    http://sourceforge.net/projects/onlinetranslate/?source=dlp

发表评论

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

您可以使用这些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="">


Ɣ回顶部

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