Skip to content

Commit

Permalink
Update translate command to use the DeepL official deepl php sdk
Browse files Browse the repository at this point in the history
Remove free option in command since deepl automaticaly
Remove unused class in use
Update readme
  • Loading branch information
GautierDig committed Jun 7, 2023
1 parent 63daa99 commit a21f3e1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions Command/TranslationExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace Diglin\Bundle\DeeplBundle\Command;

use BabyMarkt\DeepL\DeepL;
use DeepL\Translator as DeepL;
use DeepL\TranslatorOptions;
use Doctrine\ORM\Query\Expr\Join;
use Oro\Bundle\TranslationBundle\Entity\Translation;
use Oro\Bundle\TranslationBundle\Provider\LanguageProvider;
Expand Down Expand Up @@ -84,11 +85,6 @@ public function configure()
null,
InputOption::VALUE_OPTIONAL,
'Deepl API Dev Key. API key is looked up 1) at the Oro Config, 2) via the cli parameter, 3) into the var/deepl-license.key'
)->addOption(
'free-license',
null,
InputOption::VALUE_NONE,
'if you use the free license API of Deepl'
)->addOption(
'disable-deepl',
'd',
Expand Down Expand Up @@ -197,6 +193,7 @@ public function execute(InputInterface $input, OutputInterface $output)
'english_value' => $translation['englishValue'],
$locale => $translated,
];
break;
}

$output->writeln('');
Expand Down Expand Up @@ -232,9 +229,11 @@ public function execute(InputInterface $input, OutputInterface $output)
}

if (isset($this->deepl)) {
$usageArray = $this->deepl->usage();
$output->writeln(sprintf('You have used %s of %s in the current billing period.', $usageArray['character_count'], $usageArray['character_limit']));
$usageArray = $this->deepl->getUsage();
$output->writeln(sprintf('You have used %s of %s in the current billing period.', $usageArray->character->count, $usageArray->character->limit));
}

return 0;
}

/**
Expand Down Expand Up @@ -360,12 +359,10 @@ private function initDeepl(InputInterface $input): void
{
if (!$input->getOption('disable-deepl') && !$input->getOption('simulate')) {
$apiKey = $this->getDeeplApiKey($input);
$endpointUrl = $input->getOption('free-license') ? 'api-free.deepl.com' : 'api.deepl.com';

if (empty($apiKey)) {
throw new \Exception('DeepL API Key not defined. Please, go to OroPlatform Backoffice, menu System > Configuration > System Configuration > Integrations > DeepL. Or provide the key by the parameter "deepl-api-key" or set the key into the var/deepl-license.key file.');
} else {
$this->deepl = new DeepL($apiKey, 2, $endpointUrl);
$this->deepl = new DeepL($apiKey);
}
}
}
Expand All @@ -379,8 +376,8 @@ private function translate(array $translation): string
$localeLocale = explode('_', $translation['code']);
$localeLang = $localeLocale[0];// e.g. de for german
$text = str_replace($tagsToIgnore, $tagsIgnored, $translation['englishValue']);
$deeplTrans = $this->deepl->translate($text, $srcLang, $localeLang, 'xml', ['ignore']);
$deeplTrans = $this->deepl->translateText($text, $srcLang, $localeLang, ['tag_handling' => 'xml', 'ignore_tags' => 'ignore']);

return str_replace($tagsIgnored, $tagsToIgnore, $deeplTrans[0]['text']);
return str_replace($tagsIgnored, $tagsToIgnore, $deeplTrans->text);
}
}
4 changes: 2 additions & 2 deletions Command/TranslationLoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -171,5 +169,7 @@ public function execute(InputInterface $input, OutputInterface $output)

$output->writeln(sprintf('<fg=green>The file %s has been merged with %s with success.</>', $file, $target));
$output->writeln(sprintf('<fg=yellow>Then run bin/console --env=prod cache:clear && bin/console --env=prod oro:translation:load.</>'));

return 0;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Same as for the CSV export, on Production environment, you just need to let the

## Requirement

- OroPlatform 4.0.x | 4.1.x | 4.2.x (may work on previous versions but not tested)
- OroPlatform 4.0.x | 4.1.x | 4.2.x | 5.1.* (may work on previous versions but not tested)

## Configuration

Expand Down

0 comments on commit a21f3e1

Please sign in to comment.