Skip to content

Commit

Permalink
Merge pull request #18 from DivanteLtd/feature/deepl
Browse files Browse the repository at this point in the history
DeepL Formality
  • Loading branch information
bramalho authored Jun 21, 2021
2 parents 75d07ae + 53fe762 commit c70bc17
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 48 deletions.
6 changes: 5 additions & 1 deletion src/DivanteTranslationBundle/Controller/ObjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand Down
33 changes: 33 additions & 0 deletions src/DivanteTranslationBundle/Controller/ProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DivanteTranslationBundle\Controller;

use DivanteTranslationBundle\Provider\ProviderFactory;
use Pimcore\Bundle\AdminBundle\Controller\AdminController;
use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/admin")
*/
final class ProviderController extends AdminController
{
private string $sourceLanguage;
private string $provider;

public function __construct(string $sourceLanguage, string $provider)
{
$this->sourceLanguage = $sourceLanguage;
$this->provider = $provider;
}

/**
* @Route("/translate-provider", methods={"GET"})
*/
public function translationProviderInfoAction(): JsonResponse
{
return $this->adminJson([
'provider' => $this->provider
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,80 @@
/**
* 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),
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'
},
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);
}
}
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
});
Expand Down

0 comments on commit c70bc17

Please sign in to comment.