-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcessTranslatePage.config.php
More file actions
304 lines (266 loc) · 13.9 KB
/
Copy pathProcessTranslatePage.config.php
File metadata and controls
304 lines (266 loc) · 13.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php namespace ProcessWire;
class ProcessTranslatePageConfig extends ModuleConfig {
// Parts of the code are adopted from the Jumplinks module, thx!
// Copyright (c) 2016-17, Mike Rockett
protected function buildInputField($fieldNameId, $meta) {
$field = wire('modules')->get($fieldNameId);
foreach ($meta as $metaNames => $metaInfo) {
$metaNames = explode('+', $metaNames);
foreach ($metaNames as $metaName) {
$field->$metaName = $metaInfo;
}
}
return $field;
}
public function getDefaults() {
return [
'translationProvider' => 'deepl',
'deepLApiKey' => '',
'deepLGlossaryId' => '',
'googleCredentialsJson' => '',
'googleProjectId' => '',
'sourceLanguage' => wire('languages')->get('default'),
'excludedTemplates' => [],
'excludedFields' => [],
'excludedLanguages' => [],
'writemode' => 'empty',
'showSingleTargetLanguageButtons' => false,
];
}
private function getLanguageOptions() {
$languageOptions = [];
foreach (wire('languages') as $language) {
$languageOptions[$language->name] = (string)$language->get('title|name');
}
return $languageOptions;
}
private function getTemplateOptions() {
$excludedTemplatesOptions = [];
if (wire('templates')) {
foreach (wire('templates') as $template) {
if ($template->flags && $template->flags === Template::flagSystem) {
continue;
}
$label = $template->label ? $template->label.' ('.$template->name.')' : $template->name;
$excludedTemplatesOptions[$template->name] = $label;
}
}
return $excludedTemplatesOptions;
}
private function getFieldOptions() {
$excludedFieldsOptions = [];
if (wire('fields')) {
foreach (wire('fields') as $field) {
if ($field->flags && $field->flags === Field::flagSystem) {
continue;
}
$label = $field->label ? $field->label.' ('.$field->name.')' : $field->name;
$excludedFieldsOptions[$field->name] = $label;
}
}
return $excludedFieldsOptions;
}
private function formatGlossaryLabel(\DeepL\MultilingualGlossaryInfo $glossary): string {
$pairs = array_map(
fn($d) => strtoupper($d->sourceLang) . '→' . strtoupper($d->targetLang) . ' (' . $d->entryCount . ')',
$glossary->dictionaries
);
return $glossary->name . ' · ' . implode(', ', $pairs) . ' · ' . $glossary->creationTime->format('Y-m-d');
}
public function getInputFields() {
$moduleConfig = $this->modules->getConfig('ProcessTranslatePage');
$inputfields = parent::getInputfields();
$provider = $moduleConfig['translationProvider'] ?? 'deepl';
$inputfields->add(
$this->buildInputField('InputfieldRadios', [
'name+id' => 'translationProvider',
'label' => $this->_('Translation Provider'),
'options' => [
'deepl' => 'DeepL',
'google' => 'Google Cloud Translation',
],
'value' => $provider,
'optionWidth' => 1,
'columnWidth' => 100,
])
);
// DeepL settings
if ($provider === 'deepl') {
$hasDeeplKey = isset($moduleConfig['deepLApiKey']) && $moduleConfig['deepLApiKey'] !== '';
$inputfields->add(
$this->buildInputField('InputfieldText', [
'name+id' => 'deepLApiKey',
'label' => $this->_('DeepL API Key'),
'description' => $this->_('A valid API key for DeepL. A DeepL developer account is need for this (https://www.deepl.com/pro/change-plan#developer)'),
'columnWidth' => $hasDeeplKey ? 50 : 100,
])
);
if ($hasDeeplKey) {
$deepL = new \DeepL\DeepLClient($moduleConfig['deepLApiKey']);
$isFree = \DeepL\Translator::isAuthKeyFreeAccount($moduleConfig['deepLApiKey']);
$storedId = $moduleConfig['deepLGlossaryId'] ?? '';
try {
$usage = $deepL->getUsage();
$count = number_format($usage->character->count, 0, '', '.');
$limit = number_format($usage->character->limit, 0, '', '.');
$percent = number_format($usage->character->count / $usage->character->limit * 100, 1, ',', '');
$deepLInfos = "{$count} of {$limit} characters used this month ({$percent}%).";
if ($usage->anyLimitReached()) {
$deepLInfos .= ' <span class="uk-text-danger">' . $this->_('Limit exceeded.') . '</span>';
} else {
$deepLInfos = '<span class="uk-text-primary">' . $deepLInfos . '</span>';
}
} catch (\DeepL\AuthorizationException) {
$deepLInfos = '<span class="uk-text-danger">' . $this->_('Authorization failed.') . '</span>';
} catch (\DeepL\DeepLException $e) {
$deepLInfos = '<span class="uk-text-danger">' . $e->getMessage() . '</span>';
}
$planLabel = $isFree ? $this->_('Free plan') : $this->_('Pro plan');
$deepLInfos .= ' <span class="uk-text-muted">· ' . $planLabel . '</span>';
$inputfields->add(
$this->buildInputField('InputfieldMarkup', [
'name+id' => 'deeplInfo',
'label' => $this->_('DeepL usage'),
'value' => $deepLInfos,
'columnWidth' => 50,
])
);
if ($isFree) {
$inputfields->add(
$this->buildInputField('InputfieldMarkup', [
'name+id' => 'deepLFreePlanNotice',
'skipLabel' => Inputfield::skipLabelHeader,
'value' => '<div class="uk-alert-warning" uk-alert><p><strong>' . $this->_('Free plan:') . '</strong> ' . $this->_('Your DeepL account supports only one glossary across all projects. If a glossary already exists, select it below — otherwise it will remain unused.') . '</p></div>',
'columnWidth' => 100,
])
);
}
try {
$glossaries = $deepL->listMultilingualGlossaries();
$glossaryIds = array_map(fn($g) => $g->glossaryId, $glossaries);
$storedIdFound = $storedId && in_array($storedId, $glossaryIds);
$options = ['' => '— ' . $this->_('Auto-create from language entries')];
foreach ($glossaries as $g) {
$options[$g->glossaryId] = $this->formatGlossaryLabel($g);
}
$glossaryDescription = $this->_('Choose which glossary to use, or let the module create one automatically when language glossary entries are saved.');
if ($storedId && !$storedIdFound) {
$options[$storedId] = $this->_('(not found)') . ' ' . $storedId;
$glossaryDescription = '<span class="uk-text-danger">' . $this->_('The stored glossary was not found on your DeepL account — it may have been deleted externally.') . '</span> ' . $glossaryDescription;
}
$inputfields->add(
$this->buildInputField('InputfieldSelect', [
'name+id' => 'deepLGlossaryId',
'label' => $this->_('Active Glossary'),
'description' => $glossaryDescription,
'options' => $options,
'value' => $storedId,
'columnWidth' => $storedIdFound ? 50 : 100,
])
);
if ($storedIdFound) {
$inputfields->add(
$this->buildInputField('InputfieldCheckbox', [
'name+id' => 'deepLGlossaryDelete',
'label' => $this->_('Delete glossary'),
'description' => $this->_('Permanently removes this glossary from your DeepL account on save. Entries in the language fields are kept and a new glossary will be created on the next translation.'),
'collapsed' => Inputfield::collapsedYes,
'columnWidth' => 50,
])
);
}
} catch (\DeepL\DeepLException) {
$inputfields->add(
$this->buildInputField('InputfieldText', [
'name+id' => 'deepLGlossaryId',
'label' => $this->_('DeepL Glossary ID'),
'description' => $this->_('ID of DeepL glossary. Will be automatically set if glossary fields in languages are filled out.'),
'collapsed' => Inputfield::collapsedYes,
'columnWidth' => 100,
])
);
}
}
}
// Google Cloud Translation settings
if ($provider === 'google') {
$hasCredentials = isset($moduleConfig['googleCredentialsJson']) && $moduleConfig['googleCredentialsJson'] !== '';
$inputfields->add(
$this->buildInputField('InputfieldTextarea', [
'name+id' => 'googleCredentialsJson',
'label' => $this->_('Google Service Account JSON'),
'description' => $this->_('Paste the full contents of your Google Cloud service account JSON key file. To create one: GCP Console → IAM & Admin → Service Accounts → create account with role "Cloud Translation API User" → Keys → Add Key → JSON.'),
'rows' => 6,
'collapsed' => $hasCredentials ? Inputfield::collapsedYes : Inputfield::collapsedNo,
'columnWidth' => 50,
])
);
$inputfields->add(
$this->buildInputField('InputfieldText', [
'name+id' => 'googleProjectId',
'label' => $this->_('Google Cloud Project ID'),
'description' => $this->_('The project ID from your Google Cloud Console (shown in the project selector at the top of the console).'),
'columnWidth' => 50,
])
);
}
$inputfields->add(
$this->buildInputField('InputfieldSelect', [
'name+id' => 'sourceLanguageName',
'label' => $this->_('Source Language'),
'description' => $this->_('The language which will be used to translate from. If no selection is made, the default language will be used'),
'options' => array_merge(['current_user_language' => $this->_('Current user language')], $this->getLanguageOptions()),
'columnWidth' => 33,
])
);
$inputfields->add(
$this->buildInputField('InputfieldRadios', [
'name+id' => 'writemode',
'label' => $this->_('Write mode'),
'notes' => $this->_('Caution: the »Changed fields« option support is currently only one level deep. If you change any value inside a Repeater (Matrix) or FieldsetPage field, the complete field will be translated'),
'options' => [
'empty' => $this->_('Translate only if target field is empty'),
'changed' => $this->_('Translate only changed fields'),
'all' => $this->_('Overwrite all target fields'),
],
'columnWidth' => 33,
])
);
$inputfields->add(
$this->buildInputField('InputfieldCheckbox', [
'name+id' => 'showSingleTargetLanguageButtons',
'label' => $this->_('Show single target language buttons'),
'description' => $this->_('If checked, the save dropdown will add one button for each allowed target language instead of one button for all languages combined.'),
'columnWidth' => 34,
])
);
$inputfields->add(
$this->buildInputField('InputfieldASMSelect', [
'name+id' => 'excludedTemplates',
'label' => $this->_('Excluded Templates'),
'description' => $this->_('Pages with these templates will not display the save + translate option in the save dropdown'),
'options' => $this->getTemplateOptions(),
'columnWidth' => 33,
])
);
$inputfields->add(
$this->buildInputField('InputfieldASMSelect', [
'name+id' => 'excludedFields',
'label' => $this->_('Excluded Fields'),
'description' => $this->_('Fields that will be ignored when translating'),
'options' => $this->getFieldOptions(),
'columnWidth' => 33,
])
);
$inputfields->add(
$this->buildInputField('InputfieldASMSelect', [
'name+id' => 'excludedLanguages',
'label' => $this->_('Excluded Languages'),
'description' => $this->_('Target languages that will be ignored when translating'),
'options' => $this->getLanguageOptions(),
'columnWidth' => 34,
])
);
return $inputfields;
}
}