<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                執行調用方法: ~~~ use support\lib\Translator; public function index() { $fieldVal = '想要進行順利的話,吉日是非常重要的。而且這也是一個訂婚紀念日,很適合被記住,快來看看2021年農歷九月二十是黃歷訂婚吉日嗎。'; $fieldVal = '<div>訂婚一定要選擇合適的日子,<span style="font-size:18px;">這個日子</span>需要經過精挑細選才可以,并且對將來的婚姻很有好處,</div><div>想要進行順利的話,吉日是非常重要的。而且這也是一個訂婚紀念日,很適合被記住,快來看看2021年農歷九月二十是黃歷訂婚吉日嗎。</div>'; $params = [ 'from' => 'zh', 'to' => 'en' ]; $res = Translator::translate($fieldVal, $params); return ($res); } ~~~ 類庫代碼: ~~~ <?php /*翻譯器*/ namespace support\lib; class Translator { private static function tranConfig() { $defaultConfig = [ 'interval' => '100', //調用間隔(毫秒,1000毫秒=1秒) 'wait' => '2', //失敗等待(秒) 'retry' => '1', //失敗重試次數 'pass_html' => true, //是否開啟html過濾 'defaultApi' => 'baidu', //默認接口 'spareApi' => 'youdao', //備用接口 'baidu' => [ 'appid' => '', 'key' => '' ], 'youdao' => [ 'app_id' => '', 'key' => '' ] ]; $tranConfig = config('translate_config'); if (empty($tranConfig)) { $tranConfig = $defaultConfig; } return $tranConfig; } public static $all_langs = array( 'zh' => '中文', 'en' => '英語', 'fra' => '法語', 'jp' => '日語', 'kor' => '韓語', 'de' => '德語', 'ru' => '俄語', 'spa' => '西班牙語', 'pt' => '葡萄牙語', 'it' => '意大利語', 'ara' => '阿拉伯語', 'th' => '泰語', 'el' => '希臘語', 'nl' => '荷蘭語', 'pl' => '波蘭語', 'bul' => '保加利亞語', 'est' => '愛沙尼亞語', 'dan' => '丹麥語', 'fin' => '芬蘭語', 'cs' => '捷克語', 'rom' => '羅馬尼亞語', 'slo' => '斯洛文尼亞語', 'swe' => '瑞典語', 'hu' => '匈牙利語', 'tr' => '土耳其語', 'id' => '印尼語', 'ms' => '馬來西亞語', 'vie' => '越南語', 'yue' => '粵語', 'wyw' => '文言文', 'cht' => '繁體中文' ); public static $allow_langs = array( 'baidu' => array( 'zh' => 'zh', 'en' => 'en', 'fra' => 'fra', 'jp' => 'jp', 'kor' => 'kor', 'de' => 'de', 'ru' => 'ru', 'spa' => 'spa', 'pt' => 'pt', 'it' => 'it', 'ara' => 'ara', 'th' => 'th', 'el' => 'el', 'nl' => 'nl', 'pl' => 'pl', 'bul' => 'bul', 'est' => 'est', 'dan' => 'dan', 'fin' => 'fin', 'cs' => 'cs', 'rom' => 'rom', 'slo' => 'slo', 'swe' => 'swe', 'hu' => 'hu', 'vie' => 'vie', 'yue' => 'yue', 'wyw' => 'wyw', 'cht' => 'cht' ), 'youdao' => array( 'zh' => 'zh-CHS', 'en' => 'en', 'jp' => 'ja', 'kor' => 'ko', 'fra' => 'fr', 'spa' => 'es', 'pt' => 'pt', 'it' => 'it', 'ru' => 'ru', 'vie' => 'vi', 'de' => 'de', 'ara' => 'ar', 'id' => 'id', 'it' => 'it' ), 'qq' => array( 'zh' => 'zh', 'en' => 'en', 'jp' => 'jp', 'kor' => 'kr', 'de' => 'de', 'fra' => 'fr', 'spa' => 'es', 'it' => 'it', 'tr' => 'tr', 'ru' => 'ru', 'pt' => 'pt', 'vie' => 'vi', 'id' => 'id', 'ms' => 'ms', 'th' => 'th', 'cht' => 'zh-TW' ) ); /*翻譯入口*/ public static function translate($fieldVal, $params) { $transConf = self::tranConfig(); $pass_html = $transConf['pass_html']; static $regEmpty = '/^([\s\r\n]|\&nbsp\;)*$/'; //開啟html過濾 if ($pass_html == true) { $htmlMd5List = []; $txtMd5List = []; static $tagRegs = array( '/<\![\s\S]*?>/', '/<(script|style)[^\r\n]*?>[\s\S]*?<\/\1>/i', '/<[\/]*\w+\b[^\r\n]*?>/' ); foreach ($tagRegs as $tagReg) { $fieldVal = preg_replace_callback($tagReg, function ($mhtml) use (&$htmlMd5List) { $key = '{'.md5($mhtml[0]).'}'; $htmlMd5List[$key] = $mhtml[0]; return $key; }, $fieldVal); } if (empty($htmlMd5List)) { //不包含html代碼 if ( ! empty($fieldVal) && ! preg_match($regEmpty, $fieldVal)) { $fieldVal = self::execute_translate($fieldVal, $params['from'], $params['to']); } } else { //包含html代碼 $fieldVal = preg_replace_callback('/([\s\S]*?)(\{[a-zA-Z0-9]{32}\})/i', function ($mtxt) use (&$txtMd5List) { $key = '['.md5($mtxt[1]).']'; $txtMd5List[$key] = $mtxt[1]; return $key.$mtxt[2]; }, $fieldVal); foreach ($txtMd5List as $k => $v) { if ( ! empty($v) && ! preg_match($regEmpty, $v)) { $txtMd5List[$k] = self::execute_translate($v, $params['from'], $params['to']); } } if ( ! empty($txtMd5List)) { $fieldVal = str_replace(array_keys($txtMd5List), $txtMd5List, $fieldVal); } if ( ! empty($htmlMd5List)) { $fieldVal = str_replace(array_keys($htmlMd5List), $htmlMd5List, $fieldVal); } } } else { if ( ! empty($fieldVal) && ! preg_match($regEmpty, $fieldVal)) { $fieldVal = self:: execute_translate($fieldVal, $params['from'], $params['to']); } } return $fieldVal; } public static function translate_f($q, $from, $to, $returnState = false) { $transConf = self::tranConfig(); if (empty($from) || empty($to)) { return $q; } $apiType = strtolower($transConf['defaultApi']); $spareType = strtolower($transConf['spareApi']); if (empty($apiType)) { return $q; } $allowLangs = self::$allow_langs[$apiType]; if (empty($allowLangs)) { return $q; } $from = $allowLangs[$from]; $to = $allowLangs[$to]; if (empty($from) || empty($to)) { return $q; } if ($from == $to) { return $q; } //默認線路執行失敗后,執行備用線路 $return = self::sendTranslate($q, $apiType, $from, $to); if ( ! empty($return) && $return['success'] === false) { $return = self::sendTranslate($q, $spareType, $from, $to); } if ($returnState) { return $return; } else { return empty($return['success']) ? $q : $return['data']; } } public static function sendTranslate($q, $apiType, $from, $to) { switch ($apiType) { case "baidu"; $return = self::api_baidu($q, $from, $to); break; case "youdao"; $return = self::api_youdao($q, $from, $to); break; case "qq"; $return = self::api_qq($q, $from, $to); break; } return $return; } /*百度翻譯接口*/ public static function api_baidu($q, $from, $to) { $apiConf = self::tranConfig()['baidu']; $salt = time(); $sign = $apiConf['appid'].$q.$salt.$apiConf['key']; $sign = md5($sign); $url = "https://api.fanyi.baidu.com/api/trans/vip/translate"; $postData = [ 'from' => $from, 'to' => $to, 'appid' => $apiConf['appid'], 'salt' => $salt, 'sign' => $sign, 'q' => $q ]; $data = curl_post($url, $postData); $data = json_decode($data, true); $return = array('success' => false); if ( ! empty($data['error_code'])) { $return['error'] = 'error:'.$data['error_code'].'-'.$data['error_msg']; } else { $transData = ''; foreach ($data['trans_result'] as $trans) { $transData .= $trans['dst']."\r\n"; } if ($transData) { $return['success'] = true; $return['data'] = $transData; } } return $return; } /*有道翻譯接口*/ public static function api_youdao($q, $from, $to) { $apiConf = self::tranConfig()['youdao']; $salt = time(); $sign = $apiConf['app_id'].$q.$salt.$apiConf['key']; $sign = md5($sign); $url = "https://openapi.youdao.com/api"; $postData = [ 'from' => $from, 'to' => $to, 'appKey' => $apiConf['app_id'], 'salt' => $salt, 'sign' => $sign, 'q' => $q ]; $data = curl_post($url, $postData); $data = json_decode($data, true); $return = array('success' => false); if ( ! empty($data['errorCode'])) { $return['error'] = 'error:'.$data['errorCode']; } else { $transData = ''; foreach ($data['translation'] as $trans) { $transData .= $trans."\r\n"; } if ($transData) { $return['success'] = true; $return['data'] = $transData; } } return $return; } /*騰訊翻譯接口*/ public static function api_qq($q, $from, $to) { $apiConf = self::tranConfig()['qq']; $SecretId = $apiConf['secretid']; $SecretKey = $apiConf['secretkey']; $param = array(); $param["Nonce"] = rand(); $param["Timestamp"] = time(); $param["Region"] = "ap-shanghai"; $param["SecretId"] = $SecretId; $param["Action"] = "TextTranslate"; $param["Version"] = "2018-03-21"; $param["SourceText"] = $q; $param["Source"] = $from; $param["Target"] = $to; $param['ProjectId'] = '0'; ksort($param); $signStr = "GETtmt.ap-shanghai.tencentcloudapi.com/?"; foreach ($param as $key => $value) { $signStr = $signStr.$key."=".$value."&"; } $signStr = substr($signStr, 0, -1); $param['Signature'] = base64_encode(hash_hmac("sha1", $signStr, $SecretKey, true)); $return = array('success' => false); ksort($param); $url = ''; foreach ($param as $key => $value) { $url = $url.$key."=".urlencode($value)."&"; } $url = trim($url, '&'); $data = get_html('https://tmt.'.$param["Region"].'.tencentcloudapi.com/?'.$url, null, null, 'utf-8'); $data = json_decode($data, true); if ( ! empty($data['Response']['TargetText'])) { $return['success'] = true; $return['data'] = $data['Response']['TargetText']; } return $return; } public static function get_api_langs($api) { $allowLangs = self::$allow_langs[$api]; if ( ! empty($allowLangs) && is_array($allowLangs)) { foreach ($allowLangs as $k => $v) { if (empty(self::$all_langs[$k])) { unset($allowLangs[$k]); } else { $allowLangs[$k] = self::$all_langs[$k]; } } } return is_array($allowLangs) ? $allowLangs : null; } /*數據處理:翻譯*/ private static function execute_translate($q, $from, $to) { static $retryCur = 0; $transConf = self::tranConfig(); $transConf['interval'] = intval($transConf['interval']); $transConf['wait'] = intval($transConf['wait']); $transConf['retry'] = intval($transConf['retry']); $retryMax = $transConf['retry']; $return = Translator::translate_f($q, $from, $to, true); if (is_array($return)) { if ($transConf['interval'] > 0) { usleep($transConf['interval'] * 1000); } if ( ! empty($return['success'])) { $retryCur = 0; $return = $return['data']; } else { if ($retryMax <= 0 || ($retryCur <= 0)) { return error_json('數據處理?翻譯失敗'); } if ($transConf['wait'] > 0) { sleep($transConf['wait']); } if ($retryMax > 0) { if ($retryCur < $retryMax) { $retryCur++; return self::execute_translate($q, $from, $to); } else { $retryCur = 0; return error_json('數據處理?翻譯:已重試'.$retryMax.'次,翻譯無效'); } } } } return $return; } } /** * 返回success_json數組 * @param $arr * @return string */ function success_json($msg = 'ok', $data = [], $count = 0, $url = '', $wait = 3) { $result = ['code' => 200, 'msg' => $msg, 'data' => $data]; return json($result); } /** * 返回success_json數組 * @param $arr * @return string */ function error_json($msg = 'error', $data = [], $url = '', $wait = 3) { $result = [ 'code' => 0, 'msg' => $msg, 'data' => $data, ]; return json($result); } ?> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看