translater/inc/class.translater.rest.php

69 lines
2.1 KiB
PHP
Raw Normal View History

2021-08-18 19:42:30 +00:00
<?php
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of translater, a plugin for Dotclear 2.
#
2021-08-19 21:11:14 +00:00
# Copyright (c) 2009-2021 Jean-Christian Denis and contributors
2021-08-18 19:42:30 +00:00
#
# Licensed under the GPL version 2.0 license.
# A copy of this license is available in LICENSE file or at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK ------------------------------------
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)
{
$from = !empty($get['langFrom']) ? trim($get['langFrom']) : '';
$to = !empty($get['langTo']) ? trim($get['langTo']) : '';
$tool = !empty($get['langTool']) ? trim($get['langTool']) : '';
$str_in = !empty($get['langStr']) ? trim($get['langStr']) : '';
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
$str_in = text::toUTF8($str_in);
$str_in = trim($str_in);
$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-08-18 22:48:47 +00:00
$str_out = (string) $translater->proposal->getTool($tool)->translate($str_in ,$from, $to);
}
2021-08-18 19:42:30 +00:00
2021-08-18 22:48:47 +00:00
$x = new xmlTag('proposal');
$x->lang_from = $from;
$x->lang_to = $to;
$x->tool = $tool;
$x->str_from = $str_in;
$x->str_to = text::toUTF8(html::decodeEntities($str_out));
$rsp->insertNode($x);
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
return $rsp;
}
}