-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTranslateGlossary.php
More file actions
197 lines (166 loc) · 6.63 KB
/
TranslateGlossary.php
File metadata and controls
197 lines (166 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php namespace ProcessWire;
use DeepL\DeepLException;
use DeepL\MultilingualGlossaryDictionaryEntries;
use DeepL\MultilingualGlossaryInfo;
class TranslateGlossary {
private ?\DeepL\DeepLClient $client;
private ?ProcessTranslatePage $module;
private ?MultilingualGlossaryInfo $glossary;
private string $deepLGlossaryName;
public function __construct(
\DeepL\DeepLClient $client,
ProcessTranslatePage $module,
?string $glossaryId,
Language $sourceLanguage,
bool $isFreeAccount = false
) {
$this->client = $client;
$this->module = $module;
$this->deepLGlossaryName = wire('config')->httpHost;
$this->glossary = $this->setGlossary($glossaryId);
if (!$this->glossary) {
if ($isFreeAccount) {
$existing = $this->getGlossaries();
if (!empty($existing)) {
$this->module->warning($this->module->_('DeepL free plan: an existing glossary was found. Select it in module settings to use it for translations.'));
} else {
$this->glossary = $this->createGlossary($sourceLanguage);
if ($this->glossary) {
$this->saveGlossaryId($this->glossary->glossaryId);
}
}
} else {
$this->glossary = $this->createGlossary($sourceLanguage);
if ($this->glossary) {
$this->saveGlossaryId($this->glossary->glossaryId);
}
}
}
}
public function createGlossary(Language $sourceLanguage): ?MultilingualGlossaryInfo {
$dictionaries = [];
foreach (wire('languages') as $language) {
if ($language->id !== $sourceLanguage->id) {
$entryArr = self::convertGlossaryStringToArray($language->translate_glossary ?? '');
if (empty($entryArr)) {
continue;
}
if (!$language->translate_locale) {
continue;
}
try {
$dictionaries[] = new MultilingualGlossaryDictionaryEntries(self::sanitizeLocale($sourceLanguage->translate_locale), self::sanitizeLocale($language->translate_locale), $entryArr);
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
}
}
}
if (empty($dictionaries)) {
return null;
}
try {
$this->glossary = $this->client->createMultilingualGlossary($this->deepLGlossaryName, $dictionaries);
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
return null;
}
return $this->glossary;
}
public function setGlossary(?string $id = ''): ?MultilingualGlossaryInfo {
if (!$id) {
return null;
}
try {
return $this->glossary = $this->client->getMultilingualGlossary($id);
} catch (\DeepL\GlossaryNotFoundException) {
$this->module->warning($this->module->_('Stored DeepL glossary not found, it may have been deleted. A new one will be created if possible.'));
$this->saveGlossaryId(null);
return null;
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
return null;
}
}
public function getGlossary(): ?MultilingualGlossaryInfo {
return $this->glossary;
}
public function getGlossaries(): ?array {
try {
return $this->client->listMultilingualGlossaries();
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
return null;
}
}
public function deleteGlossary(): void {
if (!$this->glossary) {
return;
}
try {
$this->client->deleteMultilingualGlossary($this->glossary);
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
return;
}
$this->saveGlossaryId(null);
$this->glossary = null;
}
public function setGlossaryDictionary(string $dictionaryString, string $sourceLanguage, string $targetLanguage): void {
if (!$this->glossary) {
return;
}
$glossaryArray = $this->convertGlossaryStringToArray($dictionaryString);
try {
if (empty($glossaryArray)) {
$this->client->deleteMultilingualGlossaryDictionary($this->glossary, null, self::sanitizeLocale($sourceLanguage), self::sanitizeLocale($targetLanguage));
} else {
$dictionaryEntries = new MultilingualGlossaryDictionaryEntries(self::sanitizeLocale($sourceLanguage), self::sanitizeLocale($targetLanguage), $glossaryArray);
$this->client->replaceMultilingualGlossaryDictionary($this->glossary, $dictionaryEntries);
}
} catch (DeepLException $e) {
$this->module->error($e->getMessage());
}
}
public static function sanitizeLocale(string $locale): string {
$locale = strtoupper(str_replace('_', '-', $locale));
// DeepL glossary API uses bare EN for all English regional variants
if (str_starts_with($locale, 'EN-')) {
return 'EN';
}
return $locale;
}
public function dictionaryExists(string $sourceLanguage, string $targetLanguage): bool {
if (!$this->glossary) {
return false;
}
try {
$this->client->getMultilingualGlossaryEntries($this->glossary, self::sanitizeLocale($sourceLanguage), self::sanitizeLocale($targetLanguage));
} catch (DeepLException) {
return false;
}
return true;
}
private function saveGlossaryId(?string $id): void {
$data = wire('modules')->getConfig('ProcessTranslatePage');
$data['deepLGlossaryId'] = $id;
wire('modules')->saveConfig('ProcessTranslatePage', $data);
}
private static function convertGlossaryStringToArray(string $glossaryString = ''): array {
if (!$glossaryString) {
return [];
}
$glossaryArr = [];
$rows = explode(PHP_EOL, $glossaryString);
foreach ($rows as $row) {
if (trim($row) === '') {
continue;
}
$entry = explode('==', $row);
$entry = array_map(fn($val) => trim($val), $entry);
if (isset($entry[0]) && isset($entry[1]) && $entry[0] && $entry[1]) {
$glossaryArr[$entry[0]] = $entry[1];
}
}
return $glossaryArr;
}
}