首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享一个无需Api Key的在线翻译类

分享一个无需Api Key的在线翻译类

作者头像
夏时
发布2018-06-26 16:38:28
6980
发布2018-06-26 16:38:28
举报
文章被收录于专栏:夏时夏时

在新的一年里,我决定——少写博客多写代码……毕竟文字那是文人玩的东西,我一敲代码的要写点东西真是半天憋不出一个字,别说有多难受了!所以我今后会把重心转移到技术方面,争取做出更多作品,而博客的更新,可能会减少到一周一篇,至于博客的内容,更多的则会是记录我之后学习的过程以及相关学习笔记。

进入正题,今天分享一个 php 的翻译类模块,这个模块是之前在 thinkphp 的论坛里淘到的。小试了一下,效果非常不错!于是果断搬过来收藏。

  1. <?php  
  2. // +----------------------------------------------------------------------
  3. // | PHP MVC FrameWork v1.0 在线翻译类 使用百度翻译接口 无需申请Api Key
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2014-2099 http://qiling.org All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: qiling <70419470@qq.com> 2015年4月13日 下午2:22:15
  10. // +----------------------------------------------------------------------
  11. /**
  12.  * 在线翻译类
  13.  * @author qiling <70419470@qq.com>
  14.  */
  15. class Translate {  
  16. /**
  17.      * 支持的语种
  18.      * @var ArrayAccess
  19.      */
  20. static $Lang = Array (  
  21.             'auto' => '自动检测',  
  22.             'ara' => '阿拉伯语',  
  23.             'de' => '德语',  
  24.             'ru' => '俄语',  
  25.             'fra' => '法语',  
  26.             'kor' => '韩语',  
  27.             'nl' => '荷兰语',  
  28.             'pt' => '葡萄牙语',  
  29.             'jp' => '日语',  
  30.             'th' => '泰语',  
  31.             'wyw' => '文言文',  
  32.             'spa' => '西班牙语',  
  33.             'el' => '希腊语',  
  34.             'it' => '意大利语',  
  35.             'en' => '英语',  
  36.             'yue' => '粤语',  
  37.             'zh' => '中文'   
  38.     );  
  39. /**
  40.      * 获取支持的语种
  41.      * @return array 返回支持的语种
  42.      */
  43. static function getLang() {  
  44. return self::$Lang;  
  45.     }  
  46. /**
  47.      * 执行文本翻译
  48.      * @param string $text 要翻译的文本
  49.      * @param string $from 原语言语种 默认:中文
  50.      * @param string $to 目标语种 默认:英文
  51.      * @return boolean string 翻译失败:false 翻译成功:翻译结果
  52.      */
  53. static function exec($text, $from = 'zh', $to = 'en') {  
  54. // http://fanyi.baidu.com/v2transapi?from=zh&query=%E7%94%A8%E8%BD%A6%E8%B5%84%E8%AE%AF&to=fra
  55. $url = "http://fanyi.baidu.com/v2transapi";  
  56. $data = array (  
  57.                 'from' => $from,  
  58.                 'to' => $to,  
  59.                 'query' => $text
  60.         );  
  61. $data = http_build_query ( $data );  
  62. $ch = curl_init ();  
  63.         curl_setopt ( $ch, CURLOPT_URL, $url );  
  64.         curl_setopt ( $ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );  
  65.         curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );  
  66.         curl_setopt ( $ch, CURLOPT_HEADER, 0 );  
  67.         curl_setopt ( $ch, CURLOPT_POST, 1 );  
  68.         curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );  
  69.         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );  
  70.         curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );  
  71. $result = curl_exec ( $ch );  
  72.         curl_close ( $ch );  
  73. $result = json_decode ( $result, true );  
  74. if (!isset($result ['trans_result'] ['data'] ['0'] ['dst'])){  
  75. return false;   
  76.         }  
  77. return $result ['trans_result'] ['data'] ['0'] ['dst'];  
  78.     }  
  79. }  
  80. // 使用示例:
  81. echo Translate::exec ( "你好,世界!" );  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档