Skip to content

Commit 9e635a7

Browse files
authored
Merge pull request #14 from Maksonis/master
Add glossary function
2 parents 6516cfa + 4fcc08e commit 9e635a7

3 files changed

Lines changed: 85 additions & 8 deletions

File tree

src/QuickTranslateBundle/Controller/DefaultController.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
*
55
* Full copyright and license information is available in LICENSE.md which is distributed with this source code.
66
*
7-
* @copyright Copyright (c) Asioso GmbH (https://www.asioso.com)
7+
* @copyright Copyright (c) Asioso GmbH (https://www.asioso.com)
88
*
99
*/
1010

1111
namespace Asioso\QuickTranslateBundle\Controller;
1212

13+
use GuzzleHttp\Client;
1314
use Pimcore\Controller\FrontendController;
1415
use Symfony\Component\HttpFoundation\JsonResponse;
1516
use Pimcore\Model\WebsiteSetting;
@@ -24,12 +25,51 @@ public function getAuthKeyAction()
2425
$type = WebsiteSetting::getByName("deepl_type") ? WebsiteSetting::getByName("deepl_type")->getData() : null;
2526

2627
return JsonResponse::create([
27-
"authKey" => $authKey,
28-
"exists" => (($authKey == null || "") ? false : true),
29-
"type" => $type,
30-
"type_exists" => (($type == null || "") ? false : true),
28+
"authKey" => $authKey,
29+
"exists" => (($authKey == null || "") ? false : true),
30+
"type" => $type,
31+
"type_exists" => (($type == null || "") ? false : true),
3132
]);
3233
}
3334

34-
35+
public function getGlossariesAction()
36+
{
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+
40+
$glossaries = null;
41+
42+
if ($authKey) {
43+
$headers = [
44+
'Authorization' => 'DeepL-Auth-Key ' . $authKey,
45+
];
46+
47+
$client = new Client([
48+
'headers' => $headers
49+
]);
50+
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+
}
56+
57+
if ($response->getStatusCode() == 200) {
58+
59+
$resultDecoded = json_decode($response->getBody()->getContents(), true);
60+
61+
if (isset($resultDecoded['glossaries'])) {
62+
$glossaries = $resultDecoded['glossaries'];
63+
}
64+
65+
}
66+
67+
}
68+
69+
return JsonResponse::create([
70+
"glossaries" => $glossaries
71+
]);
72+
}
73+
74+
3575
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ asioso_quick_translate_get_auth_key:
2222
path: /asioso_quick_translate_get_auth_key
2323
controller: Asioso\QuickTranslateBundle\Controller\DefaultController::getAuthKeyAction
2424

25+
asioso_quick_translate_get_glossaries:
26+
path: /asioso_quick_translate_get_glossaries
27+
controller: Asioso\QuickTranslateBundle\Controller\DefaultController::getGlossariesAction
28+

src/QuickTranslateBundle/Resources/public/js/utilities/createDeeplApiUrl.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,47 @@ function createDeeplApiUrl(key, type, data, langFrom = null, langTo, autoDetect
1717

1818
langTo = langTo.split("_")[0];
1919
langTo = langTo.toUpperCase();
20+
2021
if (type == "PRO") {
2122
url = 'https://api.deepl.com';
2223
} else {
2324
url = 'https://api-free.deepl.com';
2425
}
26+
27+
var glossaryId = null;
28+
29+
Ext.Ajax.request({
30+
url: "/asioso_quick_translate_get_glossaries",
31+
async: false,
32+
success: function (response) {
33+
var data = Ext.decode(response.responseText);
34+
var glossaries = data.glossaries;
35+
36+
37+
if(glossaries) {
38+
var glossariesFiltered = glossaries.filter((glossary) => {
39+
return (glossary.source_lang.toUpperCase() === langFrom && glossary.target_lang.toUpperCase() === langTo)
40+
});
41+
42+
if(glossariesFiltered && glossariesFiltered[0] !== undefined) {
43+
glossaryId = glossariesFiltered[0].glossary_id;
44+
}
45+
}
46+
},
47+
failure: function () {
48+
quickTranslatecreateWindow("Connection error", "We encountered an error while getting the glossaries. Internal server error.");
49+
}
50+
});
51+
52+
var glossaryPart = '';
53+
if(glossaryId) {
54+
glossaryPart = '&glossary_id=' + glossaryId;
55+
}
56+
2557
if (autoDetect) {
26-
return url + '/v2/translate?auth_key=' + key + '&text=' + data + '&target_lang=' + langTo + '&split_sentences=nonewlines&tag_handling=xml';
58+
return url + '/v2/translate?auth_key=' + key + '&text=' + data + '&target_lang=' + langTo + '&split_sentences=nonewlines&tag_handling=xml' + glossaryPart;
2759
}
28-
return url + '/v2/translate?auth_key=' + key + '&text=' + data + '&source_lang=' + langFrom + '&target_lang=' + langTo + '&split_sentences=nonewlines&tag_handling=xml';
60+
61+
return url + '/v2/translate?auth_key=' + key + '&text=' + data + '&source_lang=' + langFrom + '&target_lang=' + langTo + '&split_sentences=nonewlines&tag_handling=xml' + glossaryPart;
2962

3063
};

0 commit comments

Comments
 (0)