Skip to content

Commit 7266430

Browse files
author
Dusan Trivic
committed
update of deepl requests to POST
1 parent bde0ff7 commit 7266430

8 files changed

Lines changed: 726 additions & 735 deletions

File tree

src/QuickTranslateBundle/Controller/DefaultController.php

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,53 @@
1414
use Pimcore\Controller\FrontendController;
1515
use Symfony\Component\HttpFoundation\JsonResponse;
1616
use Pimcore\Model\WebsiteSetting;
17+
use Symfony\Component\HttpFoundation\Request;
1718

1819
class DefaultController extends FrontendController
1920
{
21+
private $authKey;
22+
23+
private $type;
24+
25+
private $domain;
26+
27+
public function __construct()
28+
{
29+
$this->authKey = WebsiteSetting::getByName("deepl_auth_key") ? WebsiteSetting::getByName("deepl_auth_key")->getData() : null;
30+
$this->type = WebsiteSetting::getByName("deepl_type") ? WebsiteSetting::getByName("deepl_type")->getData() : null;
31+
if ($this->type == 'PRO') {
32+
$this->domain = 'https://api.deepl.com';
33+
} else {
34+
$this->domain = 'https://api-free.deepl.com';
35+
}
36+
}
2037

2138
/* used to check if deepl authentication key exists */
2239
public function getAuthKeyAction()
2340
{
24-
$authKey = WebsiteSetting::getByName("deepl_auth_key") ? WebsiteSetting::getByName("deepl_auth_key")->getData() : null;
25-
$type = WebsiteSetting::getByName("deepl_type") ? WebsiteSetting::getByName("deepl_type")->getData() : null;
26-
27-
return new JsonResponse([
28-
"authKey" => $authKey,
29-
"exists" => (($authKey == null || "") ? false : true),
30-
"type" => $type,
31-
"type_exists" => (($type == null || "") ? false : true),
41+
return JsonResponse::create([
42+
"authKey" => $this->authKey,
43+
"exists" => (($this->authKey == null || "") ? false : true),
44+
"type" => $this->type,
45+
"type_exists" => (($this->type == null || "") ? false : true),
3246
]);
3347
}
3448

3549
public function getGlossariesAction()
3650
{
37-
$authKey = WebsiteSetting::getByName("deepl_auth_key") ? WebsiteSetting::getByName("deepl_auth_key")->getData() : null;
38-
$type = WebsiteSetting::getByName("deepl_type") ? WebsiteSetting::getByName("deepl_type")->getData() : null;
39-
4051
$glossaries = null;
4152

42-
if ($authKey) {
53+
if ($this->authKey) {
4354
$headers = [
44-
'Authorization' => 'DeepL-Auth-Key ' . $authKey,
55+
'Authorization' => 'DeepL-Auth-Key ' . $this->authKey,
4556
];
4657

4758
$client = new Client([
4859
'headers' => $headers
4960
]);
5061

51-
if ($type == 'PRO') {
52-
$response = $client->request('GET', 'https://api.deepl.com/v2/glossaries');
53-
} else {
54-
$response = $client->request('GET', 'https://api-free.deepl.com/v2/glossaries');
55-
}
62+
63+
$response = $client->request('GET', $this->domain . '/v2/glossaries');
5664

5765
if ($response->getStatusCode() == 200) {
5866

@@ -66,10 +74,52 @@ public function getGlossariesAction()
6674

6775
}
6876

69-
return new JsonResponse([
77+
return JsonResponse::create([
7078
"glossaries" => $glossaries
7179
]);
7280
}
7381

82+
public function translate(Request $request): JsonResponse
83+
{
84+
$payload = json_decode($request->getContent(), true);
85+
86+
$url = $this->domain . '/v2/translate';
87+
88+
$body = [
89+
'text' => [$payload['text']] ?? [],
90+
'target_lang' => $payload['target_lang'] ?? null,
91+
'source_lang' => $payload['source_lang'] ?? null,
92+
'split_sentences' => $payload['split_sentences'] ?? 'nonewlines',
93+
'tag_handling' => $payload['tag_handling'] ?? 'xml',
94+
'glossary_id' => $payload['glossary_id'] ?? null,
95+
];
96+
97+
try {
98+
$client = new Client();
99+
100+
$response = $client->request('POST', $url, [
101+
'headers' => [
102+
'Authorization' => 'DeepL-Auth-Key ' . $this->authKey,
103+
'Content-Type' => 'application/json',
104+
],
105+
'body' => json_encode($body),
106+
]);
107+
if ($response->getStatusCode() == 200) {
108+
$data = $response->getBody()->getContents();
74109

110+
return new JsonResponse(json_decode($data, true));
111+
} else {
112+
113+
}
114+
} catch (\GuzzleHttp\Exception\ClientException $e) {
115+
// Capture 4xx errors (e.g., bad request)
116+
$errorResponse = $e->getResponse();
117+
$errorData = $errorResponse
118+
? json_decode($errorResponse->getBody()->getContents(), true)
119+
: ['error' => 'Unknown client error occurred.'];
120+
return new JsonResponse($errorData, $errorResponse->getStatusCode());
121+
} catch (\Exception $e) {
122+
return new JsonResponse(['error' => $e->getMessage()], 500);
123+
}
124+
}
75125
}

src/QuickTranslateBundle/Resources/config/pimcore/routing.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ asioso_quick_translate_check_if_exists:
1818
path: /asioso_quick_translate_check_if_exists
1919
controller: Asioso\QuickTranslateBundle\Controller\DocumentController::checkIfExistsAction
2020

21-
asioso_quick_translate_get_auth_key:
22-
path: /asioso_quick_translate_get_auth_key
23-
controller: Asioso\QuickTranslateBundle\Controller\DefaultController::getAuthKeyAction
21+
#asioso_quick_translate_get_auth_key:
22+
# path: /asioso_quick_translate_get_auth_key
23+
# controller: Asioso\QuickTranslateBundle\Controller\DefaultController::getAuthKeyAction
2424

2525
asioso_quick_translate_get_glossaries:
2626
path: /asioso_quick_translate_get_glossaries
2727
controller: Asioso\QuickTranslateBundle\Controller\DefaultController::getGlossariesAction
2828

29+
asioso_quick_translate_text:
30+
path: /asioso_quick_translate_text
31+
controller: Asioso\QuickTranslateBundle\Controller\DefaultController::translate

src/QuickTranslateBundle/Resources/public/js/pimcore/startup.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,34 @@ pimcore.plugin.asiosoQuickTranslateBundle = Class.create({
7070

7171

7272
docBtn: function (document) {
73-
7473
var menuParent;
74+
75+
var toolbarItems = document.toolbar.items.items;
76+
77+
var menuButton = toolbarItems.find(item => {
78+
return (
79+
item.btnInnerEl?.component?.menu?.items?.items &&
80+
item.btnInnerEl.component.menu.items.items[0]?.menu
81+
);
82+
});
83+
84+
if (!menuButton) {
85+
console.error("Menu button not found.");
86+
return;
87+
}
88+
7589
if (document.data.locked) {
76-
menuParent = document.toolbar.items.items[6].btnInnerEl.component.menu.items.items[0].menu;
90+
menuParent = menuButton.btnInnerEl.component.menu.items.items[0].menu;
7791
} else {
78-
if(document.data.id == 1 ){
79-
menuParent = document.toolbar.items.items[7].btnInnerEl.component.menu.items.items[0].menu;
80-
}else{
81-
menuParent = document.toolbar.items.items[9].btnInnerEl.component.menu.items.items[0].menu;
82-
}
92+
menuParent = menuButton.btnInnerEl.component.menu.items.items[0].menu;
8393
}
8494

8595
menuParent.add({
8696
text: t('Quick Translate'),
8797
iconCls: 'quick-translate-icon',
8898
scale: 'small',
8999
handler: function () {
90-
quickTranslateDocument(document)
100+
quickTranslateDocument(document);
91101
}
92102
});
93103
}

src/QuickTranslateBundle/Resources/public/js/quick-translate-api/quickTranslate.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@
77
*
88
*/
99

10-
function quickTranslate(key, type, data,srcSet = null,langFrom, langTo, id,successCallback, failCallback) {
10+
function quickTranslate(data,srcSet = null,langFrom, langTo, id,successCallback, failCallback) {
1111

12-
var url = createDeeplApiUrl(key, type, data, langFrom, langTo);
13-
14-
var settings = {
15-
"async": true,
16-
"crossDomain": true,
17-
"url": url,
18-
"method": "GET",
19-
"headers": {},
20-
};
12+
var settings = createDeeplApiSettings(data, langFrom, langTo);
2113

2214
var translatingWindow = quickTranslatecreateWindow("Translating", "Translating your content, please wait...");
2315

0 commit comments

Comments
 (0)