|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BabyMarkt\DeepL; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Glossary capsules functionality provided by DeepL where one can configure a glossary/dictionary which |
|
7
|
|
|
* is used in translation. |
|
8
|
|
|
* |
|
9
|
|
|
* @see https://support.deepl.com/hc/en-us/articles/4405021321746-Managing-glossaries-with-the-DeepL-API |
|
10
|
|
|
*/ |
|
11
|
|
|
class Glossary |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* API URL: glossaries |
|
15
|
|
|
*/ |
|
16
|
|
|
const API_URL_RESOURCE_GLOSSARIES = 'glossaries'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ClientInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param $authKey |
|
25
|
|
|
* @param int $apiVersion |
|
26
|
|
|
* @param string $host |
|
27
|
|
|
* @param ClientInterface|null $client |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com', ClientInterface $client = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->client = $client ?? new Client($authKey, $apiVersion, $host); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Calls the glossary-Endpoint and return Json-response as an array |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
* @throws DeepLException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function listGlossaries() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->client->request($this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Creates a glossary, entries must be formatted as [sourceText => entryText] e.g: ['Hallo' => 'Hello'] |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param array $entries |
|
50
|
|
|
* @param string $sourceLang |
|
51
|
|
|
* @param string $targetLang |
|
52
|
|
|
* @param string $entriesFormat |
|
53
|
|
|
* @return array|null |
|
54
|
|
|
* @throws DeepLException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function createGlossary( |
|
57
|
|
|
string $name, |
|
58
|
|
|
array $entries, |
|
59
|
|
|
string $sourceLang = 'de', |
|
60
|
|
|
string $targetLang = 'en', |
|
61
|
|
|
string $entriesFormat = 'tsv' |
|
62
|
|
|
) { |
|
63
|
|
|
$formattedEntries = ""; |
|
64
|
|
|
foreach ($entries as $source => $target) { |
|
65
|
|
|
$formattedEntries .= sprintf("%s\t%s\n", $source, $target); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$paramsArray = [ |
|
69
|
|
|
'name' => $name, |
|
70
|
|
|
'source_lang' => $sourceLang, |
|
71
|
|
|
'target_lang' => $targetLang, |
|
72
|
|
|
'entries' => $formattedEntries, |
|
73
|
|
|
'entries_format' => $entriesFormat |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
|
77
|
|
|
$body = $this->client->buildQuery($paramsArray); |
|
78
|
|
|
|
|
79
|
|
|
return $this->client->request($url, $body); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Deletes a glossary |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $glossaryId |
|
86
|
|
|
* @return array|null |
|
87
|
|
|
* @throws DeepLException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function deleteGlossary(string $glossaryId) |
|
90
|
|
|
{ |
|
91
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
|
92
|
|
|
$url .= "/$glossaryId"; |
|
93
|
|
|
|
|
94
|
|
|
return $this->client->request($url, '', 'DELETE'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets information about a glossary |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $glossaryId |
|
101
|
|
|
* @return array|null |
|
102
|
|
|
* @throws DeepLException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function glossaryInformation(string $glossaryId) |
|
105
|
|
|
{ |
|
106
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
|
107
|
|
|
$url .= "/$glossaryId"; |
|
108
|
|
|
|
|
109
|
|
|
return $this->client->request($url, '', 'GET'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Fetch glossary entries and format them as associative array [source => target] |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $glossaryId |
|
116
|
|
|
* @return array |
|
117
|
|
|
* @throws DeepLException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function glossaryEntries(string $glossaryId) |
|
120
|
|
|
{ |
|
121
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
|
122
|
|
|
$url .= "/$glossaryId/entries"; |
|
123
|
|
|
|
|
124
|
|
|
$response = $this->client->request($url, '', 'GET'); |
|
125
|
|
|
|
|
126
|
|
|
$entries = []; |
|
127
|
|
|
if (!empty($response)) { |
|
128
|
|
|
$allEntries = preg_split('/\n/', $response); |
|
|
|
|
|
|
129
|
|
|
foreach ($allEntries as $entry) { |
|
130
|
|
|
$sourceAndTarget = preg_split('/\s+/', rtrim($entry)); |
|
131
|
|
|
if (isset($sourceAndTarget[0], $sourceAndTarget[1])) { |
|
132
|
|
|
$entries[$sourceAndTarget[0]] = $sourceAndTarget[1]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $entries; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|