translater/inc/class.translater.rest.php

70 lines
2.0 KiB
PHP
Raw Normal View History

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
/**
* Translater REST service.
*
* Admin service de retrieve translation of a string
* Queries come from translater jquery tools
*/
class translaterRest
{
2021-08-18 22:48:47 +00:00
public static function getProposal($core, $get)
{
2021-11-01 21:32:32 +00:00
$from = !empty($get['langFrom']) ? trim($get['langFrom']) : '';
$to = !empty($get['langTo']) ? trim($get['langTo']) : '';
$tool = !empty($get['langTool']) ? trim($get['langTool']) : '';
2021-08-18 22:48:47 +00:00
$str_in = !empty($get['langStr']) ? trim($get['langStr']) : '';
2021-08-18 19:42:30 +00:00
2021-11-01 21:32:32 +00:00
$str_in = text::toUTF8($str_in);
$str_in = trim($str_in);
2021-08-18 22:48:47 +00:00
$str_out = '';
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
$rsp = new xmlTag();
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
try {
if (empty($from) || empty($to) || empty($tool)) {
throw new Exception(__('Missing params'));
}
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
$translater = new dcTranslater($core);
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
if (!empty($str_in)) {
if (!$translater->proposal->hasTool($tool)) {
throw new Exception(__('Failed to get translation tool'));
}
if (!$translater->proposal->getTool($tool)->isActive()) {
throw new Exception(__('Translation tool is not configured'));
}
2021-08-18 19:42:30 +00:00
2021-11-01 21:32:32 +00:00
$str_out = (string) $translater->proposal->getTool($tool)->translate($str_in, $from, $to);
2021-08-18 22:48:47 +00:00
}
2021-08-18 19:42:30 +00:00
2021-11-01 21:32:32 +00:00
$x = new xmlTag('proposal');
2021-08-18 22:48:47 +00:00
$x->lang_from = $from;
2021-11-01 21:32:32 +00:00
$x->lang_to = $to;
$x->tool = $tool;
$x->str_from = $str_in;
$x->str_to = text::toUTF8(html::decodeEntities($str_out));
2021-08-18 22:48:47 +00:00
$rsp->insertNode($x);
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
2021-11-01 21:32:32 +00:00
2021-08-18 22:48:47 +00:00
return $rsp;
}
2021-11-01 21:32:32 +00:00
}