-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5088a1
commit 0d3a457
Showing
9 changed files
with
311 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
_instanceof: | ||
Symfony\Component\Console\Command\Command: | ||
tags: ['command'] | ||
|
||
Atoolo\Translator\Console\: | ||
resource: '../src/Console' | ||
|
||
Atoolo\Translator\Service\: | ||
resource: '../src/Service' | ||
|
||
Atoolo\Translator\Adapter\: | ||
resource: '../src/Adapter' | ||
|
||
atoolo_translator.textHasher: | ||
class: Atoolo\Translator\Service\TextHasher | ||
|
||
atoolo_translator.translator.adapter: | ||
class: Atoolo\Translator\Adapter\DeeplAdapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Translator\Console; | ||
|
||
use Symfony\Component\Console\Application as BaseApplication; | ||
use Symfony\Component\Console\Command\Command; | ||
|
||
class Application extends BaseApplication | ||
{ | ||
/** | ||
* @param iterable<Command> $commands | ||
*/ | ||
public function __construct(iterable $commands = []) | ||
{ | ||
parent::__construct(); | ||
foreach ($commands as $command) { | ||
$this->add($command); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Translator\Console\Command; | ||
|
||
use Atoolo\Translator\Console\Command\Io\TypifiedInput; | ||
use Atoolo\Translator\Dto\Format; | ||
use Atoolo\Translator\Dto\TranslationParameter; | ||
use Atoolo\Translator\Service\TextHasher; | ||
use Psr\Cache\InvalidArgumentException; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
#[AsCommand( | ||
name: 'translator:cache:get', | ||
description: 'get cached translations', | ||
)] | ||
class CacheGet extends Command | ||
{ | ||
public function __construct( | ||
private readonly CacheInterface $translationCache, | ||
#[Autowire(service: 'atoolo_translator.textHasher')] | ||
private readonly TextHasher $textHasher, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setHelp('Command to list cached translations.') | ||
->addArgument( | ||
'text', | ||
InputArgument::REQUIRED, | ||
'Text whose translation is to be retrieved from the cache.', | ||
) | ||
->addOption( | ||
'sourceLang', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Language to be used. (de, en, fr, it, ...)', | ||
'de', | ||
) | ||
->addOption( | ||
'targetLang', | ||
'l', | ||
InputOption::VALUE_REQUIRED, | ||
'Language to be used. (de, en, fr, it, ...)', | ||
); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function execute( | ||
InputInterface $input, | ||
OutputInterface $output, | ||
): int { | ||
|
||
$typedInput = new TypifiedInput($input); | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$sourceLang = $typedInput->getStringOption('sourceLang'); | ||
$targetLang = $typedInput->getStringOption('targetLang'); | ||
$text = $typedInput->getStringArgument('text'); | ||
|
||
$translationParamter = new TranslationParameter($sourceLang, $targetLang, Format::TEXT); | ||
$hash = $this->textHasher->hash($text, $translationParamter); | ||
|
||
$translation = $this->translationCache->get($hash, function () { | ||
return null; | ||
}); | ||
|
||
$io->text($translation ?? 'No translation found in cache.'); | ||
Check failure on line 82 in src/Console/Command/CacheGet.php GitHub Actions / verify / Composer Verify (PHP 8.2)
Check failure on line 82 in src/Console/Command/CacheGet.php GitHub Actions / verify / Composer Verify (PHP 8.3)
|
||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Translator\Console\Command\Io; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
class TypifiedInput | ||
{ | ||
public function __construct(private readonly InputInterface $input) {} | ||
|
||
public function getStringOption(string $name): string | ||
{ | ||
$value = $this->input->getOption($name); | ||
if (!is_string($value)) { | ||
throw new InvalidArgumentException( | ||
'option ' . $name . ' must be a string: ' . $value, | ||
); | ||
} | ||
return $value; | ||
} | ||
|
||
public function getStringArgument(string $name): string | ||
{ | ||
$value = $this->input->getArgument($name); | ||
if (!is_string($value)) { | ||
throw new InvalidArgumentException( | ||
'argument ' . $name . ' must be a string', | ||
); | ||
} | ||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Translator\Console\Command; | ||
|
||
use Atoolo\Translator\Console\Command\Io\TypifiedInput; | ||
use Atoolo\Translator\Dto\Format; | ||
use Atoolo\Translator\Dto\TranslationParameter; | ||
use Atoolo\Translator\Service\TextHasher; | ||
use Atoolo\Translator\Service\Translator; | ||
use Psr\Cache\InvalidArgumentException; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
#[AsCommand( | ||
name: 'translator:translate', | ||
description: 'translate text, and cache the result', | ||
)] | ||
class Translate extends Command | ||
{ | ||
public function __construct( | ||
private readonly Translator $translator, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setHelp('Command to translate a text.') | ||
->addArgument( | ||
'text', | ||
InputArgument::REQUIRED, | ||
'Text to translate.', | ||
) | ||
->addOption( | ||
'sourceLang', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Language to be used. (de, en, fr, it, ...)', | ||
'de', | ||
) | ||
->addOption( | ||
'targetLang', | ||
'l', | ||
InputOption::VALUE_REQUIRED, | ||
'Language to be used. (de, en, fr, it, ...)', | ||
) | ||
->addOption( | ||
'format', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'text | html', | ||
'text', | ||
); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function execute( | ||
InputInterface $input, | ||
OutputInterface $output, | ||
): int { | ||
|
||
$typedInput = new TypifiedInput($input); | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$sourceLang = $typedInput->getStringOption('sourceLang'); | ||
$targetLang = $typedInput->getStringOption('targetLang'); | ||
$text = $typedInput->getStringArgument('text'); | ||
$format = Format::from($typedInput->getStringOption('format')); | ||
|
||
$parameter = new TranslationParameter( | ||
sourceLang: $sourceLang, | ||
targetLang: $targetLang, | ||
format: $format, | ||
); | ||
|
||
$translated = $this->translator->translate([$text], $parameter); | ||
|
||
$io->text($translated[0]); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.