Skip to content

Commit dad625e

Browse files
authored
Merge pull request #36 from quicktranslate-module/master-3.x
translate api update to POST request
2 parents 3e27035 + afdc0e6 commit dad625e

8 files changed

Lines changed: 721 additions & 735 deletions

File tree

src/QuickTranslateBundle/Controller/DefaultController.php

Lines changed: 67 additions & 17 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-
2741
return JsonResponse::create([
28-
"authKey" => $authKey,
29-
"exists" => (($authKey == null || "") ? false : true),
30-
"type" => $type,
31-
"type_exists" => (($type == null || "") ? false : true),
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

@@ -71,5 +79,47 @@ public function getGlossariesAction()
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
@@ -67,24 +67,34 @@ pimcore.plugin.asiosoQuickTranslateBundle = Class.create(pimcore.plugin.admin, {
6767

6868

6969
docBtn: function (document) {
70-
7170
var menuParent;
71+
72+
var toolbarItems = document.toolbar.items.items;
73+
74+
var menuButton = toolbarItems.find(item => {
75+
return (
76+
item.btnInnerEl?.component?.menu?.items?.items &&
77+
item.btnInnerEl.component.menu.items.items[0]?.menu
78+
);
79+
});
80+
81+
if (!menuButton) {
82+
console.error("Menu button not found.");
83+
return;
84+
}
85+
7286
if (document.data.locked) {
73-
menuParent = document.toolbar.items.items[6].btnInnerEl.component.menu.items.items[0].menu;
87+
menuParent = menuButton.btnInnerEl.component.menu.items.items[0].menu;
7488
} else {
75-
if(document.data.id == 1 ){
76-
menuParent = document.toolbar.items.items[7].btnInnerEl.component.menu.items.items[0].menu;
77-
}else{
78-
menuParent = document.toolbar.items.items[9].btnInnerEl.component.menu.items.items[0].menu;
79-
}
89+
menuParent = menuButton.btnInnerEl.component.menu.items.items[0].menu;
8090
}
8191

8292
menuParent.add({
8393
text: t('Quick Translate'),
8494
iconCls: 'quick-translate-icon',
8595
scale: 'small',
8696
handler: function () {
87-
quickTranslateDocument(document)
97+
quickTranslateDocument(document);
8898
}
8999
});
90100
}

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)