2021-08-18 19:42:30 +00:00
|
|
|
<?php
|
2021-09-02 21:44:01 +00:00
|
|
|
/**
|
|
|
|
* @brief translater, a plugin for Dotclear 2
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-02 21:44:01 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-02 21:44:01 +00:00
|
|
|
* @author Jean-Christian Denis & contributors
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-02 21:44:01 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
2021-08-18 22:48:47 +00:00
|
|
|
if (!defined('DC_CONTEXT_ADMIN')) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-18 19:42:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Google proposal tool.
|
|
|
|
*
|
|
|
|
* This uses google API v2 to translate strings
|
|
|
|
*/
|
|
|
|
class googleProposalTool extends translaterProposalTool
|
|
|
|
{
|
2021-11-01 21:32:32 +00:00
|
|
|
private $api = 'https://www.googleapis.com/language/translate/v2';
|
2021-08-18 22:48:47 +00:00
|
|
|
private $agent = 'dcTranslater - http://jcd.lv/?q=translater';
|
2021-11-01 21:32:32 +00:00
|
|
|
private $key = null; //ex: AsSDqsGsfdSDSQFQsfedj9bnzY390aIg-1d
|
2021-08-18 19:42:30 +00:00
|
|
|
|
2021-08-18 22:48:47 +00:00
|
|
|
protected function setup()
|
|
|
|
{
|
2022-11-13 17:40:31 +00:00
|
|
|
$this->key = dcCore::app()->blog->settings->translater->translater_google_proposal_key;
|
2021-08-18 22:48:47 +00:00
|
|
|
|
|
|
|
$this->setName(__('Google'));
|
|
|
|
$this->setDesc(__('Google Translation Tool API'));
|
|
|
|
$this->setActive(!empty($this->key));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function form()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
'<p><label class="classic" for="translater_google_proposal_key">' .
|
|
|
|
__('API Console Single Access Key') . '<br />' .
|
|
|
|
form::field('translater_google_proposal_key', 65, 255, $this->key) .
|
2021-11-01 21:32:32 +00:00
|
|
|
'</label></p>' .
|
2021-08-18 22:48:47 +00:00
|
|
|
'<p>' . __('You must have on Google API console:') . '</p>' .
|
|
|
|
'<ul>' .
|
|
|
|
'<li><a href="https://code.google.com/apis/console/#access">' . __('A single access API key') . '</a></li>' .
|
|
|
|
'<li><a href="https://code.google.com/apis/console/#services">' . __('Activate the "translate API" service') . '</a></li>' .
|
|
|
|
'</ul>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save()
|
|
|
|
{
|
2021-11-01 21:32:32 +00:00
|
|
|
$key = empty($_POST['translater_google_proposal_key']) ?
|
2021-08-18 22:48:47 +00:00
|
|
|
'' : $_POST['translater_google_proposal_key'];
|
|
|
|
|
2022-11-13 17:40:31 +00:00
|
|
|
dcCore::app()->blog->settings->translater->put('translater_google_proposal_key', $key, 'string', '', true, true);
|
2021-08-18 22:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function translate($str, $from, $to)
|
|
|
|
{
|
|
|
|
try {
|
2021-11-01 21:32:32 +00:00
|
|
|
$data = [
|
|
|
|
'key' => $this->key,
|
|
|
|
'q' => $str,
|
2021-08-18 22:48:47 +00:00
|
|
|
'source' => $from,
|
2022-11-13 17:40:31 +00:00
|
|
|
'target' => $to,
|
2021-11-01 21:32:32 +00:00
|
|
|
];
|
2021-08-18 22:48:47 +00:00
|
|
|
|
2021-11-01 21:32:32 +00:00
|
|
|
$path = '';
|
2021-08-18 22:48:47 +00:00
|
|
|
$client = netHttp::initClient($this->api, $path);
|
|
|
|
$client->setUserAgent($this->agent);
|
|
|
|
$client->useGzip(false);
|
|
|
|
$client->setPersistReferers(false);
|
2021-11-01 21:32:32 +00:00
|
|
|
$client->get($path, $data);
|
2021-08-18 22:48:47 +00:00
|
|
|
|
|
|
|
$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;
|
2021-11-01 21:32:32 +00:00
|
|
|
} catch (Exception $e) {
|
2021-08-18 22:48:47 +00:00
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
|
2021-08-18 22:48:47 +00:00
|
|
|
return '';
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
}
|