From a5d18557b63125b090d8f0e73de88e62cd9a9028 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 12:07:17 +0200 Subject: [PATCH 1/6] fix empty data --- src/DivanteTranslationBundle/Controller/ObjectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DivanteTranslationBundle/Controller/ObjectController.php b/src/DivanteTranslationBundle/Controller/ObjectController.php index dd68645..389f73e 100644 --- a/src/DivanteTranslationBundle/Controller/ObjectController.php +++ b/src/DivanteTranslationBundle/Controller/ObjectController.php @@ -41,7 +41,7 @@ public function translateFieldAction(Request $request, ProviderFactory $provider $lang = $request->get('lang'); $fieldName = 'get' . ucfirst($request->get('fieldName')); - $data = $object->$fieldName($lang) ?? $object->$fieldName($this->sourceLanguage); + $data = $object->$fieldName($lang) ?: $object->$fieldName($this->sourceLanguage); if (!$data) { return $this->adminJson([ From 027e1efe207cc311b83b85e495a079150d340cc5 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 13:32:45 +0200 Subject: [PATCH 2/6] add provider controller --- src/DivanteTranslationBundle/Controller/ObjectController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/DivanteTranslationBundle/Controller/ObjectController.php b/src/DivanteTranslationBundle/Controller/ObjectController.php index 389f73e..d8c2bf4 100644 --- a/src/DivanteTranslationBundle/Controller/ObjectController.php +++ b/src/DivanteTranslationBundle/Controller/ObjectController.php @@ -51,6 +51,10 @@ public function translateFieldAction(Request $request, ProviderFactory $provider } $provider = $providerFactory->get($this->provider); + if ($request->get('formality') && ($this->provider === 'deepl' || $this->provider === 'deepl_free')) { + $provider->setFormality($request->get('formality')); + } + $data = $provider->translate($data, $lang); } catch (\Throwable $exception) { return $this->adminJson([ From 239a848297ebe764debaedf754e8ec7204a2dff1 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 13:33:25 +0200 Subject: [PATCH 3/6] set var for provider --- .../Resources/public/js/pimcore/startup.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/startup.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/startup.js index 859cea7..ab9233b 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/startup.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/startup.js @@ -6,6 +6,15 @@ pimcore.plugin.TranslationBundle = Class.create(pimcore.plugin.admin, { }, initialize: function () { + Ext.Ajax.request({ + url: "/admin/translate-provider", + method: "GET", + success: function (response) { + var res = Ext.decode(response.responseText); + pimcore.globalmanager.add('translationBundle_provider', res.provider); + } + }); + pimcore.plugin.broker.registerPlugin(this); }, }); From 7d551afc1928c770a06d93562267ee1c245aed79 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 13:33:35 +0200 Subject: [PATCH 4/6] set formality button --- .../js/pimcore/object/elementservice.js | 117 +++++++++++------- 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js index 7de5bf5..89f73f0 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js @@ -1,54 +1,77 @@ -/** - * Pimcore - * - * This source file is available under two different licenses: - * - GNU General Public License version 3 (GPLv3) - * - Pimcore Enterprise License (PEL) - * Full copyright and license information is available in - * LICENSE.md which is distributed with this source code. - * - * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) - * @license http://www.pimcore.org/license GPLv3 and PEL - */ - pimcore.registerNS("pimcore.object.elementservice.x"); pimcore.object.elementservice.translateButton = function (id, fieldName, component, type, lang) { - return new Ext.Button({ - iconCls: "pimcore_icon_translations", - cls: 'pimcore_button_transparent', - tooltip: t("translate_field"), - handler: function () { - Ext.Ajax.request({ - url: "/admin/object/translate-field", - method: "GET", - params: { - sourceId: id, - fieldName: fieldName, - lang: lang, - type: type + var provider = pimcore.globalmanager.get('translationBundle_provider'); + + if (provider === 'deepl' || provider === 'deepl_free') { + return new Ext.Button({ + iconCls: "pimcore_icon_translations", + cls: 'pimcore_button_transparent', + tooltip: t("translate_field"), + menu: [ + { + text: 'Formality Default', + handler: function () { + handleTranslationRequest(id, fieldName, component, type, lang, 'default') + }.bind(this), + }, + { + text: 'Formality More', + handler: function () { + handleTranslationRequest(id, fieldName, component, type, lang, 'more') + }.bind(this), + }, + { + text: 'Formality Less', + handler: function () { + handleTranslationRequest(id, fieldName, component, type, lang, 'less') + }.bind(this), }, - success: function (response) { - var res = Ext.decode(response.responseText); + ], + style: "margin-left: 10px; filter:grayscale(100%);", + }); + } else { + return new Ext.Button({ + iconCls: "pimcore_icon_translations", + cls: 'pimcore_button_transparent', + tooltip: t("translate_field"), + handler: function () { + handleTranslationRequest(id, fieldName, component, type, lang, '') + }.bind(this), + style: "margin-left: 10px; filter:grayscale(100%);", + }); + } +}; + +function handleTranslationRequest(id, fieldName, component, type, lang, formality) { + Ext.Ajax.request({ + url: "/admin/object/translate-field", + method: "GET", + params: { + sourceId: id, + fieldName: fieldName, + lang: lang, + type: type, + formality: formality + }, + success: function (response) { + var res = Ext.decode(response.responseText); - if (res.success) { - switch (type) { - case 'wysiwyg': - CKEDITOR.instances[component.editableDivId].setData(res.data); - break; - case 'input': - component.setRawValue(res.data); - break; - case 'textarea': - component.component.setValue(res.data); - break; - } - } else { - pimcore.helpers.showPrettyError('object', t("error"), t("saving_failed"), res.message); - } + if (res.success) { + switch (type) { + case 'wysiwyg': + CKEDITOR.instances[component.editableDivId].setData(res.data); + break; + case 'input': + component.setRawValue(res.data); + break; + case 'textarea': + component.component.setValue(res.data); + break; } - }); - }.bind(this), - style: "margin-left: 10px; filter:grayscale(100%);", + } else { + pimcore.helpers.showPrettyError('object', t("error"), t("saving_failed"), res.message); + } + } }); -}; +} From 3d727faaff8e6a0074d75cd6eb21f211a344feb8 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 13:33:44 +0200 Subject: [PATCH 5/6] add provider controller --- .../Controller/ProviderController.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/DivanteTranslationBundle/Controller/ProviderController.php diff --git a/src/DivanteTranslationBundle/Controller/ProviderController.php b/src/DivanteTranslationBundle/Controller/ProviderController.php new file mode 100644 index 0000000..18b4fd0 --- /dev/null +++ b/src/DivanteTranslationBundle/Controller/ProviderController.php @@ -0,0 +1,33 @@ +sourceLanguage = $sourceLanguage; + $this->provider = $provider; + } + + /** + * @Route("/translate-provider", methods={"GET"}) + */ + public function translationProviderInfoAction(): JsonResponse + { + return $this->adminJson([ + 'provider' => $this->provider + ]); + } +} From 53fe76278657130f223fae504d922bcbfaf07752 Mon Sep 17 00:00:00 2001 From: Bruno Ramalho Date: Mon, 21 Jun 2021 14:15:35 +0200 Subject: [PATCH 6/6] add icons --- .../Resources/public/js/pimcore/object/elementservice.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js index 89f73f0..af37313 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js @@ -14,18 +14,21 @@ pimcore.object.elementservice.translateButton = function (id, fieldName, compone handler: function () { handleTranslationRequest(id, fieldName, component, type, lang, 'default') }.bind(this), + iconCls: "pimcore_icon_more", }, { text: 'Formality More', handler: function () { handleTranslationRequest(id, fieldName, component, type, lang, 'more') }.bind(this), + iconCls: 'pimcore_icon_up' }, { text: 'Formality Less', handler: function () { handleTranslationRequest(id, fieldName, component, type, lang, 'less') }.bind(this), + iconCls: 'pimcore_icon_down' }, ], style: "margin-left: 10px; filter:grayscale(100%);",