Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Controller/API/App/EngineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function add(): void {
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters[ 'apikey' ] = $engineData[ 'secret' ];
$newEngineStruct->extra_parameters[ 'service' ] = $engineData[ 'service' ];
$newEngineStruct->extra_parameters[ 'provider' ] = $engineData[ 'provider' ];
$newEngineStruct->extra_parameters[ 'providerkey' ] = $engineData[ 'providerkey' ];
$newEngineStruct->extra_parameters[ 'providercategory' ] = $engineData[ 'providercategory' ];
Expand Down
67 changes: 40 additions & 27 deletions lib/Utils/Engines/Intento.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Engines_Intento extends Engines_AbstractEngine {
];

private $apiKey;
private $service = null;
private $provider = [];
private $providerKey;
private $providerCategory;
Expand All @@ -26,10 +27,11 @@ public function __construct( $engineRecord ) {

$extra = $engineRecord->getExtraParamsAsArray();

$this->apiKey = $extra['apikey'] ?? null;
$this->provider = $extra['provider'] ?? [];
$this->providerKey = $extra['providerkey'] ?? null;
$this->providerCategory = $extra['providercategory'] ?? null;
$this->apiKey = $extra[ 'apikey' ] ?? null;
$this->service = $extra[ 'service' ] ?? null;
$this->provider = $extra[ 'provider' ] ?? [];
$this->providerKey = $extra[ 'providerkey' ] ?? null;
$this->providerCategory = $extra[ 'providercategory' ] ?? null;
}

/**
Expand Down Expand Up @@ -60,32 +62,32 @@ protected function _decode( $rawValue, $parameters = null, $function = null ) {
if ( $result and isset( $result->id ) ) {
$id = $result->id;

if ( isset( $result->response ) and !empty($result->response) and isset( $result->done ) and $result->done == true ) {
if ( isset( $result->response ) and !empty( $result->response ) and isset( $result->done ) and $result->done == true ) {
$text = $result->response[ 0 ]->results[ 0 ];
$decoded = [
'data' => [
'translations' => [
[ 'translatedText' => $text ]
'data' => [
'translations' => [
[ 'translatedText' => $text ]
]
]
]
];

} elseif ( isset( $result->done ) and $result->done == false ) {
sleep( 2 );
$cnf = [ 'async' => true, 'id' => $id ];

return $this->_curl_async( $cnf, $parameters, $function );
} elseif ( isset( $result->error ) and !empty($result->error) ) {
} elseif ( isset( $result->error ) and !empty( $result->error ) ) {

$httpCode = $result->error->data[0]->response->body->error->code ?? 500;
$message = $result->error->data[0]->response->body->error->message ?? $result->error->reason ?? "Unknown error";
$httpCode = $result->error->data[ 0 ]->response->body->error->code ?? 500;
$message = $result->error->data[ 0 ]->response->body->error->message ?? $result->error->reason ?? "Unknown error";

$decoded = [
'error' => [
'code' => -2,
'message' => $message,
'http_code' => $httpCode
]
'error' => [
'code' => -2,
'message' => $message,
'http_code' => $httpCode
]
];
} else {
$cnf = [ 'async' => true, 'id' => $id ];
Expand Down Expand Up @@ -121,35 +123,46 @@ protected function _decode( $rawValue, $parameters = null, $function = null ) {

}

return $this->_composeMTResponseAsMatch($parameters[ 'context' ][ 'text' ], $decoded);
return $this->_composeMTResponseAsMatch( $parameters[ 'context' ][ 'text' ], $decoded );
}

/**
* Get matches
*
* @param $_config
*
* @return array|Engines_Results_AbstractResponse
* @throws Exception
*/
public function get( $_config ) {

$_config[ 'source' ] = $this->_fixLangCode( $_config[ 'source' ] );
$_config[ 'target' ] = $this->_fixLangCode( $_config[ 'target' ] );
$_config[ 'source' ] = $this->_fixLangCode( $_config[ 'source' ] );
$_config[ 'target' ] = $this->_fixLangCode( $_config[ 'target' ] );

$parameters = [];
if ( !empty($this->apiKey) ) {
if ( !empty( $this->apiKey ) ) {
$_headers = [ 'apikey: ' . $this->apiKey, 'Content-Type: application/json' ];
}

$parameters[ 'context' ][ 'from' ] = $_config[ 'source' ];
$parameters[ 'context' ][ 'to' ] = $_config[ 'target' ];
$parameters[ 'context' ][ 'text' ] = $_config[ 'segment' ];
$service = $this->service;
$provider = $this->provider;
$providerKey = $this->providerKey;
$providerCategory = $this->providerCategory;

if ( !empty($provider) ) {
if ( !empty( $service ) ) {
$parameters[ 'service' ][ 'routing' ] = $service;
} elseif ( !empty( $provider ) ) {
$parameters[ 'service' ][ 'async' ] = true;
$parameters[ 'service' ][ 'provider' ] = $provider['id'];
$parameters[ 'service' ][ 'provider' ] = $provider[ 'id' ];

if ( !empty($providerKey) ) {
$parameters[ 'service' ][ 'auth' ][ $provider['id'] ] = [json_decode( $providerKey, true )];
if ( !empty( $providerKey ) ) {
$parameters[ 'service' ][ 'auth' ][ $provider[ 'id' ] ] = [ json_decode( $providerKey, true ) ];
}

if ( !empty($providerCategory) ) {
if ( !empty( $providerCategory ) ) {
$parameters[ 'context' ][ 'category' ] = $providerCategory;
}
}
Expand All @@ -173,7 +186,7 @@ public function get( $_config ) {
protected function _curl_async( $config, $parameters = null, $function = null ) {
$id = $config[ 'id' ];

if ( !empty($this->apiKey) ) {
if ( !empty( $this->apiKey ) ) {
$_headers = [ 'apikey: ' . $this->apiKey, 'Content-Type: application/json' ];
}

Expand Down