key = dcCore::app()->blog->settings->translater->translater_google_proposal_key; $this->setName(__('Google')); $this->setDesc(__('Google Translation Tool API')); $this->setActive(!empty($this->key)); } public function form() { return '
' . '' . __('You must have on Google API console:') . '
' . ''; } public function save() { $key = empty($_POST['translater_google_proposal_key']) ? '' : $_POST['translater_google_proposal_key']; dcCore::app()->blog->settings->translater->put('translater_google_proposal_key', $key, 'string', '', true, true); } public function translate($str, $from, $to) { try { $data = [ 'key' => $this->key, 'q' => $str, 'source' => $from, 'target' => $to, ]; $path = ''; $client = netHttp::initClient($this->api, $path); $client->setUserAgent($this->agent); $client->useGzip(false); $client->setPersistReferers(false); $client->get($path, $data); $rs = $client->getContent(); if ($client->getStatus() != 200) { throw new Exception(__('Failed to query service.')); } if (null === ($dec = json_decode($rs))) { throw new Exception('Failed to decode result'); } if ('' == @$dec->data->translations[0]->translatedText) { throw new Exception('No data response'); } return $dec->data->translations[0]->translatedText; } catch (Exception $e) { } return ''; } }