|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2023 Atlas Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace BEdita\I18n\Aws\Core; |
|
16
|
|
|
|
|
17
|
|
|
use Aws\Translate\TranslateClient; |
|
18
|
|
|
use BEdita\I18n\Core\TranslatorInterface; |
|
19
|
|
|
use Cake\Log\LogTrait; |
|
20
|
|
|
use Exception; |
|
21
|
|
|
use Psr\Log\LogLevel; |
|
22
|
|
|
|
|
23
|
|
|
class Translator implements TranslatorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use LogTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The AWS API client. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Aws\Translate\TranslateClient |
|
31
|
|
|
*/ |
|
32
|
|
|
protected TranslateClient $awsClient; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The engine options. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected array $options = ['version' => 'latest']; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Setup translator engine. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $options The options |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setup(array $options = []): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->options = array_merge($options, $this->options); |
|
50
|
|
|
$this->awsClient = new TranslateClient($this->options); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Translate an array of texts $texts from language source $from to language target $to |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $texts The texts to translate |
|
57
|
|
|
* @param string $from The source language |
|
58
|
|
|
* @param string $to The target language |
|
59
|
|
|
* @param array $options The options |
|
60
|
|
|
* @return string The translation in json format, i.e. |
|
61
|
|
|
* { |
|
62
|
|
|
* "translation": [ |
|
63
|
|
|
* "<translation of first text>", |
|
64
|
|
|
* "<translation of second text>", |
|
65
|
|
|
* [...] |
|
66
|
|
|
* "<translation of last text>" |
|
67
|
|
|
* ] |
|
68
|
|
|
* } |
|
69
|
|
|
*/ |
|
70
|
|
|
public function translate(array $texts, string $from, string $to, array $options = []): string |
|
71
|
|
|
{ |
|
72
|
|
|
$translation = []; |
|
73
|
|
|
try { |
|
74
|
|
|
$translation = []; |
|
75
|
|
|
foreach ($texts as $key => $text) { |
|
76
|
|
|
$translation[$key] = $this->awsClient->translateText([ |
|
77
|
|
|
'SourceLanguageCode' => $from, |
|
78
|
|
|
'TargetLanguageCode' => $to, |
|
79
|
|
|
'Text' => $text, |
|
80
|
|
|
])['TranslatedText']; |
|
81
|
|
|
} |
|
82
|
|
|
} catch (Exception $e) { |
|
83
|
|
|
$this->log($e->getMessage(), LogLevel::ERROR); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return (string)json_encode(compact('translation')); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|