1)Google翻译接口
2)微信调用
3)效果展示
----------------------------------------------------------------------------------------
1)Google翻译接口
Google提供翻译的API接口,参见https://developers.google.com/translate/v2/getting_started
but
Google Translate API is a paid service.
so
想利用Google Translate API free charge 就要另外想办法了
Google提供免费的在线翻译功能,因此,可以通过Web发送翻译请求给Google,接收它的html返回,然后,通过分析html获取翻译后的文字.
function translate_web($text, $language="auto|en") { if (empty($text)) return false; $url = "http://google.cn/translate_t?ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $html=file_get_contents($url); // parse html // html souce: TTS_TEXT_SIZE_LIMIT=100;TRANSLATED_TEXT='世界,你好!';INPUT_TOOL_PATH='//www.google.com'; $mode= ("/TRANSLATED_TEXT='(.*)';INPUT_TOOL_PATH/"); if (preg_match($mode,$html,$out)){ return $out[1];//ret; } }
更有甚者,有人发现通过http://translate.google.com/translate_a/t?client=p与Google交互可以得到json返回,这就相当于API使用了
function translate_json($text, $language="auto|en") { if (empty($text)) return false; $url = "http://translate.google.cn/translate_a/t?client=p&ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $json=file_get_contents($url); $data = json_decode($json); return $data->sentences[0]->trans; }
Google翻译接口示例: http://download.csdn.net/detail/d_eng/6563915
这里要注意的有两个问题
1)编码问题,例子中都采用utf-8,连php文件的属性都是utf-8
2)google问题,google在中国不保证一直能连上,虽然有多个链接 google.com/google.cn/google.com.hk
2)微信调用
有了接口,在微信中调用就简单了
接口文件 translate_func.php (save as utf-8)
<?php /* 2 Google Translate interface 1) Google Translate WEB IF get translated text by parsing return html which code is GBK function translate_web() 2) Google Translate JSON IF get translated text which format=json(set client<>t)&default code=GBK function translate_json() d_eng ([email protected]) 2013-11-16 */ /* Google Translate WEB IF get translated text by parsing return html which code is GBK */ //header("Content-Type:text/html; charset=utf-8"); function translate_web($text, $language="auto|en") { if (empty($text)) return false; $url = "http://google.cn/translate_t?ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $html=file_get_contents($url); // parse html // html souce: TTS_TEXT_SIZE_LIMIT=100;TRANSLATED_TEXT='世界,你好!';INPUT_TOOL_PATH='//www.google.com'; $mode= ("/TRANSLATED_TEXT='(.*)';INPUT_TOOL_PATH/"); if (preg_match($mode,$html,$out)){ return $out[1];//ret; } } function translate_json($text, $language="auto|en") { if (empty($text)) return false; $url = "http://translate.google.cn/translate_a/t?client=p&ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $json=file_get_contents($url); $data = json_decode($json); return $data->sentences[0]->trans; } function with_chinese($text){ return preg_match('/[\x7f-\xff]/',$text); } function translate($text) { if (with_chinese($text)) { return translate_json($text,'zh-CN|en'); } else { return translate_json($text,'en|zh-CN'); } } ?>
调用代码片段
if ($RX_TYPE=="text") { include("translate_func.php"); $resultStr = $this->responseText($postObj, translate(trim($postObj->Content))); }
3)效果展示
以上就是微信公众平台开发--谷歌翻译的内容,更多相关内容请关注Work网(www.php.cn)!