记录学习与后端知识并分享学习代码过程(会飞的鱼Blog)

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

会飞的鱼 0 3116 2017年9月26日

今天是农历丁酉年正月初三,首先给各位补上一句新年好!

之前因为过春节忙得不可开交,博客又断更了几天。

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

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

<?php  
// +----------------------------------------------------------------------  
// | PHP MVC FrameWork v1.0 在线翻译类 使用百度翻译接口 无需申请Api Key  
// +----------------------------------------------------------------------  
// | Copyright (c) 2014-2099 http://qiling.org All rights reserved.  
// +----------------------------------------------------------------------  
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  
// +----------------------------------------------------------------------  
// | Author: qiling <70419470@qq.com> 2015年4月13日 下午2:22:15  
// +----------------------------------------------------------------------  
/** 
 * 在线翻译类 
 * @author qiling <70419470@qq.com> 
 */  
class Translate {  
    /** 
     * 支持的语种 
     * @var ArrayAccess 
     */  
    static $Lang = Array (  
            'auto' => '自动检测',  
            'ara' => '阿拉伯语',  
            'de' => '德语',  
            'ru' => '俄语',  
            'fra' => '法语',  
            'kor' => '韩语',  
            'nl' => '荷兰语',  
            'pt' => '葡萄牙语',  
            'jp' => '日语',  
            'th' => '泰语',  
            'wyw' => '文言文',  
            'spa' => '西班牙语',  
            'el' => '希腊语',  
            'it' => '意大利语',  
            'en' => '英语',  
            'yue' => '粤语',  
            'zh' => '中文'   
    );  
    /** 
     * 获取支持的语种 
     * @return array 返回支持的语种 
     */  
    static function getLang() {  
        return self::$Lang;  
    }  
    /** 
     * 执行文本翻译 
     * @param string $text 要翻译的文本 
     * @param string $from 原语言语种 默认:中文 
     * @param string $to 目标语种 默认:英文 
     * @return boolean string 翻译失败:false 翻译成功:翻译结果 
     */  
    static function exec($text, $from = 'zh', $to = 'en') {  
        // http://fanyi.baidu.com/v2transapi?from=zh&query=%E7%94%A8%E8%BD%A6%E8%B5%84%E8%AE%AF&to=fra  
        $url = "http://fanyi.baidu.com/v2transapi";  
        $data = array (  
                'from' => $from,  
                'to' => $to,  
                'query' => $text   
        );  
        $data = http_build_query ( $data );  
        $ch = curl_init ();  
        curl_setopt ( $ch, CURLOPT_URL, $url );  
        curl_setopt ( $ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );  
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );  
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );  
        curl_setopt ( $ch, CURLOPT_POST, 1 );  
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );  
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );  
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );  
        $result = curl_exec ( $ch );  
        curl_close ( $ch );  

        $result = json_decode ( $result, true );  

        if (!isset($result ['trans_result'] ['data'] ['0'] ['dst'])){  
            return false;   
        }  
        return $result ['trans_result'] ['data'] ['0'] ['dst'];  
    }  
}  

// 使用示例:  
echo Translate::exec ( "你好,世界!" );  
本文由 @会飞的鱼 于 2017-9-26 发布在 会飞的鱼Blog,如无特别说明,本博文章均为原创,转载请保留出处。

网友评论

    暂无评论

会飞的鱼 V

一条会飞的鱼!

744 文章
7272 评论
1067 万 阅读
8年 博龄
最新文章
最新评论
xje
3个月前 (2024-01-07)

LayCenter_4.7.2_2022-07-07

标签

会飞的鱼 在线咨询

在线时间:9:00-22:00
周六、周日:14:00-22:00