<?php /** * @link http://www.joinf.com * @copyright Copyright (C) 2017 joinf.com. All rights reserved. */ namespace siteadmincontroller; use siteadminmodelLanguageModel; /** * 翻译管理 */ class TranslateController extends siteadmincomponentController { private $appid; private $seckey; private $_model; /** * 初始化 * @access public * @return void */ public function __construct() { $this->_model = new LanguageModel(); $translate=$this->_model->getTranslate(); if($translate){ $this->appid=$translate['appid']; $this->seckey=$translate['seckey']; }else{ $this->appid=''; $this->seckey=''; } } /** * 翻译接口 * * POST:source_id=原始语言ID,dest_ids=目标语言(数组格式),txts=数据源(数组格式) * * @access public * @return void */ public function toLanguage() { /* $this->request->post= [ 'source_id' => '1', 'dest_ids' => ["2",'3'], 'txts' =>['今天天气不错,挺风和日丽的','我们都有一个家,名字叫中国'], ]; */ //google翻译接口 $language_id=isset($this->request->post['source_id'])?$this->request->post['source_id']:''; $dest_ids=isset($this->request->post['dest_ids'])?$this->request->post['dest_ids']:''; $data = isset($this->request->post['txts']) ? $this->request->post['txts']: ''; if(empty($language_id)||empty($dest_ids)||empty($data)){ $this->flash('error', '参数错误'); return $this->formatResponse(); }else{ //获取源语种 $b_l=explode(',',$this->config->get('sys_google_translate_code')); $url=$this->config->get('sys_google_translate_url'); foreach ($b_l as $k=>$v){ if(($k+1)==$language_id){ $source=$v; } } $t_data=array(); foreach ($dest_ids as $kk=>$vv){ //计算翻译目标语种 foreach ($b_l as $k=>$v){ if(($k+1)==$vv){ $target=$v; } } //组装译文 $translist=array(); foreach ($data as $key=>$value){ //初始化 $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, $url); //设置头文件的信息作为数据流输出 curl_setopt($curl, CURLOPT_HEADER, 1); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置post方式提交 curl_setopt($curl, CURLOPT_POST, 1); //设置post数据 $post_data = array( "q" => $value, "source" => $source, "target" => $target, 'format' => 'text' ); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); //执行命令 $exec_data = curl_exec($curl); //关闭URL请求 curl_close($curl); //显示获得的数据 $result=json_decode(strstr($exec_data,'{'),true); $translist[]=$result['data']['translations'][0]['translatedText']; } //该语种全部译文 $t_data[$vv]=$translist; } $this->manageLogModel->add('多语言翻译'); $this->flash('success', '翻译成功'); $this->formatResponse($t_data); } //百度翻译接口 /* $language_id=isset($this->request->post['source_id'])?$this->request->post['source_id']:''; $dest_ids=isset($this->request->post['dest_ids'])?$this->request->post['dest_ids']:''; $data = isset($this->request->post['txts']) ? $this->request->post['txts']: ''; if(empty($language_id)||empty($dest_ids)||empty($data)){ $this->flash('error', '提交错误'); return $this->formatResponse(); }else{ //获取源语种 $b_l=explode(',',$this->config->get('sys_baidu_language_code')); foreach ($b_l as $k=>$v){ if(($k+1)==$language_id){ $from=$v; } } $t_data=array(); $count_num=0; foreach ($dest_ids as $kk=>$vv){ //计算翻译目标语种 foreach ($b_l as $k=>$v){ if(($k+1)==$vv){ $to=$v; } } //组装译文 $translist=array(); foreach ($data as $key=>$value){ $result=$this->translate($value,$from,$to); $translist[]=$result['trans_result'][0]['dst']; $count_num+=mb_strlen($value,'utf-8'); } //该语种全部译文 $t_data[$vv]=$translist; } $this->_model->updateTranslate($this->appid,$count_num,time()); $this->manageLogModel->add('多语言翻译'); $this->flash('success', '翻译成功'); $this->formatResponse($t_data); } */ } //翻译入口 public function translate($query, $from, $to){ $args = array( 'q' => $query, 'appid' => $this->appid, 'salt' => rand(10000,99999), 'from' => $from, 'to' => $to, ); $args['sign'] = $this->buildSign($query, $this->appid, $args['salt'], $this->seckey); $ret = $this->call($this->config->get('sys_baidu_url'), $args,'post',0,$this->config->get('sys_baidu_curl_timeout')); $ret = json_decode($ret, true); return $ret; } //加密 public function buildSign($query, $appID, $salt, $secKey){ $str = $appID . $query . $salt . $secKey; $ret = md5($str); return $ret; } //发起网络请求 public function call($url, $args=null, $method="post", $testflag = 0, $timeout = 0, $headers=array()){ $ret = false; $i = 0; while($ret === false) { if($i > 1) break; if($i > 0) { sleep(1); } $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers); $i++; } return $ret; } public function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = 0, $headers=array()){ $ch = curl_init(); if($method == "post") { $data = $this->convert($args); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POST, 1); } else { $data = convert($args); if($data) { if(stripos($url, "?") > 0) { $url .= "&$data"; } else { $url .= "?$data"; } } } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if(!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } if($withCookie) { curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE); } $r = curl_exec($ch); curl_close($ch); return $r; } public function convert(&$args){ $data = ''; if (is_array($args)) { foreach ($args as $key=>$val) { if (is_array($val)) { foreach ($val as $k=>$v) { $data .= $key.'['.$k.']='.rawurlencode($v).'&'; } } else { $data .="$key=".rawurlencode($val)."&"; } } return trim($data, "&"); } return $args; } }